This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
150 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ module.exports = { | |
expect: true, | ||
assert: true, | ||
sleep: true, | ||
wait: true, | ||
onload: true, | ||
}, | ||
rules: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import { start } from '../start'; | ||
|
||
/** | ||
* Get the measurement diff from start | ||
* @param {String} measurement | ||
* @return {Number} | ||
* Add a 'measure' entry measuring the execution of a function | ||
* @param {Function} fn | ||
* @param {String} name | ||
* @return {Any} original method return value | ||
*/ | ||
export const measure = (measurement) => Math.max(window.performance.timing[measurement] - start(), 0); | ||
export async function measure(fn, name) { | ||
const {performance} = window; | ||
const unique = Math.random().toString(32).substr(2); | ||
|
||
const [start, end] = ['start', 'end'].map( | ||
(suffix) => [name, suffix, unique].join('-') | ||
); | ||
|
||
performance.mark(start); | ||
const result = await fn(); | ||
|
||
performance.mark(end); | ||
performance.measure(name, start, end); | ||
performance.clearMarks(start); | ||
performance.clearMarks(end); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
const { measure } = require('.'); | ||
|
||
describe('measure', () => { | ||
const performance = Object.getOwnPropertyDescriptor(window, 'performance'); | ||
|
||
afterEach(() => | ||
Object.defineProperty(window, 'performance', performance) | ||
); | ||
beforeEach(() => { | ||
performance.clearMarks(); | ||
}); | ||
it('Should add a measure entry for an async function', async() => { | ||
await measure( | ||
async() => await wait(50), | ||
'my-function' | ||
); | ||
const [{duration, name}] = performance.getEntriesByType('measure'); | ||
|
||
it('Should return the diff between timing metric to start point', () => { | ||
window.performance = { | ||
timeOrigin: 100, | ||
timing: { | ||
something: 250, | ||
}, | ||
}; | ||
expect(measure('something')).to.equal(150); | ||
expect(name).to.equal('my-function'); | ||
expect(duration).to.be.at.least(50); | ||
expect(duration).to.be.at.most(100); | ||
}); | ||
it('Should add a measure entry for a sync function', () => { | ||
measure( | ||
() => sleep(50), | ||
'my-function' | ||
); | ||
|
||
it('Should return 0 if the time unit it lower than start (e.g. 0)', () => { | ||
window.performance = { | ||
timeOrigin: 100, | ||
timing: { | ||
something: 50, | ||
const [{duration, name}] = performance.getEntriesByType('measure'); | ||
|
||
expect(name).to.equal('my-function'); | ||
expect(duration).to.be.at.least(50); | ||
expect(duration).to.be.at.most(100); | ||
}); | ||
it('Should return original function return value', async() => { | ||
const result = await measure( | ||
async() => { | ||
await wait(50); | ||
return [1, 2, 3]; | ||
}, | ||
}; | ||
expect(measure('something')).to.equal(0); | ||
'my-function' | ||
); | ||
expect(result).to.deep.equal([1, 2, 3]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { start } from '../start'; | ||
|
||
/** | ||
* Get the measurement diff from start | ||
* @param {String} entry | ||
* @return {Number} | ||
*/ | ||
export const measurement = (entry) => Math.max(window.performance.timing[entry] - start(), 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { measurement } = require('.'); | ||
|
||
describe('measurement', () => { | ||
const performance = Object.getOwnPropertyDescriptor(window, 'performance'); | ||
|
||
afterEach(() => | ||
Object.defineProperty(window, 'performance', performance) | ||
); | ||
|
||
it('Should return the diff between timing metric to start point', () => { | ||
window.performance = { | ||
timeOrigin: 100, | ||
timing: { | ||
something: 250, | ||
}, | ||
}; | ||
expect(measurement('something')).to.equal(150); | ||
}); | ||
|
||
it('Should return 0 if the time unit it lower than start (e.g. 0)', () => { | ||
window.performance = { | ||
timeOrigin: 100, | ||
timing: { | ||
something: 50, | ||
}, | ||
}; | ||
expect(measurement('something')).to.equal(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters