diff --git a/README.md b/README.md index 588c591..31bd840 100644 --- a/README.md +++ b/README.md @@ -95,5 +95,20 @@ const myMeasure = timeline.getEntriesByName('foo-measure')[0]; // {name: 'foo-measure', startTime: 227851.91, duration: 10.5, entryType: 'measure'} ``` +#### `timestamp` (bool: optional = false) + +Add a `timestamp` (unix epoch) value for each mark based on `Date.now()`. + +```js +const Perf = require('performance-node'); + +const timeline = new Perf({ timestamp: true }); + +timeline.mark('foo-start'); + +const myMeasure = timeline.getEntriesByName('foo-start')[0]; +// {name: 'foo-start', startTime: 1.2, duration: 0, entryType: 'mark', timestamp: 1501189303951} +``` + ## Contributing - This project uses [Prettier](https://github.com/prettier/prettier). Please execute `npm run eslint -- --fix` to auto-format the code before submitting pull requests. diff --git a/src/index.js b/src/index.js index 2d5c0ff..ffdaf96 100644 --- a/src/index.js +++ b/src/index.js @@ -13,23 +13,28 @@ function getOffset(timelineInstance) { } function markData(timelineInstance, obj = {}) { - const { data = [] } = timelineInstance; + const { data = [], useTimestamp } = timelineInstance; const defaultHrtime = process.hrtime(); // set defaults const { name, startTime = hrMillis(defaultHrtime, getOffset(timelineInstance)), + timestamp = Date.now(), duration = 0, entryType = 'mark' } = obj; - const item = { + let item = { name, startTime, duration, entryType }; + if (useTimestamp) { + item.timestamp = timestamp; + } + return data.concat(item).sort((a, b) => a.startTime - b.startTime); } @@ -38,6 +43,7 @@ module.exports = class Timeline { this.constructionTimeMillis = hrMillis(); this.data = []; this.offset = kwargs.offset; + this.useTimestamp = kwargs.timestamp || false; return this; } mark(name) { @@ -71,6 +77,7 @@ module.exports = class Timeline { this.data = markData(this, { name, startTime: startMark.startTime, + timestamp: startMark.timestamp, duration, entryType: 'measure' }); diff --git a/src/index.test.js b/src/index.test.js index e571395..5ff43ea 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -47,6 +47,7 @@ test('Can mark one', () => { expect(_.isNumber(first.startTime)).toBe(true); expect(_.isNumber(first.duration)).toBe(true); expect(_.isString(first.name)).toBe(true); + expect(first.timestamp).toBe(undefined); }); test('Can mark multiple', async () => { @@ -82,6 +83,7 @@ test('Order of marks is correct', async () => { expect(bar1.name).toBe('bar'); expect(bar2.name).toBe('bar'); expect(measure.duration).toEqual(bar2.startTime - foo2.startTime); + expect(measure.timestamp).toBe(undefined); }); test('Can clear marks, measures, and all items', async () => { @@ -208,6 +210,21 @@ test('The lib uses the custom offset option properly, perf.now should be very si expect(_.inRange(number, startMillis - 10, startMillis + 10)).toBe(true); }); +test('The lib uses the timestamp option properly', async () => { + const perf = new lib({ timestamp: true }); + const before = Date.now() - 1; + perf.mark('foo'); + await delay(10); + perf.mark('bar'); + perf.measure('m1', 'foo', 'bar'); + const after = Date.now() + 1; + const { timestamp } = perf.getEntries()[0]; + expect(_.isNumber(timestamp)).toBe(true); + expect(_.inRange(timestamp, before, after)).toBe(true); + const { timestamp: measureTimestamp } = perf.getEntriesByName('m1')[0]; + expect(measureTimestamp).toBe(timestamp); +}); + test('The code is fast enough', async () => { const perf = new lib();