|
1 | 1 | /** |
2 | 2 | * 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` |
4 | 4 | * milliseconds to complete. |
5 | 5 | */ |
6 | 6 |
|
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') |
11 | 11 |
|
12 | | -var initialTimes = 10; |
13 | | -var minDuration_ms = 2000; |
| 12 | +var initialTimes = 10 |
| 13 | +var minDurationMs = 2000 |
14 | 14 |
|
15 | | -var queue = [], running = false; |
| 15 | +var queue = [] |
| 16 | +var running = false |
16 | 17 |
|
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() |
20 | 21 | } |
21 | 22 |
|
22 | | -function next() { |
| 23 | +function next () { |
23 | 24 | if (queue.length && !running) { |
24 | | - run(queue.pop(), initialTimes, Date.now()); |
| 25 | + run(queue.pop(), initialTimes, Date.now()) |
25 | 26 | } |
26 | 27 | } |
27 | 28 |
|
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 | + |
32 | 34 | 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 | + } |
37 | 42 | } else { |
38 | | - while (n--) fn(); |
39 | | - done(benchmark, originalN, start); |
| 43 | + while (n--) fn() |
| 44 | + done(benchmark, originalN, start) |
40 | 45 | } |
41 | 46 | } |
42 | 47 |
|
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()) |
47 | 53 | } else { |
48 | 54 | 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) |
51 | 57 | } 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) |
53 | 59 | } |
54 | | - running = false; |
55 | | - next(); |
| 60 | + running = false |
| 61 | + next() |
56 | 62 | } |
57 | 63 | } |
58 | 64 |
|
59 | 65 | // node-canvas |
60 | 66 |
|
61 | | -bm('lineTo()', function(){ |
62 | | - ctx.lineTo(0, 50); |
63 | | -}); |
| 67 | +bm('lineTo()', function () { |
| 68 | + ctx.lineTo(0, 50) |
| 69 | +}) |
64 | 70 |
|
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 | +}) |
68 | 74 |
|
69 | | -bm('fillStyle= hex', function(){ |
70 | | - ctx.fillStyle = '#FFCCAA'; |
71 | | -}); |
| 75 | +bm('fillStyle= hex', function () { |
| 76 | + ctx.fillStyle = '#FFCCAA' |
| 77 | +}) |
72 | 78 |
|
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 | +}) |
76 | 82 |
|
77 | 83 | // Apparently there's a bug in cairo by which the fillRect and strokeRect are |
78 | 84 | // 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) { |
104 | 110 | canvas.toBuffer(function (err, buf) { |
105 | | - done(); |
106 | | - }); |
107 | | -}); |
| 111 | + if (err) throw err |
| 112 | + |
| 113 | + done() |
| 114 | + }) |
| 115 | +}) |
108 | 116 |
|
109 | | -bm('toBuffer() async 1000x1000', function(done){ |
| 117 | +bm('toBuffer() async 1000x1000', function (done) { |
110 | 118 | 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) { |
146 | 156 | // whatever |
147 | | - }); |
148 | | - stream.on('end', function(){ |
149 | | - done(); |
150 | | - }); |
151 | | -}); |
| 157 | + }) |
| 158 | + stream.on('end', function () { |
| 159 | + done() |
| 160 | + }) |
| 161 | +}) |
0 commit comments