Skip to content

Commit 1f4caa8

Browse files
committed
feat(benchmark): add a simple benchmark for the di module
1 parent 035dc5b commit 1f4caa8

12 files changed

Lines changed: 298 additions & 28 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Don’t commit the following directories created by pub.
22
build/
3+
benchpress-build/
34
packages/
45
.buildlog
56
node_modules

gulpfile.js

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@ var fs = require('fs');
1515
var path = require('path');
1616
var readline = require('readline');
1717
var Q = require('q');
18+
var merge = require('merge');
19+
var benchpress = require('angular-benchpress/lib/cli');
1820

1921
var js2es5Options = {
2022
annotations: true, // parse annotations
2123
types: true, // parse types
2224
script: false, // parse as a module
23-
modules: 'register',
25+
modules: 'instantiate'
26+
};
27+
28+
var js2es5OptionsProd = merge(true, js2es5Options, {
29+
typeAssertions: false
30+
});
31+
32+
var js2es5OptionsDev = merge(true, js2es5Options, {
2433
typeAssertionModule: 'rtts_assert/rtts_assert',
2534
typeAssertions: true
26-
};
35+
});
2736

2837
var js2dartOptions = {
2938
annotations: true, // parse annotations
@@ -38,16 +47,18 @@ var gulpTraceur = require('./tools/transpiler/gulp-traceur');
3847
// traceur runtime
3948

4049
gulp.task('jsRuntime/build', function() {
41-
var traceurRuntime = gulp.src(gulpTraceur.RUNTIME_PATH)
42-
.pipe(gulp.dest('build/js'));
50+
var traceurRuntime = gulp.src([
51+
gulpTraceur.RUNTIME_PATH,
52+
"node_modules/es6-module-loader/dist/es6-module-loader-sans-promises.src.js",
53+
"node_modules/systemjs/lib/extension-register.js"
54+
]).pipe(gulp.dest('build/js'));
4355
return traceurRuntime;
4456
});
4557

4658
// -----------------------
4759
// modules
4860
var sourceTypeConfigs = {
4961
dart: {
50-
compilerOptions: js2dartOptions,
5162
transpileSrc: ['modules/**/*.js'],
5263
htmlSrc: ['modules/*/src/**/*.html'],
5364
copySrc: ['modules/**/*.dart'],
@@ -56,7 +67,6 @@ var sourceTypeConfigs = {
5667
mimeType: 'application/dart'
5768
},
5869
js: {
59-
compilerOptions: js2es5Options,
6070
transpileSrc: ['modules/**/*.js', 'modules/**/*.es6'],
6171
htmlSrc: ['modules/*/src/**/*.html'],
6272
copySrc: ['modules/**/*.es5'],
@@ -72,7 +82,7 @@ gulp.task('modules/clean', function() {
7282
});
7383

7484
gulp.task('modules/build.dart/src', function() {
75-
return createModuleTask(sourceTypeConfigs.dart);
85+
return createModuleTask(merge(sourceTypeConfigs.dart, {compilerOptions: js2dartOptions}));
7686
});
7787

7888
gulp.task('modules/build.dart/pubspec', function(done) {
@@ -105,14 +115,24 @@ gulp.task('modules/build.dart/pubspec', function(done) {
105115

106116
gulp.task('modules/build.dart', ['modules/build.dart/src', 'modules/build.dart/pubspec']);
107117

108-
gulp.task('modules/build.js', function() {
109-
return createModuleTask(sourceTypeConfigs.js);
118+
gulp.task('modules/build.dev.js', function() {
119+
return createModuleTask(merge(true, sourceTypeConfigs.js, {compilerOptions: js2es5OptionsDev}));
120+
});
121+
122+
gulp.task('modules/build.prod.js', function() {
123+
return createModuleTask(merge(true, sourceTypeConfigs.js, {compilerOptions: js2es5OptionsProd}));
110124
});
111125

112126
function renameSrcToLib(file) {
113127
file.dirname = file.dirname.replace(/\bsrc\b/, 'lib');
114128
}
115129

130+
function renameEs5ToJs(file) {
131+
if (file.extname == '.es5') {
132+
file.extname = '.js';
133+
}
134+
}
135+
116136
function createModuleTask(sourceTypeConfig) {
117137
var transpile = gulp.src(sourceTypeConfig.transpileSrc)
118138
.pipe(rename({extname: '.'+sourceTypeConfig.outputExt}))
@@ -121,6 +141,7 @@ function createModuleTask(sourceTypeConfig) {
121141
.pipe(gulp.dest(sourceTypeConfig.outputDir));
122142
var copy = gulp.src(sourceTypeConfig.copySrc)
123143
.pipe(rename(renameSrcToLib))
144+
.pipe(rename(renameEs5ToJs))
124145
.pipe(gulp.dest(sourceTypeConfig.outputDir));
125146
// TODO: provide the list of files to the template
126147
// automatically!
@@ -202,22 +223,50 @@ gulp.task('analyze/dartanalyzer', function(done) {
202223
});
203224

204225

226+
227+
// ------------------
228+
// BENCHMARKS
229+
230+
var benchmarksBuildPath = 'build/benchpress';
231+
var benchmarksCompiledJsPath = 'build/js/benchmarks/lib';
232+
233+
gulp.task('benchmarks/build.benchpress', function () {
234+
benchpress.build({
235+
benchmarksPath: benchmarksCompiledJsPath,
236+
buildPath: benchmarksBuildPath
237+
})
238+
});
239+
240+
gulp.task('benchmarks/build', function() {
241+
runSequence(
242+
['jsRuntime/build', 'modules/build.prod.js'],
243+
'benchmarks/build.benchpress'
244+
);
245+
});
246+
247+
248+
205249
// ------------------
206250
// WEB SERVER
207-
gulp.task('serve', connect.server({
208-
root: [__dirname+'/build'],
209-
port: 8000,
210-
livereload: false,
211-
open: false,
212-
middleware: function() {
213-
return [function(req, resp, next){
214-
if (req.url.match(/\.dart$/)) {
215-
resp.setHeader("Content-Type", "application/dart");
216-
}
217-
next();
218-
}];
219-
}
220-
}));
251+
252+
gulp.task('serve', function() {
253+
connect.server({
254+
root: [__dirname+'/build'],
255+
port: 8000,
256+
livereload: false,
257+
open: false,
258+
middleware: function() {
259+
return [function(req, resp, next){
260+
if (req.url.match(/\.dart$/)) {
261+
resp.setHeader("Content-Type", "application/dart");
262+
}
263+
next();
264+
}];
265+
}
266+
})();
267+
});
268+
269+
221270

222271
// --------------
223272
// general targets
@@ -227,7 +276,7 @@ gulp.task('clean', ['modules/clean']);
227276
gulp.task('build', function(done) {
228277
runSequence(
229278
// parallel
230-
['jsRuntime/build', 'modules/build.dart', 'modules/build.js'],
279+
['jsRuntime/build', 'modules/build.dart', 'modules/build.dev.js'],
231280
// sequential
232281
'analyze/dartanalyzer'
233282
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
System.import('benchmarks/di/injector_get_benchmark').then(function (bm) {
2+
window.benchmarkSteps.push({name: 'Injector.get (token)', fn: bm.run});
3+
}, console.log.bind(console));
4+
5+
System.import('benchmarks/di/injector_get_by_key_benchmark').then(function (bm) {
6+
window.benchmarkSteps.push({name: 'Injector.get (key)', fn: bm.run});
7+
}, console.log.bind(console));
8+
9+
System.import('benchmarks/di/injector_get_child_benchmark').then(function (bm) {
10+
window.benchmarkSteps.push({name: 'Injector.get (grand x 5 child)', fn: bm.run});
11+
}, console.log.bind(console));
12+
13+
System.import('benchmarks/di/injector_instantiate_benchmark').then(function (bm) {
14+
window.benchmarkSteps.push({name: 'Injector.instantiate', fn: bm.run});
15+
}, console.log.bind(console));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = function(config) {
2+
config.set({
3+
scripts: [
4+
{src: '/js/traceur-runtime.js'},
5+
{src: '/js/es6-module-loader-sans-promises.src.js'},
6+
{src: '/js/extension-register.js'},
7+
{src: 'register_system.js'},
8+
{src: 'benchmark.js'}
9+
]
10+
});
11+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Injector} from "di/di";
2+
3+
var count = 0;
4+
5+
export function run () {
6+
var bindings = [A, B, C, D, E];
7+
var injector = new Injector(bindings);
8+
9+
for (var i = 0; i < 20000; ++i) {
10+
injector.get(D);
11+
injector.get(E);
12+
}
13+
}
14+
15+
class A {
16+
constructor() {
17+
count++;
18+
}
19+
}
20+
21+
class B {
22+
constructor(a:A) {
23+
count++;
24+
}
25+
}
26+
27+
class C {
28+
constructor(b:B) {
29+
count++;
30+
}
31+
}
32+
33+
class D {
34+
constructor(c:C, b:B) {
35+
count++;
36+
}
37+
}
38+
39+
class E {
40+
constructor(d:D, c:C) {
41+
count++;
42+
}
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {Injector, Key} from "di/di";
2+
3+
var count = 0;
4+
5+
export function run () {
6+
var bindings = [A, B, C, D, E];
7+
var injector = new Injector(bindings);
8+
9+
var D_KEY = Key.get(D);
10+
var E_KEY = Key.get(E);
11+
12+
for (var i = 0; i < 20000; ++i) {
13+
injector.get(D_KEY);
14+
injector.get(E_KEY);
15+
}
16+
}
17+
18+
class A {
19+
constructor() {
20+
count++;
21+
}
22+
}
23+
24+
class B {
25+
constructor(a:A) {
26+
count++;
27+
}
28+
}
29+
30+
class C {
31+
constructor(b:B) {
32+
count++;
33+
}
34+
}
35+
36+
class D {
37+
constructor(c:C, b:B) {
38+
count++;
39+
}
40+
}
41+
42+
class E {
43+
constructor(d:D, c:C) {
44+
count++;
45+
}
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {Injector, Key} from "di/di";
2+
3+
var count = 0;
4+
5+
export function run () {
6+
var bindings = [A, B, C, D, E];
7+
var injector = new Injector(bindings);
8+
var childInjector = injector.
9+
createChild([]).
10+
createChild([]).
11+
createChild([]).
12+
createChild([]).
13+
createChild([]);
14+
15+
for (var i = 0; i < 20000; ++i) {
16+
childInjector.get(D);
17+
childInjector.get(E);
18+
}
19+
}
20+
21+
class A {
22+
constructor() {
23+
count++;
24+
}
25+
}
26+
27+
class B {
28+
constructor(a:A) {
29+
count++;
30+
}
31+
}
32+
33+
class C {
34+
constructor(b:B) {
35+
count++;
36+
}
37+
}
38+
39+
class D {
40+
constructor(c:C, b:B) {
41+
count++;
42+
}
43+
}
44+
45+
class E {
46+
constructor(d:D, c:C) {
47+
count++;
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {Injector, Key} from "di/di";
2+
3+
var count = 0;
4+
5+
export function run () {
6+
var bindings = [A, B, C, D];
7+
var injector = new Injector(bindings);
8+
9+
for (var i = 0; i < 1000; ++i) {
10+
var child = injector.createChild([E]);
11+
child.get(E);
12+
}
13+
14+
console.log(count)
15+
}
16+
17+
class A {
18+
constructor() {
19+
count++;
20+
}
21+
}
22+
23+
class B {
24+
constructor(a:A) {
25+
count++;
26+
}
27+
}
28+
29+
class C {
30+
constructor(b:B) {
31+
count++;
32+
}
33+
}
34+
35+
class D {
36+
constructor(c:C, b:B) {
37+
count++;
38+
}
39+
}
40+
41+
class E {
42+
constructor(d:D, c:C) {
43+
count++;
44+
}
45+
}

modules/benchmarks/src/di/main.html

Whitespace-only changes.

0 commit comments

Comments
 (0)