Skip to content

Commit 5b56a61

Browse files
committed
added benchmark
1 parent 44cfedd commit 5b56a61

3 files changed

Lines changed: 189 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/node_modules
22
/test/js
33
/test/browsertest/js
4+
/benchmark/js
5+
/benchmark/fixtures
46
/examples/*/js
57
/coverage
6-
.DS_Store
8+
.DS_Store

benchmark/benchmark.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
var path = require("path");
2+
var fs = require("fs");
3+
var Benchmark = require("benchmark");
4+
var webpack = require("../");
5+
var fixtures = path.join(__dirname, "fixtures");
6+
var outputPath = path.join(__dirname, "js");
7+
8+
var benchmarkOptions = {
9+
defer: true,
10+
onCycle: function() {
11+
process.stderr.write(".");
12+
},
13+
minSamples: 10
14+
};
15+
16+
function runTimes(compiler, times, deferred) {
17+
fs.writeFileSync(path.join(fixtures, "0.js"), "module.exports = " + Math.random(), "utf-8");
18+
compiler.run(function(err, stats) {
19+
if(err) throw err;
20+
if(times === 1)
21+
deferred.resolve();
22+
else
23+
runTimes(compiler, times - 1, deferred);
24+
});
25+
}
26+
27+
var tests = {
28+
"normal build": [[0, 1, 5, 10, 50, 100, 200], function(size, deferred) {
29+
webpack({
30+
context: fixtures,
31+
entry: "./" + size + ".js",
32+
output: {
33+
path: outputPath,
34+
filename: "bundle.js"
35+
}
36+
}, function(err, stats) {
37+
if(err) throw err;
38+
deferred.resolve();
39+
});
40+
}],
41+
"sourcemap build": [[0, 1, 2, 5, 10, 15], function(size, deferred) {
42+
webpack({
43+
context: fixtures,
44+
entry: "./" + size + ".big.js",
45+
output: {
46+
path: outputPath,
47+
filename: "bundle.js"
48+
},
49+
devtool: "source-map"
50+
}, function(err, stats) {
51+
if(err) throw err;
52+
deferred.resolve();
53+
})
54+
}],
55+
"build w/ chunks": [[0, 1, 5, 10, 50, 100, 200], function(size, deferred) {
56+
webpack({
57+
context: fixtures,
58+
entry: "./" + size + ".async.js",
59+
output: {
60+
path: outputPath,
61+
filename: "bundle.js"
62+
}
63+
}, function(err, stats) {
64+
if(err) throw err;
65+
deferred.resolve();
66+
})
67+
}],
68+
"incremental": [[0, 1, 5, 10, 50, 100, 200], function(size, deferred) {
69+
var compiler = webpack({
70+
cache: true,
71+
context: fixtures,
72+
entry: "./" + size + ".js",
73+
output: {
74+
path: outputPath,
75+
filename: "bundle.js"
76+
}
77+
});
78+
runTimes(compiler, 2, deferred);
79+
}],
80+
"incremental2": [[0, 1, 5, 10, 50, 100, 200], function(size, deferred) {
81+
var compiler = webpack({
82+
cache: true,
83+
context: fixtures,
84+
entry: "./" + size + ".js",
85+
output: {
86+
path: outputPath,
87+
filename: "bundle.js"
88+
}
89+
});
90+
runTimes(compiler, 3, deferred);
91+
}],
92+
"incremental4": [[0, 1, 5, 10, 50, 100, 200], function(size, deferred) {
93+
var compiler = webpack({
94+
cache: true,
95+
context: fixtures,
96+
entry: "./" + size + ".js",
97+
output: {
98+
path: outputPath,
99+
filename: "bundle.js"
100+
}
101+
});
102+
runTimes(compiler, 5, deferred);
103+
}],
104+
"incremental16": [[0, 1, 5, 10, 50, 100, 200], function(size, deferred) {
105+
var compiler = webpack({
106+
cache: true,
107+
context: fixtures,
108+
entry: "./" + size + ".js",
109+
output: {
110+
path: outputPath,
111+
filename: "bundle.js"
112+
}
113+
});
114+
runTimes(compiler, 17, deferred);
115+
}],
116+
};
117+
118+
var suite = new Benchmark.Suite;
119+
120+
Object.keys(tests).forEach(function(name) {
121+
var test = tests[name];
122+
test[0].forEach(function(size) {
123+
suite.add(name + " " + size, function(deferred) {
124+
test[1](size, deferred);
125+
}, benchmarkOptions);
126+
});
127+
});
128+
129+
suite.on("cycle", function(event) {
130+
process.stderr.write("\n");
131+
var b = event.target;
132+
console.log(b.name + "\t" + Math.floor(1000 * (b.stats.mean - b.stats.moe)) + "\t" + Math.floor(1000 * (b.stats.mean + b.stats.moe)));
133+
});
134+
135+
suite.run({ async: true });

benchmark/createFixtures.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var path = require("path");
2+
var fs = require("fs");
3+
4+
var fixtures = path.join(__dirname, "fixtures");
5+
6+
7+
for(var i = 0; i < 1000; i++) {
8+
var source = [];
9+
if(i > 8)
10+
source.push("require("+ JSON.stringify("./" + (i / 8 | 0) + ".js") + ");");
11+
if(i > 4)
12+
source.push("require("+ JSON.stringify("./" + (i / 4 | 0) + ".js") + ");");
13+
if(i > 2)
14+
source.push("require("+ JSON.stringify("./" + (i / 2 | 0) + ".js") + ");");
15+
if(i > 0)
16+
source.push("require("+ JSON.stringify("./" + (i - 1) + ".js") + ");");
17+
source.push("module.exports = " + i + ";");
18+
fs.writeFileSync(path.join(fixtures, i + ".js"), source.join("\n"), "utf-8");
19+
}
20+
21+
for(var i = 0; i < 1000; i++) {
22+
var source = [];
23+
source.push("require.ensure([], function(require) {");
24+
if(i > 8)
25+
source.push("require("+ JSON.stringify("./" + (i / 8 | 0) + ".async.js") + ");");
26+
if(i > 4)
27+
source.push("require("+ JSON.stringify("./" + (i / 4 | 0) + ".async.js") + ");");
28+
if(i > 2)
29+
source.push("require("+ JSON.stringify("./" + (i / 2 | 0) + ".async.js") + ");");
30+
if(i > 0)
31+
source.push("require("+ JSON.stringify("./" + (i - 1) + ".async.js") + ");");
32+
source.push("});");
33+
source.push("module.exports = " + i + ";");
34+
fs.writeFileSync(path.join(fixtures, i + ".async.js"), source.join("\n"), "utf-8");
35+
}
36+
37+
for(var i = 0; i < 100; i++) {
38+
var source = [];
39+
if(i > 8)
40+
source.push("require("+ JSON.stringify("./" + (i / 8 | 0) + ".big.js") + ");");
41+
if(i > 4)
42+
source.push("require("+ JSON.stringify("./" + (i / 4 | 0) + ".big.js") + ");");
43+
if(i > 2)
44+
source.push("require("+ JSON.stringify("./" + (i / 2 | 0) + ".big.js") + ");");
45+
if(i > 0)
46+
source.push("require("+ JSON.stringify("./" + (i - 1) + ".big.js") + ");");
47+
for(var j = 0; j < 300; j++)
48+
source.push("if(Math.random())hello.world();test.a.b.c.d();x(1,2,3,4);var a,b,c,d,e,f;");
49+
source.push("module.exports = " + i + ";");
50+
fs.writeFileSync(path.join(fixtures, i + ".big.js"), source.join("\n"), "utf-8");
51+
}

0 commit comments

Comments
 (0)