Skip to content

Commit

Permalink
Merge pull request #3 from coreylight/add-timestamp
Browse files Browse the repository at this point in the history
Allow timestamp configuration option
  • Loading branch information
pselle authored Jul 27, 2017
2 parents ac05512 + 6477ef3 commit b3bca9f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -71,6 +77,7 @@ module.exports = class Timeline {
this.data = markData(this, {
name,
startTime: startMark.startTime,
timestamp: startMark.timestamp,
duration,
entryType: 'measure'
});
Expand Down
17 changes: 17 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit b3bca9f

Please sign in to comment.