-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Some benchmark performance improvements
- Loading branch information
Showing
2 changed files
with
42 additions
and
18 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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
var performanceNow, | ||
let callGarbageCollection, | ||
performanceNow, | ||
performance, | ||
nowOverhead, | ||
polystart, | ||
hrtime; | ||
hrtime, | ||
v8, | ||
vm, | ||
gc; | ||
|
||
if (Blast.isNode) { | ||
v8 = require('v8'); | ||
vm = require('vm'); | ||
v8.setFlagsFromString('--expose_gc'); | ||
gc = vm.runInNewContext('gc'); | ||
callGarbageCollection = () => { | ||
if (gc) gc(); | ||
} | ||
} else { | ||
callGarbageCollection = () => {}; | ||
} | ||
|
||
// Get the performance object, if it exists | ||
performance = Blast.Globals.performance || {}; | ||
|
@@ -81,6 +97,8 @@ Blast.benchmark_async_sizes = [120, 120, 120, 240, 240, 480]; | |
*/ | ||
Blast.defineStatic('Function', 'benchmark', function benchmark(fn, acb) { | ||
|
||
callGarbageCollection(); | ||
|
||
if (fn.length == 0) { | ||
return doSyncBench(fn, acb); | ||
} | ||
|
@@ -94,7 +112,7 @@ Blast.defineStatic('Function', 'benchmark', function benchmark(fn, acb) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.2 | ||
* @version 0.5.4 | ||
* @version 0.9.3 | ||
* | ||
* @param {number} runs | ||
*/ | ||
|
@@ -125,6 +143,8 @@ function getFunctionOverhead(runs) { | |
result = 0.5; | ||
} | ||
|
||
callGarbageCollection(); | ||
|
||
return result; | ||
} | ||
|
||
|
@@ -133,7 +153,7 @@ function getFunctionOverhead(runs) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.1.2 | ||
* @version 0.5.9 | ||
* @version 0.9.3 | ||
* | ||
* @param {Function} fn | ||
* @param {number} runs Expected runs per 50ms | ||
|
@@ -177,6 +197,8 @@ function syncTest(fn, runs, iterOverhead) { | |
// Get the elapsed time | ||
elapsed = Blast.performanceNow() - start; | ||
|
||
callGarbageCollection(); | ||
|
||
// Remove the function call overhead | ||
elapsed = elapsed - (shard * (iterOverhead/runs)); | ||
|
||
|
@@ -244,11 +266,13 @@ function asyncTest(fn, testruns, asyncOverhead, callback) { | |
// Calculate how many times to do the test | ||
shard = 1 + ~~((testruns/300)*sizes[r]); | ||
|
||
callGarbageCollection(); | ||
|
||
Fn.doAmount(shard, fn, function done(err, runs, elapsed) { | ||
elapsed = elapsed - (shard * (asyncOverhead/testruns)); | ||
samples.push(~~((shard/elapsed)*1000)); | ||
total += shard; | ||
|
||
callGarbageCollection(); | ||
next(); | ||
}); | ||
|
||
|
@@ -356,22 +380,14 @@ var benchQueue = [], | |
*/ | ||
function doAsyncBench(fn, callback) { | ||
|
||
var fnOverhead, | ||
pretotal, | ||
result, | ||
args, | ||
i; | ||
|
||
if (benchRunning > 0) { | ||
|
||
// Keep function optimized by not leaking the `arguments` object | ||
args = new Array(arguments.length); | ||
for (i = 0; i < args.length; i++) args[i] = arguments[i]; | ||
|
||
let args = [...arguments]; | ||
benchQueue.push(args); | ||
return; | ||
} | ||
|
||
let pledge = new Classes.Pledge.Swift(); | ||
|
||
benchRunning++; | ||
|
||
// See how many times we can get it to run for 300ms | ||
|
@@ -384,6 +400,12 @@ function doAsyncBench(fn, callback) { | |
var next, | ||
name; | ||
|
||
if (err) { | ||
pledge.reject(err); | ||
} else { | ||
pledge.resolve(result); | ||
} | ||
|
||
// Call the callback | ||
if (callback) { | ||
result.name = fn.name || ''; | ||
|
@@ -410,6 +432,8 @@ function doAsyncBench(fn, callback) { | |
}); | ||
}); | ||
}); | ||
|
||
return pledge; | ||
} | ||
|
||
var baselineMinimum = Infinity, | ||
|
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