-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathbench-baseline.js
More file actions
485 lines (417 loc) · 17.1 KB
/
Copy pathbench-baseline.js
File metadata and controls
485 lines (417 loc) · 17.1 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env node
/**
* BotBrowser Benchmark -Dimension 1: Single Instance Baseline
*
* Compares Stock Chrome (no profile) vs BotBrowser with Android/Windows/macOS profiles.
* Tests: Speedometer 3.0, JetStream 3, MotionMark, fingerprint API overhead, page loads.
*
* Usage:
* node scripts/bench-baseline.js --mode headless
* node scripts/bench-baseline.js --mode headed --runs 5
*/
import { chromium } from 'playwright-core';
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';
import { getConfig } from '../config.js';
import {
stats, saveResults, serveDir, parseArgs, printTable,
waitForPageCondition, timedMsAsync, getSystemInfo,
} from '../utils.js';
const config = getConfig();
const args = parseArgs();
const HEADLESS = args.mode === 'headless';
const RUNS = args.runs;
const BENCH_DIR = config.benchDir;
// ── Test Configurations ────────────────────────────────────────────────
function buildConfigs() {
const configs = [
{ name: 'Stock Chrome (no profile)', profile: null },
];
for (const [platform, profilePath] of Object.entries(config.profiles)) {
if (profilePath) {
configs.push({ name: `BotBrowser + ${platform}`, profile: profilePath });
}
}
return configs;
}
// ── Browser Launch ─────────────────────────────────────────────────────
async function launchBrowser(profilePath) {
const launchArgs = [
'--no-first-run',
'--disable-sync',
'--disable-extensions',
'--disable-background-networking',
];
// Linux needs extra flags (especially when running as root)
if (process.platform === 'linux') {
launchArgs.push(
'--no-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
);
}
if (profilePath) {
launchArgs.push(`--bot-profile=${profilePath}`);
}
return chromium.launch({
executablePath: config.browserPath,
headless: HEADLESS,
args: launchArgs,
});
}
// ── Benchmark Suite Runners ────────────────────────────────────────────
async function runSpeedometer(browser, serverUrl) {
const context = await browser.newContext({ viewport: { width: 1280, height: 800 } });
const page = await context.newPage();
try {
await page.goto(`${serverUrl}/`, { waitUntil: 'load', timeout: 60000 });
// Click "Start Test" button
await page.click('button.start-tests-button', { timeout: 10000 });
// Wait for benchmark to complete -score appears in #result-number
// The page transitions from #running to #summary section when done
await waitForPageCondition(page, () => {
const el = document.querySelector('#result-number');
return el && el.textContent && el.textContent.trim().length > 0 && parseFloat(el.textContent) > 0;
}, 300000); // 5 min timeout
// Extract score
const score = await page.evaluate(() => {
const el = document.querySelector('#result-number');
return el ? parseFloat(el.textContent) : null;
});
return score;
} finally {
await context.close();
}
}
async function runJetStream(browser, serverUrl) {
const context = await browser.newContext({ viewport: { width: 1280, height: 800 } });
const page = await context.newPage();
try {
// Use startAutomatically=true to auto-start (JetStream doesn't start by default)
await page.goto(`${serverUrl}/?startAutomatically=true`, { waitUntil: 'load', timeout: 120000 });
// Wait for completion: JetStream.isDone becomes true or result-summary gets 'done' class
// JetStream 3 can take 10-30 minutes depending on hardware
await waitForPageCondition(page, () => {
return window.JetStream?.isDone === true
|| document.getElementById('result-summary')?.classList.contains('done');
}, 1800000); // 30 min timeout
const score = await page.evaluate(() => {
const el = document.querySelector('#result-summary .score');
return el ? parseFloat(el.textContent) : null;
});
return score;
} finally {
await context.close();
}
}
async function runMotionMark(browser, serverUrl) {
// MotionMark requires landscape viewport -create page with explicit wide viewport
// This is needed especially for mobile profiles (Android) which set smaller screen sizes
const context = await browser.newContext({ viewport: { width: 1280, height: 800 } });
const page = await context.newPage();
try {
// MotionMark root index.html redirects to MotionMark/ subdirectory
await page.goto(`${serverUrl}/MotionMark/`, { waitUntil: 'load', timeout: 60000 });
// Wait for start button to exist in DOM (may be hidden due to CSS orientation checks)
await page.waitForSelector('#start-button', { state: 'attached', timeout: 30000 });
// Wait for frame rate detection, then force-start via JS (bypasses visibility/disabled checks)
let started = false;
for (let attempt = 0; attempt < 20 && !started; attempt++) {
await new Promise(r => setTimeout(r, 2000));
try {
started = await page.evaluate(() => {
const btn = document.getElementById('start-button');
if (btn && !btn.disabled) {
benchmarkController.startBenchmark();
return true;
}
// If still detecting frame rate, check if we can force it
if (btn && btn.disabled && btn.textContent.includes('Run')) {
btn.disabled = false;
benchmarkController.startBenchmark();
return true;
}
return false;
});
} catch { /* retry */ }
}
if (!started) throw new Error('Could not start MotionMark benchmark');
// Wait for completion -body gets class 'showing-results' and score is populated
await waitForPageCondition(page, () => {
return document.body.classList.contains('showing-results')
&& document.querySelector('#results .score')?.textContent?.trim().length > 0;
}, 600000);
// Score format: "{score} @ {fps}fps" -parseFloat extracts the numeric score
const score = await page.evaluate(() => {
const el = document.querySelector('#results .score');
return el ? parseFloat(el.textContent) : null;
});
return score;
} finally {
await context.close();
}
}
// ── Fingerprint API Overhead ───────────────────────────────────────────
async function runFingerprintBench(browser, pagesServerUrl) {
const page = await browser.newPage();
try {
await page.goto(`${pagesServerUrl}/fingerprint-bench.html`, { waitUntil: 'load', timeout: 30000 });
// Wait for benchmark to finish
await waitForPageCondition(page, () => window.__benchDone === true, 120000);
const results = await page.evaluate(() => window.__benchResults);
return results;
} finally {
await page.close();
}
}
// ── Page Load Timing ───────────────────────────────────────────────────
async function measurePageLoad(browser, url, label) {
const times = [];
for (let i = 0; i < 10; i++) {
const page = await browser.newPage();
try {
const start = performance.now();
await page.goto(url, { waitUntil: 'load', timeout: 30000 });
const elapsed = performance.now() - start;
times.push(elapsed);
} catch (e) {
times.push(-1);
} finally {
await page.close();
}
}
return { label, url, ...stats(times.filter(t => t >= 0)) };
}
async function measurePageLoadLocal(browser) {
// Use route interception to serve pages locally
const page = await browser.newPage();
const times = [];
await page.route('https://example.com/**', async (route) => {
await route.fulfill({
status: 200,
contentType: 'text/html',
body: '<!DOCTYPE html><html><head></head><body><h1>Benchmark</h1><p>Local page for load timing.</p></body></html>',
});
});
for (let i = 0; i < 10; i++) {
const start = performance.now();
await page.goto('https://example.com/bench', { waitUntil: 'load' });
times.push(performance.now() - start);
}
await page.close();
return { label: 'Local intercepted page', ...stats(times) };
}
// ── Main ───────────────────────────────────────────────────────────────
async function main() {
console.log('='.repeat(70));
console.log('BotBrowser Benchmark -Dimension 1: Single Instance Baseline');
console.log('='.repeat(70));
console.log(`Platform: ${config.platform} (${config.arch})`);
console.log(`Browser: ${config.browserPath}`);
console.log(`Mode: ${args.mode}`);
console.log(`Runs: ${RUNS}`);
console.log('');
const testConfigs = buildConfigs();
console.log(`Test configurations: ${testConfigs.map(c => c.name).join(', ')}`);
console.log('');
// Start local HTTP servers
const pagesDir = path.join(BENCH_DIR, 'pages');
const pagesServer = await serveDir(pagesDir);
console.log(`Pages server: ${pagesServer.url}`);
// Check for benchmark suites
const benchmarksDir = path.join(BENCH_DIR, 'benchmarks');
const hasSpeedometer = fs.existsSync(path.join(benchmarksDir, 'speedometer', 'index.html'));
const hasJetStream = fs.existsSync(path.join(benchmarksDir, 'jetstream', 'index.html'));
const hasMotionMark = fs.existsSync(path.join(benchmarksDir, 'motionmark', 'MotionMark', 'index.html'));
let speedometerServer, jetStreamServer, motionMarkServer;
if (hasSpeedometer) {
speedometerServer = await serveDir(path.join(benchmarksDir, 'speedometer'));
console.log(`Speedometer server: ${speedometerServer.url}`);
}
if (hasJetStream) {
jetStreamServer = await serveDir(path.join(benchmarksDir, 'jetstream'));
console.log(`JetStream server: ${jetStreamServer.url}`);
}
if (hasMotionMark) {
motionMarkServer = await serveDir(path.join(benchmarksDir, 'motionmark'));
console.log(`MotionMark server: ${motionMarkServer.url}`);
}
console.log('');
const allResults = {
mode: args.mode,
configurations: {},
};
// Initialize result structures for each config
for (const cfg of testConfigs) {
allResults.configurations[cfg.name] = {
name: cfg.name,
profile: cfg.profile,
benchmarks: {},
fingerprint: null,
pageLoads: [],
};
}
// ── Standard Benchmarks (INTERLEAVED to eliminate warm-up bias) ──────
// Instead of running all Stock runs then all BB runs, we alternate:
// Run 1: Stock → BB+android → BB+windows → ...
// Run 2: Stock → BB+android → BB+windows → ...
// This ensures each config gets the same thermal/cache conditions.
const benchmarks = [
{ key: 'speedometer', server: speedometerServer, runner: runSpeedometer, label: 'Speedometer 3.0' },
{ key: 'jetstream', server: jetStreamServer, runner: runJetStream, label: 'JetStream 3' },
{ key: 'motionmark', server: motionMarkServer, runner: runMotionMark, label: 'MotionMark' },
];
for (const bench of benchmarks) {
if (!bench.server) continue;
console.log('='.repeat(50));
console.log(`${bench.label} -${RUNS} runs x ${testConfigs.length} configs (interleaved)`);
console.log('='.repeat(50));
// Collect scores per config
const scoresByConfig = {};
for (const cfg of testConfigs) scoresByConfig[cfg.name] = [];
// Warm-up run (discarded) -run each config once to warm caches
console.log(' Warm-up round (discarded)...');
for (const cfg of testConfigs) {
const browser = await launchBrowser(cfg.profile);
try {
await bench.runner(browser, bench.server.url);
console.log(` ${cfg.name}: done`);
} catch (e) {
console.log(` ${cfg.name}: ${e.message}`);
} finally {
await browser.close();
}
}
// Interleaved measurement runs
for (let run = 0; run < RUNS; run++) {
console.log(` Run ${run + 1}/${RUNS}:`);
for (const cfg of testConfigs) {
const browser = await launchBrowser(cfg.profile);
try {
const score = await bench.runner(browser, bench.server.url);
if (score !== null) scoresByConfig[cfg.name].push(score);
console.log(` ${cfg.name}: ${score}`);
} catch (e) {
console.log(` ${cfg.name}: ERROR - ${e.message}`);
} finally {
await browser.close();
}
}
}
// Store stats
for (const cfg of testConfigs) {
const scores = scoresByConfig[cfg.name];
allResults.configurations[cfg.name].benchmarks[bench.key] = stats(scores);
const s = stats(scores);
console.log(` ${cfg.name} -median: ${s.median}, stddev: ${s.stdev?.toFixed(2) ?? 'N/A'}, n=${s.n}`);
}
console.log('');
}
// ── Fingerprint API Overhead (per config, single run) ──────────────
for (const cfg of testConfigs) {
console.log(` Fingerprint API: ${cfg.name}...`);
const browser = await launchBrowser(cfg.profile);
try {
allResults.configurations[cfg.name].fingerprint = await runFingerprintBench(browser, pagesServer.url);
const apiCount = Object.keys(allResults.configurations[cfg.name].fingerprint || {}).length;
console.log(` ${apiCount} APIs tested`);
} catch (e) {
console.log(` ERROR: ${e.message}`);
} finally {
await browser.close();
}
}
// ── Page Load Timing (per config) ──────────────────────────────────
for (const cfg of testConfigs) {
console.log(` Page load: ${cfg.name}...`);
const browser = await launchBrowser(cfg.profile);
try {
const localResult = await measurePageLoadLocal(browser);
allResults.configurations[cfg.name].pageLoads.push(localResult);
console.log(` Local page: ${localResult.median.toFixed(1)}ms`);
const blankResult = await measurePageLoad(browser, 'about:blank', 'about:blank');
allResults.configurations[cfg.name].pageLoads.push(blankResult);
console.log(` about:blank: ${blankResult.median.toFixed(1)}ms`);
} catch (e) {
console.log(` ERROR: ${e.message}`);
} finally {
await browser.close();
}
}
// ── Summary ──────────────────────────────────────────────────────────
console.log('='.repeat(70));
console.log('SUMMARY');
console.log('='.repeat(70));
// Benchmark scores comparison
const benchNames = ['speedometer', 'jetstream', 'motionmark'];
for (const benchName of benchNames) {
const rows = [];
for (const [cfgName, cfgResult] of Object.entries(allResults.configurations)) {
const b = cfgResult.benchmarks[benchName];
if (b && b.n > 0) {
rows.push([cfgName, b.median, b.mean, b.stdev, b.n]);
}
}
if (rows.length > 0) {
console.log(`\n${benchName.toUpperCase()}:`);
printTable(['Configuration', 'Median', 'Mean', 'StdDev', 'Runs'], rows);
}
}
// Fingerprint API comparison
console.log('\nFINGERPRINT API OVERHEAD (median ms):');
const fpHeaders = ['API'];
const fpAPIs = new Set();
for (const cfgResult of Object.values(allResults.configurations)) {
fpHeaders.push(cfgResult.name.substring(0, 25));
if (cfgResult.fingerprint) {
for (const key of Object.keys(cfgResult.fingerprint)) fpAPIs.add(key);
}
}
const fpRows = [];
for (const api of fpAPIs) {
const row = [api];
for (const cfgResult of Object.values(allResults.configurations)) {
const val = cfgResult.fingerprint?.[api]?.median;
row.push(val !== undefined ? val.toFixed(4) : 'N/A');
}
fpRows.push(row);
}
if (fpRows.length > 0) {
printTable(fpHeaders, fpRows);
}
// Page load comparison
console.log('\nPAGE LOAD TIMING (median ms):');
const pageHeaders = ['Page'];
for (const cfgResult of Object.values(allResults.configurations)) {
pageHeaders.push(cfgResult.name.substring(0, 25));
}
const pageLabels = new Set();
for (const cfgResult of Object.values(allResults.configurations)) {
for (const pl of cfgResult.pageLoads) pageLabels.add(pl.label);
}
const pageRows = [];
for (const label of pageLabels) {
const row = [label];
for (const cfgResult of Object.values(allResults.configurations)) {
const pl = cfgResult.pageLoads.find(p => p.label === label);
row.push(pl ? pl.median.toFixed(1) : 'N/A');
}
pageRows.push(row);
}
if (pageRows.length > 0) {
printTable(pageHeaders, pageRows);
}
// Save results
const resultPath = saveResults(`baseline-${args.mode}`, allResults, BENCH_DIR);
console.log(`\nFull results saved to: ${resultPath}`);
// Cleanup
await pagesServer.close();
if (speedometerServer) await speedometerServer.close();
if (jetStreamServer) await jetStreamServer.close();
if (motionMarkServer) await motionMarkServer.close();
}
main().catch(e => {
console.error('Fatal error:', e);
process.exit(1);
});