-
Notifications
You must be signed in to change notification settings - Fork 10
/
benchmark.stories.tsx
316 lines (265 loc) · 12.1 KB
/
benchmark.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* eslint-disable sonarjs/no-duplicate-string */
import React from "react";
import canvasTxt from "canvas-txt";
import canvasMultilineText from "canvas-multiline-text";
import { split, clearCache } from "../index";
import Breaker from "linebreak";
import "./main.css";
// More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
export default {
title: "Benchmark",
};
function makeString(input: number, length: number): string {
const r = `${input}: Now, this is a story all about how My life got flipped-turned upside down And I'd like to take a minute Just sit right there I'll tell you how I became the prince of a town called Bel-Air In West Philadelphia born and raised On the playground was where I spent most of my days Chillin' out, maxin', relaxin', all cool And all shootin' some b-ball outside of the school When a couple of guys who were up to no good Started making trouble in my neighborhood I got in one little fight and my mom got scared She said, "You're movin' with your auntie and uncle in Bel-Air" I begged and pleaded with her day after day But she packed my suitcase and sent me on my way She gave me a kiss and then she gave me my ticket I put my Walkman on and said, "I might as well kick it" First class, yo this is bad Drinking orange juice out of a champagne glass Is this what the people of Bel-Air living like? Hmm, this might be alright But wait, I hear they're prissy, bourgeois, all that Is this the type of place that they just send this cool cat? I don't think so I'll see when I get there I hope they're prepared for the prince of Bel-Air Well, the plane landed and when I came out There was a dude who looked like a cop standing there with my name out I ain't trying to get arrested yet, I just got here I sprang with the quickness like lightning, disappeared I whistled for a cab and when it came near The license plate said, "Fresh" and it had dice in the mirror If anything I could say that this cab was rare But I thought "Nah, forget it, yo, holmes to Bel Air" I pulled up to the house about seven or eight And I yelled to the cabbie, "Yo holmes, smell ya later" I looked at my kingdom I was finally there To sit on my throne as the prince of Bel-Air`;
return r.slice(0, Math.max(0, length));
}
const correctnessString =
"This string should have a natural wrap due to this s-entence being a bit too long. Now we will do some new lines.\n\n- First\n- Second\n\nAndnowwewillseehowwell-thishandleswhenthereareno-spacesandthealgorithm-mustfigureoutwhattodo.\n\n政鮮山案表三搬手神一公輩越船断。歳五京政子球納給亡世人陳邸真面観。合内勢美一舞検科続最治持紙。全巡民学働毎満緊用学勢都味補造枝換先。新任海三格季隣抗権住応化止部属話。中党致窓未止用質会図期現。原者減過玉歴試特講向谷浩野案載文東。寿引害近銅大出怒水病噴績座番摂渡。率当来注情場問引社愛配成少読。力機道経動郎遂際検覧前認方";
const iterations = 5000;
export const Benchmark = () => {
const txtRef = React.useRef<HTMLCanvasElement>(null);
const hyperRef = React.useRef<HTMLCanvasElement>(null);
const multiRef = React.useRef<HTMLCanvasElement>(null);
const [txtResult, setTxtResult] = React.useState<number>();
const [hyperResult, setHyperResult] = React.useState<number>();
const [multiResult, setMultiResult] = React.useState<number>();
const [allowHyper, setAllowHyper] = React.useState(false);
const [length, setLength] = React.useState(400);
const onBenchmark = React.useCallback(async () => {
setTxtResult(undefined);
setHyperResult(undefined);
setMultiResult(undefined);
const txtCanvas = txtRef.current;
const hyperCanvas = hyperRef.current;
const multiCanvas = multiRef.current;
if (txtCanvas === null || hyperCanvas === null || multiCanvas === null) return;
const txtCtx = txtCanvas.getContext("2d", {
alpha: false,
});
const hyperCtx = hyperCanvas.getContext("2d", {
alpha: false,
});
const multiCtx = multiCanvas.getContext("2d", {
alpha: false,
});
if (txtCtx === null || hyperCtx === null || multiCtx === null) return;
let total = 0;
canvasTxt.font = "sans-serif";
canvasTxt.fontSize = 20;
canvasTxt.align = "center";
canvasTxt.vAlign = "top";
txtCtx.fillStyle = "#eee";
txtCtx.fillRect(0, 0, 500, 500);
hyperCtx.fillStyle = "#eee";
hyperCtx.fillRect(0, 0, 500, 500);
multiCtx.fillStyle = "#eee";
multiCtx.fillRect(0, 0, 500, 500);
for (let i = 0; i < iterations; i++) {
txtCtx.fillStyle = "#eee";
txtCtx.fillRect(0, 0, 500, 500);
txtCtx.fillStyle = "black";
const start = performance.now();
canvasTxt.drawText(txtCtx, makeString(i, length), 25, 25, 450, 475);
const end = performance.now();
total += end - start;
// just to make sure things update
if (i % 100 === 0) {
await new Promise(r => window.requestAnimationFrame(r));
}
}
setTxtResult(total);
total = 0;
multiCtx.textAlign = "center";
for (let i = 0; i < iterations; i++) {
multiCtx.fillStyle = "#eee";
multiCtx.fillRect(0, 0, 500, 500);
multiCtx.fillStyle = "black";
const start = performance.now();
canvasMultilineText(multiCtx, makeString(i, length), {
font: "sans-serif",
lineHeight: 1,
minFontSize: 20,
maxFontSize: 20,
rect: {
x: 25 + 450 / 2,
y: 25,
width: 450,
height: 475,
},
});
const end = performance.now();
total += end - start;
// just to make sure things update
if (i % 100 === 0) {
await new Promise(r => window.requestAnimationFrame(r));
}
}
setMultiResult(total);
// keep things fair
clearCache();
total = 0;
hyperCtx.font = "20px sans-serif";
hyperCtx.textBaseline = "top";
hyperCtx.textAlign = "center";
for (let i = 0; i < iterations; i++) {
hyperCtx.fillStyle = "#eee";
hyperCtx.fillRect(0, 0, 500, 500);
hyperCtx.fillStyle = "black";
const start = performance.now();
const lines = split(hyperCtx, makeString(i, length), "marker", 450, allowHyper);
let y = 25;
for (const l of lines) {
hyperCtx.fillText(l, 250, y);
y += 20;
if (y > 500) break;
}
const end = performance.now();
total += end - start;
// just to make sure things update
if (i % 100 === 0) {
await new Promise(r => window.requestAnimationFrame(r));
}
}
setHyperResult(total);
}, [allowHyper, length]);
return (
<div className="stack">
<div className="bar">
<button onClick={onBenchmark}>Benchmark</button>
<span>Enable HyperWrapping: </span>
<input type={"checkbox"} checked={allowHyper} onChange={e => setAllowHyper(e.currentTarget.checked)} />
</div>
Length:{" "}
<input
min={20}
max={2000}
step={20}
type={"range"}
value={length}
onChange={e => setLength(e.currentTarget.valueAsNumber)}
/>
{" " + length}
<div className="benchmark-container">
<div>
Canvas-Txt
<canvas width={500} height={500} ref={txtRef} />
</div>
<div>
canvas-multiline-text
<canvas width={500} height={500} ref={multiRef} />
</div>
<div>
Canvas-HyperTxt
<canvas width={500} height={500} ref={hyperRef} />
</div>
</div>
{txtResult !== undefined && (
<div>
canvas-txt time: <b>{Math.round(txtResult / 10) / 100}s</b>
</div>
)}
{multiResult !== undefined && (
<div>
canvas-multiline-text time: <b>{Math.round(multiResult / 10) / 100}s</b>
</div>
)}
{hyperResult !== undefined && (
<div>
canvas-hypertxt time: <b>{Math.round(hyperResult / 10) / 100}s</b>
</div>
)}
</div>
);
};
export const Correctness = () => {
const txtRef = React.useRef<HTMLCanvasElement>(null);
const hyperRef = React.useRef<HTMLCanvasElement>(null);
const multiRef = React.useRef<HTMLCanvasElement>(null);
const onDraw = React.useCallback(async () => {
const txtCanvas = txtRef.current;
const hyperCanvas = hyperRef.current;
const multiCanvas = multiRef.current;
if (txtCanvas === null || hyperCanvas === null || multiCanvas === null) return;
const txtCtx = txtCanvas.getContext("2d", {
alpha: false,
});
const hyperCtx = hyperCanvas.getContext("2d", {
alpha: false,
});
const multiCtx = multiCanvas.getContext("2d", {
alpha: false,
});
if (txtCtx === null || hyperCtx === null || multiCtx === null) return;
canvasTxt.font = "sans-serif";
canvasTxt.fontSize = 20;
canvasTxt.align = "center";
canvasTxt.vAlign = "top";
txtCtx.fillStyle = "#eee";
txtCtx.fillRect(0, 0, 500, 500);
hyperCtx.fillStyle = "#eee";
hyperCtx.fillRect(0, 0, 500, 500);
multiCtx.fillStyle = "#eee";
multiCtx.fillRect(0, 0, 500, 500);
txtCtx.fillStyle = "#eee";
txtCtx.fillRect(0, 0, 500, 500);
txtCtx.fillStyle = "black";
canvasTxt.drawText(txtCtx, correctnessString, 25, 25, 450, 475);
multiCtx.textAlign = "center";
multiCtx.fillStyle = "#eee";
multiCtx.fillRect(0, 0, 500, 500);
multiCtx.fillStyle = "black";
canvasMultilineText(multiCtx, correctnessString, {
font: "sans-serif",
lineHeight: 1,
minFontSize: 20,
maxFontSize: 20,
rect: {
x: 25 + 450 / 2,
y: 25,
width: 450,
height: 475,
},
});
hyperCtx.font = "20px sans-serif";
hyperCtx.textBaseline = "top";
hyperCtx.textAlign = "center";
hyperCtx.fillStyle = "#eee";
hyperCtx.fillRect(0, 0, 500, 500);
hyperCtx.fillStyle = "black";
const lines = split(hyperCtx, correctnessString, "marker", 450, false, s => {
const r: number[] = [];
const b = new Breaker(s);
let br = b.nextBreak();
while (br !== null) {
r.push(br.position);
br = b.nextBreak();
}
return r;
});
let y = 25;
for (const l of lines) {
hyperCtx.fillText(l, 250, y);
y += 20;
if (y > 500) break;
}
}, []);
React.useEffect(() => void onDraw(), [onDraw]);
return (
<div className="stack">
<div className="benchmark-container">
<div>
Canvas-Txt
<canvas width={500} height={500} ref={txtRef} />
</div>
<div>
canvas-multiline-text
<canvas width={500} height={500} ref={multiRef} />
</div>
<div>
Canvas-HyperTxt
<canvas width={500} height={500} ref={hyperRef} />
</div>
</div>
</div>
);
};