Skip to content

Commit

Permalink
some benchmarks
Browse files Browse the repository at this point in the history
* by default, run in fresh processes
* optional run in same process
  • Loading branch information
stefanpenner committed Apr 17, 2016
1 parent 7fac9d0 commit 2860a50
Show file tree
Hide file tree
Showing 10 changed files with 662 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bower_components
node_modules
isolate-0x*
24 changes: 24 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Running benchmarks

note: these benchmarks run in seperate processes, the idea is to similate
initial load + use, not after the JIT has settled

```sh
env NODE_ENV=production node runner.js ./benchmarks/scenarios/<name of scenario>
```

If you want to run in a single process, to easily debug or run a profiler the
`run-once.js` should be considered

```sh
env NODE_ENV=production node run-once.js ./benchmarks/scenarios/<name of scenario>
```

Running with the profiler:

```sh
env NODE_ENV=production node --prof run-once.js ./benchmarks/scenarios/<name of scenario>

# to view the output:
node --prof-process isolate-0x<name of file>
```
46 changes: 46 additions & 0 deletions benchmarks/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var ps = require('child_process');
var series = require('promise-map-series');
var ora = require('ora');
var tests = [];
var ara = require('ara');
var path = require('path');
var file = process.argv[2];

for (var i = 0; i < 50; i++) {
tests.push({
id: i,
run: function() {
var worker = new ara.EvalWorker();

return worker.run(function(file) {
return require(file)();
}, path.resolve(file)).finally(function() {
worker.terminate();
});
}});
}

var spinner = ora('running...');

console.log('Benchmark');
console.log(' file: ' + file);

spinner.start();

series(tests, function(test) {
spinner.text = 'running... (' + test.id+ '/' + tests.length + ')';

return test.run();
}).then(function(results) {
spinner.stop();

var total = results.reduce(function(sum, result) {
return sum + result.time;
}, 0);

console.log(' total: ', total + 'ms')
console.log(' per op: ', total / results.length + 'ms');
}).catch(function(reason) {
console.error(reason);
process.exit(1);
});
19 changes: 19 additions & 0 deletions benchmarks/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports.measure = function(cb) {
var start;

return new Promise(function(resolve) {
start = Date.now();
var result = cb();
if (typeof result === 'object') {
resolve(result);
} else {
resolve({
time: Date.now() - start
});
}
}).then(function() {
return {
time: Date.now() - start
};
});
};
9 changes: 9 additions & 0 deletions benchmarks/run-once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var file = process.argv[2];
var path = require('path');

require(path.resolve(process.argv[2]))().then(function(result) {
console.log(result);
}).catch(function(reason) {
console.log(reason);
process.exit(1);
});
46 changes: 46 additions & 0 deletions benchmarks/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var ps = require('child_process');
var series = require('promise-map-series');
var ora = require('ora');
var tests = [];
var ara = require('ara');
var path = require('path');
var file = process.argv[2];

for (var i = 0; i < 50; i++) {
tests.push({
id: i,
run: function() {
var worker = new ara.EvalWorker();

return worker.run(function(file) {
return require(file)();
}, path.resolve(file)).finally(function() {
worker.terminate();
});
}});
}

var spinner = ora('running...');

console.log('Benchmark');
console.log(' file: ' + file);

spinner.start();

series(tests, function(test) {
spinner.text = 'running... (' + test.id+ '/' + tests.length + ')';

return test.run();
}).then(function(results) {
spinner.stop();

var total = results.reduce(function(sum, result) {
return sum + result.time;
}, 0);

console.log(' total: ', total + 'ms')
console.log(' per op: ', total / results.length + 'ms');
}).catch(function(reason) {
console.error(reason);
process.exit(1);
});
13 changes: 13 additions & 0 deletions benchmarks/scenarios/long-chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var loader = require('../../lib/loader/loader.js');
var measure = require('../handler').measure;

module.exports = function() {
return measure(function() {
loader.define('foo' + -1, function() {});
for (var i = 0; i < 1000; i++) {
loader.define('foo' + i, ['foo' + (i - 1)], function() {});
}

loader.require('foo' + (i - 1));
});
};
499 changes: 499 additions & 0 deletions benchmarks/scenarios/other.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/loader/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,7 @@ var loader, define, requireModule, require, requirejs;

requirejs.clear();

if (typeof module !== 'undefined') {
module.exports = { require: require, define: define };
}
})(this);
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
},
"dependencies": {},
"devDependencies": {
"ora": "^0.2.1",
"promise-map-series": "^0.2.2",
"qunitjs": "^1.20.0",
"testem": "^1.0.0"
},
Expand Down

0 comments on commit 2860a50

Please sign in to comment.