Skip to content

Commit a64fa26

Browse files
split test-invoker (#451)
1 parent e2afcb6 commit a64fa26

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

resources/benchmark-runner.mjs

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Metric } from "./metric.mjs";
22
import { params } from "./params.mjs";
3+
import { TEST_INVOKER_LOOKUP } from "./test-invoker.mjs";
34

45
const performance = globalThis.performance;
56

@@ -266,61 +267,6 @@ const WarmupSuite = {
266267
],
267268
};
268269

269-
class TestInvoker {
270-
constructor(syncCallback, asyncCallback, reportCallback) {
271-
this._syncCallback = syncCallback;
272-
this._asyncCallback = asyncCallback;
273-
this._reportCallback = reportCallback;
274-
}
275-
}
276-
277-
class TimerTestInvoker extends TestInvoker {
278-
start() {
279-
return new Promise((resolve) => {
280-
setTimeout(() => {
281-
this._syncCallback();
282-
setTimeout(() => {
283-
this._asyncCallback();
284-
requestAnimationFrame(async () => {
285-
await this._reportCallback();
286-
resolve();
287-
});
288-
}, 0);
289-
}, params.waitBeforeSync);
290-
});
291-
}
292-
}
293-
294-
class RAFTestInvoker extends TestInvoker {
295-
start() {
296-
return new Promise((resolve) => {
297-
if (params.waitBeforeSync)
298-
setTimeout(() => this._scheduleCallbacks(resolve), params.waitBeforeSync);
299-
else
300-
this._scheduleCallbacks(resolve);
301-
});
302-
}
303-
304-
_scheduleCallbacks(resolve) {
305-
requestAnimationFrame(() => this._syncCallback());
306-
requestAnimationFrame(() => {
307-
setTimeout(() => {
308-
this._asyncCallback();
309-
setTimeout(async () => {
310-
await this._reportCallback();
311-
resolve();
312-
}, 0);
313-
}, 0);
314-
});
315-
}
316-
}
317-
318-
const TEST_INVOKER_LOOKUP = {
319-
__proto__: null,
320-
timer: TimerTestInvoker,
321-
raf: RAFTestInvoker,
322-
};
323-
324270
// https://stackoverflow.com/a/47593316
325271
function seededHashRandomNumberGenerator(a) {
326272
return function () {
@@ -658,7 +604,7 @@ export class SuiteRunner {
658604

659605
const report = () => this._recordTestResults(test, syncTime, asyncTime);
660606
const invokerClass = TEST_INVOKER_LOOKUP[params.measurementMethod];
661-
const invoker = new invokerClass(runSync, measureAsync, report);
607+
const invoker = new invokerClass(runSync, measureAsync, report, params);
662608

663609
return invoker.start();
664610
}

resources/test-invoker.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class TestInvoker {
2+
constructor(syncCallback, asyncCallback, reportCallback, params) {
3+
this._syncCallback = syncCallback;
4+
this._asyncCallback = asyncCallback;
5+
this._reportCallback = reportCallback;
6+
this._params = params;
7+
}
8+
}
9+
10+
export class TimerTestInvoker extends TestInvoker {
11+
start() {
12+
return new Promise((resolve) => {
13+
setTimeout(() => {
14+
this._syncCallback();
15+
setTimeout(() => {
16+
this._asyncCallback();
17+
requestAnimationFrame(async () => {
18+
await this._reportCallback();
19+
resolve();
20+
});
21+
}, 0);
22+
}, this._params.waitBeforeSync);
23+
});
24+
}
25+
}
26+
27+
export class RAFTestInvoker extends TestInvoker {
28+
start() {
29+
return new Promise((resolve) => {
30+
if (this._params.waitBeforeSync)
31+
setTimeout(() => this._scheduleCallbacks(resolve), this._params.waitBeforeSync);
32+
else
33+
this._scheduleCallbacks(resolve);
34+
});
35+
}
36+
37+
_scheduleCallbacks(resolve) {
38+
requestAnimationFrame(() => this._syncCallback());
39+
requestAnimationFrame(() => {
40+
setTimeout(() => {
41+
this._asyncCallback();
42+
setTimeout(async () => {
43+
await this._reportCallback();
44+
resolve();
45+
}, 0);
46+
}, 0);
47+
});
48+
}
49+
}
50+
51+
export const TEST_INVOKER_LOOKUP = {
52+
__proto__: null,
53+
timer: TimerTestInvoker,
54+
raf: RAFTestInvoker,
55+
};

0 commit comments

Comments
 (0)