Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Make tests unique each time they are compiled.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Aug 21, 2013
1 parent 475fb6d commit 1c579e8
Showing 1 changed file with 71 additions and 71 deletions.
142 changes: 71 additions & 71 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -1569,69 +1569,17 @@
// lazy define for hi-res timers
clock = function(clone) {
var deferred;
templateData.uid = uid + uidCounter++;

if (clone instanceof Deferred) {
deferred = clone;
clone = deferred.benchmark;
}
var bench = clone._original,
fn = bench.fn,
fnArg = deferred ? getFirstArgument(fn) || 'deferred' : '',
stringable = isStringable(fn);

_.extend(templateData, {
'setup': getSource(bench.setup, interpolate('m#.setup()')),
'fn': getSource(fn, interpolate('m#.fn(' + fnArg + ')')),
'fnArg': fnArg,
'teardown': getSource(bench.teardown, interpolate('m#.teardown()'))
});

// use API of chosen timer
if (timer.unit == 'ns') {
if (timer.ns.nanoTime) {
_.extend(templateData, {
'begin': interpolate('s#=n#.nanoTime()'),
'end': interpolate('r#=(n#.nanoTime()-s#)/1e9')
});
} else {
_.extend(templateData, {
'begin': interpolate('s#=n#()'),
'end': interpolate('r#=n#(s#);r#=r#[0]+(r#[1]/1e9)')
});
}
}
else if (timer.unit == 'us') {
if (timer.ns.stop) {
_.extend(templateData, {
'begin': interpolate('s#=n#.start()'),
'end': interpolate('r#=n#.microseconds()/1e6')
});
} else if (perfName) {
_.extend(templateData, {
'begin': interpolate('s#=n#.' + perfName + '()'),
'end': interpolate('r#=(n#.' + perfName + '()-s#)/1e3')
});
} else {
_.extend(templateData, {
'begin': interpolate('s#=n#()'),
'end': interpolate('r#=(n#()-s#)/1e6')
});
}
}
else {
_.extend(templateData, {
'begin': interpolate('s#=new n#'),
'end': interpolate('r#=(new n#-s#)/1e3')
});
}

var count = bench.count = clone.count,
stringable = isStringable(bench.fn),
count = bench.count = clone.count,
decompilable = support.decompilation || stringable,
id = bench.id,
isEmpty = !(templateData.fn || stringable),
name = bench.name || (typeof id == 'number' ? '<Test #' + id + '>' : id),
ns = timer.ns,
result = 0;

// init `minTime` if needed
Expand All @@ -1641,22 +1589,12 @@
// (some Chrome builds erase the `ns` variable after millions of executions)
if (applet) {
try {
ns.nanoTime();
timer.ns.nanoTime();
} catch(e) {
// use non-element to avoid issues with libs that augment them
ns = timer.ns = new applet.Packages.nano;
timer.ns = new applet.Packages.nano;
}
}
// define `timer` methods
timer.start = createFunction(
interpolate('o#'),
interpolate('var n#=this.ns,${begin};o#.elapsed=0;o#.timeStamp=s#')
);

timer.stop = createFunction(
interpolate('o#'),
interpolate('var n#=this.ns,s#=o#.timeStamp,${end};o#.elapsed=r#')
);

// Compile in setup/teardown functions and the test loop.
// Create a new compiled test, instead of using the cached `bench.compiled`,
Expand All @@ -1679,7 +1617,8 @@
: 'var r#,s#,m#=this,f#=m#.fn,i#=m#.count,n#=t#.ns;${setup}\n${begin};' +
'while(i#--){${fn}\n}${end};${teardown}\nreturn{elapsed:r#,uid:"${uid}"}';

var compiled = bench.compiled = clone.compiled = createCompiled(funcBody);
var compiled = bench.compiled = clone.compiled = createCompiled(bench, deferred, funcBody),
isEmpty = !(templateData.fn || stringable);

try {
if (isEmpty) {
Expand Down Expand Up @@ -1709,7 +1648,7 @@
',n#=t#.ns;${setup}\n${begin};m#.f#=f#;while(i#--){m#.f#()}${end};' +
'delete m#.f#;${teardown}\nreturn{elapsed:r#}';

compiled = createCompiled(funcBody);
compiled = createCompiled(bench, deferred, funcBody);

try {
// pretest one more time to check for errors
Expand All @@ -1725,10 +1664,9 @@
}
}
}

// if no errors run the full test loop
if (!clone.error) {
compiled = bench.compiled = clone.compiled = createCompiled(funcBody);
compiled = bench.compiled = clone.compiled = createCompiled(bench, deferred, funcBody);
result = compiled.call(deferred || bench, context, timer).elapsed;
}
return result;
Expand All @@ -1739,7 +1677,69 @@
/**
* Creates a compiled function from the given function `body`.
*/
function createCompiled(body) {
function createCompiled(bench, deferred, body) {
var fn = bench.fn,
fnArg = deferred ? getFirstArgument(fn) || 'deferred' : '';

templateData.uid = uid + uidCounter++;

_.extend(templateData, {
'setup': getSource(bench.setup, interpolate('m#.setup()')),
'fn': getSource(fn, interpolate('m#.fn(' + fnArg + ')')),
'fnArg': fnArg,
'teardown': getSource(bench.teardown, interpolate('m#.teardown()'))
});

// use API of chosen timer
if (timer.unit == 'ns') {
if (timer.ns.nanoTime) {
_.extend(templateData, {
'begin': interpolate('s#=n#.nanoTime()'),
'end': interpolate('r#=(n#.nanoTime()-s#)/1e9')
});
} else {
_.extend(templateData, {
'begin': interpolate('s#=n#()'),
'end': interpolate('r#=n#(s#);r#=r#[0]+(r#[1]/1e9)')
});
}
}
else if (timer.unit == 'us') {
if (timer.ns.stop) {
_.extend(templateData, {
'begin': interpolate('s#=n#.start()'),
'end': interpolate('r#=n#.microseconds()/1e6')
});
} else if (perfName) {
_.extend(templateData, {
'begin': interpolate('s#=n#.' + perfName + '()'),
'end': interpolate('r#=(n#.' + perfName + '()-s#)/1e3')
});
} else {
_.extend(templateData, {
'begin': interpolate('s#=n#()'),
'end': interpolate('r#=(n#()-s#)/1e6')
});
}
}
else {
_.extend(templateData, {
'begin': interpolate('s#=new n#'),
'end': interpolate('r#=(new n#-s#)/1e3')
});
}
// define `timer` methods
timer.start = createFunction(
interpolate('o#'),
interpolate('var n#=this.ns,${begin};o#.elapsed=0;o#.timeStamp=s#')
);

timer.stop = createFunction(
interpolate('o#'),
interpolate('var n#=this.ns,s#=o#.timeStamp,${end};o#.elapsed=r#')
);

// create compiled test
return createFunction(
interpolate('window,t#'),
'var global = window, clearTimeout = global.clearTimeout, setTimeout = global.setTimeout;\n' +
Expand Down

0 comments on commit 1c579e8

Please sign in to comment.