-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: improve reproducibility (#2039)
- Loading branch information
1 parent
3cd06f1
commit bad0a69
Showing
10 changed files
with
75 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// @noflow | ||
|
||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
|
||
// Clocks the time taken to execute a test per cycle (secs). | ||
function clock(count, fn) { | ||
const start = process.hrtime.bigint(); | ||
for (let i = 0; i < count; ++i) { | ||
fn(); | ||
} | ||
return Number(process.hrtime.bigint() - start) / count; | ||
} | ||
|
||
if (require.main === module) { | ||
const modulePath = process.env.BENCHMARK_MODULE_PATH; | ||
assert(typeof modulePath === 'string'); | ||
assert(process.send); | ||
const module = require(modulePath); | ||
|
||
clock(7, module.measure); // warm up | ||
process.nextTick(() => { | ||
process.send({ | ||
name: module.name, | ||
clocked: clock(module.count, module.measure), | ||
}); | ||
}); | ||
} | ||
|
||
function sampleModule(modulePath) { | ||
return new Promise((resolve, reject) => { | ||
const env = { BENCHMARK_MODULE_PATH: modulePath }; | ||
const child = cp.fork(__filename, { env }); | ||
let message; | ||
let error; | ||
|
||
child.on('message', msg => (message = msg)); | ||
child.on('error', e => (error = e)); | ||
child.on('close', () => { | ||
if (message) { | ||
return resolve(message); | ||
} | ||
reject(error || new Error('Forked process closed without error')); | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = { sampleModule }; |
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
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