Repository navigation
Expand file tree
/
Copy pathrunner.js
More file actions
84 lines (69 loc) · 2.15 KB
/
Copy pathrunner.js
File metadata and controls
84 lines (69 loc) · 2.15 KB
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
8D3A
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
// @ts-check
/* eslint-env node, es2020 */
import playwright from 'playwright';
import {startDevServer} from '@web/dev-server';
import getPort from 'get-port';
import * as os from 'os';
import * as path from 'path';
import {promises as fs} from 'fs';
async function main() {
const verbose = process.argv.includes('--verbose');
const devtools = process.argv.includes('--devtools');
const port = await getPort();
const server = await startDevServer({
config: {
rootDir: process.cwd(),
port,
watch: false,
},
readCliArgs: false,
readFileConfig: false,
logStartMessage: verbose,
});
const browserType = 'chromium';
const userDataDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'replicache-playwright-'),
);
const context = await playwright[
browserType
].launchPersistentContext(userDataDir, {devtools});
const page = await context.newPage();
await page.goto(`http://127.0.0.1:${port}/perf/index.html`);
await page.waitForFunction('typeof nextTest === "function"');
logLine('Running benchmarks please wait...');
if (devtools) {
console.log('Enter `await nextTest()` in the devtools console');
return;
}
for (;;) {
const testResult = await page.evaluate('nextTest()');
if (testResult === null) {
break;
}
logLine(formatAsBenchmarkJS(testResult));
}
logLine('Done!');
await server.stop();
await fs.rmdir(userDataDir, {recursive: true});
// context.close does not terminate! Give it a second.
Promise.race([context.close(), wait(1000)]);
}
main().catch(err => {
console.error(err);
process.exit(1);
});
/** @param {string} s */
function logLine(s) {
process.stdout.write(s + '\n');
}
// TODO(arv): Use BenchmarkJS instead of our custom runner?
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function formatAsBenchmarkJS({name, value, median, runs}) {
// Example:
// fib(20) x 11,465 ops/sec ±1.12% (91 runs sampled)
// createObjectBuffer with 200 comments x 81.61 ops/sec ±1.70% (69 runs sampled)
return `${name} x ${value} ±0.0% (${runs} runs sampled)`;
}
function wait(n) {
return new Promise(resolve => setTimeout(resolve, n));
}