Skip to content

Commit e7fa0de

Browse files
committed
Cleanup benchmark
1 parent fe78b7c commit e7fa0de

File tree

2 files changed

+124
-114
lines changed

2 files changed

+124
-114
lines changed

benchmarks/run.js

Lines changed: 123 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,161 @@
11
/**
22
* Adaptive benchmarking. Starts with `initialTimes` iterations, increasing by
3-
* a power of two each time until the benchmark takes at least `minDuration_ms`
3+
* a power of two each time until the benchmark takes at least `minDurationMs`
44
* milliseconds to complete.
55
*/
66

7-
var Canvas = require('../lib/canvas')
8-
, canvas = new Canvas(200, 200)
9-
, largeCanvas = new Canvas(1000, 1000)
10-
, ctx = canvas.getContext('2d');
7+
var Canvas = require('../')
8+
var canvas = new Canvas(200, 200)
9+
var largeCanvas = new Canvas(1000, 1000)
10+
var ctx = canvas.getContext('2d')
1111

12-
var initialTimes = 10;
13-
var minDuration_ms = 2000;
12+
var initialTimes = 10
13+
var minDurationMs = 2000
1414

15-
var queue = [], running = false;
15+
var queue = []
16+
var running = false
1617

17-
function bm(label, fn) {
18-
queue.push({label: label, fn: fn});
19-
next();
18+
function bm (label, fn) {
19+
queue.push({ label: label, fn: fn })
20+
next()
2021
}
2122

22-
function next() {
23+
function next () {
2324
if (queue.length && !running) {
24-
run(queue.pop(), initialTimes, Date.now());
25+
run(queue.pop(), initialTimes, Date.now())
2526
}
2627
}
2728

28-
function run(benchmark, n, start) {
29-
running = true;
30-
var originalN = n;
31-
var fn = benchmark.fn;
29+
function run (benchmark, n, start) {
30+
running = true
31+
var originalN = n
32+
var fn = benchmark.fn
33+
3234
if (fn.length) { // async
33-
var pending = n;
34-
while (n--) fn(function () {
35-
--pending || done(benchmark, originalN, start, true);
36-
});
35+
var pending = n
36+
37+
while (n--) {
38+
fn(function () {
39+
--pending || done(benchmark, originalN, start, true)
40+
})
41+
}
3742
} else {
38-
while (n--) fn();
39-
done(benchmark, originalN, start);
43+
while (n--) fn()
44+
done(benchmark, originalN, start)
4045
}
4146
}
4247

43-
function done(benchmark, times, start, async) {
44-
var duration = Date.now() - start;
45-
if (duration < minDuration_ms) {
46-
run(benchmark, times * 2, Date.now());
48+
function done (benchmark, times, start, isAsync) {
49+
var duration = Date.now() - start
50+
51+
if (duration < minDurationMs) {
52+
run(benchmark, times * 2, Date.now())
4753
} else {
4854
var opsSec = times / duration * 1000
49-
if (async) {
50-
console.log(' - \x1b[33m%s\x1b[0m %s ops/sec (%s times, async)', benchmark.label, opsSec.toLocaleString(), times);
55+
if (isAsync) {
56+
console.log(' - \x1b[33m%s\x1b[0m %s ops/sec (%s times, async)', benchmark.label, opsSec.toLocaleString(), times)
5157
} else {
52-
console.log(' - \x1b[33m%s\x1b[0m %s ops/sec (%s times)', benchmark.label, opsSec.toLocaleString(), times);
58+
console.log(' - \x1b[33m%s\x1b[0m %s ops/sec (%s times)', benchmark.label, opsSec.toLocaleString(), times)
5359
}
54-
running = false;
55-
next();
60+
running = false
61+
next()
5662
}
5763
}
5864

5965
// node-canvas
6066

61-
bm('lineTo()', function(){
62-
ctx.lineTo(0, 50);
63-
});
67+
bm('lineTo()', function () {
68+
ctx.lineTo(0, 50)
69+
})
6470

65-
bm('arc()', function(){
66-
ctx.arc(75,75,50,0,Math.PI*2,true);
67-
});
71+
bm('arc()', function () {
72+
ctx.arc(75, 75, 50, 0, Math.PI * 2, true)
73+
})
6874

69-
bm('fillStyle= hex', function(){
70-
ctx.fillStyle = '#FFCCAA';
71-
});
75+
bm('fillStyle= hex', function () {
76+
ctx.fillStyle = '#FFCCAA'
77+
})
7278

73-
bm('fillStyle= rgba()', function(){
74-
ctx.fillStyle = 'rgba(0,255,80,1)';
75-
});
79+
bm('fillStyle= rgba()', function () {
80+
ctx.fillStyle = 'rgba(0,255,80,1)'
81+
})
7682

7783
// Apparently there's a bug in cairo by which the fillRect and strokeRect are
7884
// slow only after a ton of arcs have been drawn.
79-
bm('fillRect()', function(){
80-
ctx.fillRect(50, 50, 100, 100);
81-
});
82-
83-
bm('strokeRect()', function(){
84-
ctx.strokeRect(50, 50, 100, 100);
85-
});
86-
87-
bm('linear gradients', function(){
88-
var lingrad = ctx.createLinearGradient(0,50,0,95);
89-
lingrad.addColorStop(0.5, '#000');
90-
lingrad.addColorStop(1, 'rgba(0,0,0,0)');
91-
ctx.fillStyle = lingrad;
92-
ctx.fillRect(10,10,130,130);
93-
});
94-
95-
bm('toBuffer() 200x200', function(){
96-
canvas.toBuffer();
97-
});
98-
99-
bm('toBuffer() 1000x1000', function(){
100-
largeCanvas.toBuffer();
101-
});
102-
103-
bm('toBuffer() async 200x200', function(done){
85+
bm('fillRect()', function () {
86+
ctx.fillRect(50, 50, 100, 100)
87+
})
88+
89+
bm('strokeRect()', function () {
90+
ctx.strokeRect(50, 50, 100, 100)
91+
})
92+
93+
bm('linear gradients', function () {
94+
var lingrad = ctx.createLinearGradient(0, 50, 0, 95)
95+
lingrad.addColorStop(0.5, '#000')
96+
lingrad.addColorStop(1, 'rgba(0,0,0,0)')
97+
ctx.fillStyle = lingrad
98+
ctx.fillRect(10, 10, 130, 130)
99+
})
100+
101+
bm('toBuffer() 200x200', function () {
102+
canvas.toBuffer()
103+
})
104+
105+
bm('toBuffer() 1000x1000', function () {
106+
largeCanvas.toBuffer()
107+
})
108+
109+
bm('toBuffer() async 200x200', function (done) {
104110
canvas.toBuffer(function (err, buf) {
105-
done();
106-
});
107-
});
111+
if (err) throw err
112+
113+
done()
114+
})
115+
})
108116

109-
bm('toBuffer() async 1000x1000', function(done){
117+
bm('toBuffer() async 1000x1000', function (done) {
110118
largeCanvas.toBuffer(function (err, buf) {
111-
done();
112-
});
113-
});
114-
115-
bm('toBuffer().toString("base64") 200x200', function(){
116-
canvas.toBuffer().toString('base64');
117-
});
118-
119-
bm('toDataURL() 200x200', function(){
120-
canvas.toDataURL();
121-
});
122-
123-
bm('moveTo() / arc() / stroke()', function(){
124-
ctx.beginPath();
125-
ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle
126-
ctx.moveTo(110,75);
127-
ctx.arc(75,75,35,0,Math.PI,false); // Mouth
128-
ctx.moveTo(65,65);
129-
ctx.arc(60,65,5,0,Math.PI*2,true); // Left eye
130-
ctx.moveTo(95,65);
131-
ctx.arc(90,65,5,0,Math.PI*2,true); // Right eye
132-
ctx.stroke();
133-
});
134-
135-
bm('createImageData(300,300)', function(){
136-
ctx.createImageData(300,300);
137-
});
138-
139-
bm('getImageData(0,0,100,100)', function(){
140-
ctx.getImageData(0,0,100,100);
141-
});
142-
143-
bm('PNGStream 200x200', function(done){
144-
var stream = canvas.createSyncPNGStream();
145-
stream.on('data', function(chunk){
119+
if (err) throw err
120+
121+
done()
122+
})
123+
})
124+
125+
bm('toBuffer().toString("base64") 200x200', function () {
126+
canvas.toBuffer().toString('base64')
127+
})
128+
129+
bm('toDataURL() 200x200', function () {
130+
canvas.toDataURL()
131+
})
132+
133+
bm('moveTo() / arc() / stroke()', function () {
134+
ctx.beginPath()
135+
ctx.arc(75, 75, 50, 0, Math.PI * 2, true) // Outer circle
136+
ctx.moveTo(110, 75)
137+
ctx.arc(75, 75, 35, 0, Math.PI, false) // Mouth
138+
ctx.moveTo(65, 65)
139+
ctx.arc(60, 65, 5, 0, Math.PI * 2, true) // Left eye
140+
ctx.moveTo(95, 65)
141+
ctx.arc(90, 65, 5, 0, Math.PI * 2, true) // Right eye
142+
ctx.stroke()
143+
})
144+
145+
bm('createImageData(300,300)', function () {
146+
ctx.createImageData(300, 300)
147+
})
148+
149+
bm('getImageData(0,0,100,100)', function () {
150+
ctx.getImageData(0, 0, 100, 100)
151+
})
152+
153+
bm('PNGStream 200x200', function (done) {
154+
var stream = canvas.createSyncPNGStream()
155+
stream.on('data', function (chunk) {
146156
// whatever
147-
});
148-
stream.on('end', function(){
149-
done();
150-
});
151-
});
157+
})
158+
stream.on('end', function () {
159+
done()
160+
})
161+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"prebenchmark": "node-gyp build",
2525
"benchmark": "node benchmarks/run.js",
2626
"pretest": "node-gyp build",
27-
"test": "standard examples/*.js test/server.js test/public/*.js && mocha test/*.test.js",
27+
"test": "standard examples/*.js test/server.js test/public/*.js benchmark/run.js && mocha test/*.test.js",
2828
"pretest-server": "node-gyp build",
2929
"test-server": "node test/server.js"
3030
},

0 commit comments

Comments
 (0)