Repository navigation
Expand file tree
/
Copy pathperf.js
More file actions
79 lines (74 loc) · 4.02 KB
/
Copy pathperf.js
File metadata and controls
79 lines (74 loc) · 4.02 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
/* Measures frame pacing and the settled quality tier, and can capture two
screenshots a few seconds apart to confirm the disk is visibly rotating. */
const { spawn } = require('child_process');
const fs = require('fs'), path = require('path'), os = require('os');
const FILE = process.argv[2] || 'gargantua.html';
const W = parseInt(process.argv[3] || '1920', 10);
const H = parseInt(process.argv[4] || '1080', 10);
const SECS = parseInt(process.argv[5] || '12', 10);
const CHROME = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
const PORT = 9500 + (Date.now() % 400);
const prof = fs.mkdtempSync(path.join(os.tmpdir(), 'bhperf-'));
const ch = spawn(CHROME, ['--headless=new', `--remote-debugging-port=${PORT}`,
`--user-data-dir=${prof}`, '--no-first-run', '--no-default-browser-check',
'--hide-scrollbars', '--mute-audio', '--enable-unsafe-swiftshader',
`--window-size=${W},${H}`, 'about:blank'], { stdio: ['ignore', 'pipe', 'pipe'] });
const sleep = ms => new Promise(r => setTimeout(r, ms));
(async () => {
let t = null;
for (let i = 0; i < 80 && !t; i++) {
try {
const j = await (await fetch(`http://127.0.0.1:${PORT}/json/list`)).json();
t = j.find(x => x.type === 'page');
} catch (e) {}
if (!t) await sleep(250);
}
const ws = new WebSocket(t.webSocketDebuggerUrl);
let id = 0; const pend = new Map(); const logs = [];
await new Promise(r => { ws.onopen = r; });
ws.onmessage = e => {
const m = JSON.parse(e.data);
if (m.id && pend.has(m.id)) { pend.get(m.id)(m); pend.delete(m.id); }
else if (m.method === 'Runtime.exceptionThrown') logs.push('EXCEPTION ' + JSON.stringify(m.params.exceptionDetails.text));
};
const send = (method, params) => new Promise(res => { const i = ++id; pend.set(i, res); ws.send(JSON.stringify({ id: i, method, params: params || {} })); });
await send('Runtime.enable');
await send('Page.enable');
await send('Page.addScriptToEvaluateOnNewDocument', {
source: `window.__fr=[];(function(){var raf=window.requestAnimationFrame,last=0;
window.requestAnimationFrame=function(cb){return raf(function(ts){
if(last) window.__fr.push(ts-last); last=ts; if(window.__fr.length>4000) window.__fr.shift(); cb(ts);});};})();`
});
let src = fs.readFileSync(FILE, 'utf8');
src = src.replace(' frame++;', ' frame++; window.__q=qIdx; window.__rt=RT.w+"x"+RT.h; window.__dt=dtEMA; window.__st=simT;');
if (process.env.BH_RAW) {
for (const pair of process.env.BH_RAW.split('###')) {
const i = pair.indexOf('|||');
src = src.replace(pair.slice(0, i), pair.slice(i + 3));
}
}
if (process.env.BH_LOCKQ) src = src.replace('var qIdx = 1, qw = 1,', 'var qIdx = ' + process.env.BH_LOCKQ + ', qw = ' + process.env.BH_LOCKQ + ',').replace('qHold = 8,', 'qHold = 1e9,');
fs.writeFileSync('__perf.html', src);
await send('Page.navigate', { url: 'file:///' + path.resolve('__perf.html').replace(/\\/g, '/') });
for (let s = 1; s <= SECS; s++) {
await sleep(1000);
const r = await send('Runtime.evaluate', {
expression: `(function(){var f=window.__fr||[];var n=f.slice(-30).slice();
n.sort(function(a,b){return a-b;});
var med=n.length?n[(n.length>>1)]:0;
var c=document.querySelector('canvas');
return [f.length, med.toFixed(1), c?c.width+'x'+c.height:'?',
'q='+window.__q, 'rt='+window.__rt, 'dt='+(window.__dt||0).toFixed(1),
'simT='+(window.__st||0).toFixed(1)].join(' | ');})()`,
returnByValue: true
});
console.log('t=' + s + 's ' + (r.result && r.result.result && r.result.result.value));
if (s === Math.max(1, SECS - 6) || s === SECS) {
const shot = await send('Page.captureScreenshot', { format: 'png' });
if (shot.result && shot.result.data)
fs.writeFileSync(s === SECS ? 'view_b.png' : 'view_a.png', Buffer.from(shot.result.data, 'base64'));
}
}
if (logs.length) console.log(logs.join('\n'));
ws.close(); ch.kill(); setTimeout(() => process.exit(0), 300);
})().catch(e => { console.log('ERR ' + e.message); ch.kill(); process.exit(1); });