Hémisphérique 2,35 → 1,75, ciel refroidi #e4ebf6, sol #c9cbcd ; soleil 1,05 → 2,1 (même direction) ; ombre 0,7 → 0,62.
Mesures (banc, 2560 px) : contraste des pans témoin 1,13 → 1,32, maisons 4 et 13 1,07/1,10 → 1,29/1,34 ; saturation
de l'ombre portée (11 échantillons) 0,19 → 0,05. ?rendu=avant et ?mode=fond : 0 px contre c946484.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
9.7 KiB
JavaScript
130 lines
9.7 KiB
JavaScript
// Banc de mesure et de captures, lot M1 trait et gris. node bench.mjs <base-url> <label> <outdir> [quoi]
|
|
// quoi : captures | mesures | fond | fps | tout
|
|
import { createRequire } from 'node:module';
|
|
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
const require = createRequire(process.env.PW_CORE || '/home/julesneny8/.npm/_npx/9833c18b2d85bc59/node_modules/');
|
|
const { chromium } = require('playwright-core');
|
|
const [base, label, out, quoi = 'tout'] = process.argv.slice(2);
|
|
mkdirSync(out, { recursive: true });
|
|
const EXE = process.env.CHROMIUM || '/home/julesneny8/.cache/ms-playwright/chromium-1254/chrome-linux64/chrome';
|
|
const D = JSON.parse(readFileSync(new URL('../data/scene2035.json', import.meta.url), 'utf8'));
|
|
const extra = process.env.EXTRA || ''; // ex. &rendu=avant
|
|
|
|
const browser = await chromium.launch({ executablePath: EXE, args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'] });
|
|
const erreurs = [];
|
|
async function ouvrir(w, h, dpr, q, hash, reduit = true) {
|
|
const ctx = await browser.newContext({ viewport: { width: w, height: h }, deviceScaleFactor: dpr, reducedMotion: reduit ? 'reduce' : 'no-preference' });
|
|
const page = await ctx.newPage();
|
|
if (reduit && !process.env.HORLOGE_LIBRE) await page.addInitScript(() => { const T = 5000; performance.now = () => T; const r = window.requestAnimationFrame.bind(window); window.requestAnimationFrame = (f) => r(() => f(T)); });
|
|
page.on('console', (m) => { if (m.type() === 'error') erreurs.push(`${w} ${q}${hash}: ${m.text()}`); });
|
|
page.on('pageerror', (e) => erreurs.push(`${w} ${q}${hash}: ${e.message}`));
|
|
if (process.env.CANVAS_SEUL) await page.addInitScript(() => addEventListener('DOMContentLoaded', () => { const st = document.createElement('style'); st.textContent = '*{visibility:hidden!important} #scene{visibility:visible!important}'; document.head.append(st); }));
|
|
await page.goto(`${base}/?${q}${extra}${hash ? '#' + hash : ''}`, { waitUntil: 'networkidle' });
|
|
await page.waitForTimeout(3500);
|
|
return { ctx, page };
|
|
}
|
|
|
|
// les deux pans de toit d'une maison (même calcul que maison() de scene.js)
|
|
function pans(h) {
|
|
const p = h.p, c = [p.reduce((a, q) => a + q[0], 0) / 4, p.reduce((a, q) => a + q[1], 0) / 4];
|
|
const e1 = [p[1][0] - p[0][0], p[1][1] - p[0][1]], e2 = [p[2][0] - p[1][0], p[2][1] - p[1][1]];
|
|
const n = (v) => { const l = Math.hypot(...v); return [v[0] / l, v[1] / l]; }, fw = [Math.cos(h.f), -Math.sin(h.f)];
|
|
let u, Lv;
|
|
if (Math.abs(n(e1)[0] * fw[0] + n(e1)[1] * fw[1]) >= Math.abs(n(e2)[0] * fw[0] + n(e2)[1] * fw[1])) { u = n(e1); Lv = Math.hypot(...e2); } else { u = n(e2); Lv = Math.hypot(...e1); }
|
|
const r = Math.atan2(-u[1], u[0]), eave = 5.4, rh = Math.min(3.4, Lv * 0.31);
|
|
return [-1, 1].map((s) => { const z = s * Lv / 4; return [c[0] + z * Math.sin(r), eave + rh / 2 + 0.1, c[1] + z * Math.cos(r)]; });
|
|
}
|
|
// ombre portée : un point du sol du côté opposé au soleil (soleil de (-110,170,120) : l'ombre part vers +x, -z), et un point éclairé côté soleil
|
|
function sols(h) {
|
|
const p = h.p, c = [p.reduce((a, q) => a + q[0], 0) / 4, p.reduce((a, q) => a + q[1], 0) / 4];
|
|
const R = Math.max(...p.map((q) => Math.hypot(q[0] - c[0], q[1] - c[1])));
|
|
const d = [0.675, -0.737];
|
|
return { ombre: [c[0] + d[0] * (R + 1.2), 0.05, c[1] + d[1] * (R + 1.2)], lumiere: [c[0] - d[0] * (R + 4), 0.05, c[1] - d[1] * (R + 4)] };
|
|
}
|
|
const lum = ([r, g, b]) => { const f = (v) => { v /= 255; return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4; }; return 0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b); };
|
|
const sat = ([r, g, b]) => { const M = Math.max(r, g, b) / 255, m = Math.min(r, g, b) / 255, L = (M + m) / 2; return M === m ? 0 : L > 0.5 ? (M - m) / (2 - M - m) : (M - m) / (M + m); };
|
|
|
|
async function pixels(page, pts) {
|
|
// une image rendue maintenant, lue aux points écran (px CSS) donnés
|
|
return page.evaluate(async (pts) => {
|
|
const Q = window.__quartier, url = Q.renderer.domElement.toDataURL ? (Q.renderer.render ? null : null) : null;
|
|
const img = await new Promise((ok) => { const i = new Image(); i.onload = () => ok(i); i.src = window.__apercuPNG(); });
|
|
const c = document.createElement('canvas'); c.width = img.width; c.height = img.height; const x = c.getContext('2d'); x.drawImage(img, 0, 0);
|
|
const k = img.width / innerWidth;
|
|
return pts.map(([X, Y]) => { const d = x.getImageData(Math.round(X * k), Math.round(Y * k), 1, 1).data; return [d[0], d[1], d[2]]; });
|
|
}, pts);
|
|
}
|
|
async function preparer(page) {
|
|
await page.evaluate(() => {
|
|
const Q = window.__quartier, r = Q.renderer;
|
|
window.__apercuPNG = () => { const s = r.domElement; r.info.autoReset = true; window.__scene3.apercu(); return s.toDataURL('image/png'); };
|
|
});
|
|
}
|
|
|
|
const res = { label, erreurs };
|
|
if (quoi === 'captures' || quoi === 'tout') {
|
|
const SH = [['390-M-01-arrivee', 390, 844, 3, ''], ['390-M-02-convoi', 390, 844, 3, 'quartier-convoi'], ['1280-M-01-ensemble', 1280, 800, 1, ''],
|
|
['1280-M-02-arret-convoi', 1280, 800, 1, 'quartier-convoi'], ['1280-M-03-velos-gros-plan', 1280, 800, 1, 'quartier-cargos'], ['1280-M-04-foret', 1280, 800, 1, 'vivant-foret']];
|
|
const garder = (process.env.SHOTS || '').split(',').filter(Boolean);
|
|
for (const [nom, w, h, dpr, hash] of SH) {
|
|
if (garder.length && !garder.includes(nom)) continue;
|
|
const { ctx, page } = await ouvrir(w, h, dpr, 'mesure', hash);
|
|
await page.screenshot({ path: `${out}/${nom}-${label}.png` }); await ctx.close();
|
|
}
|
|
}
|
|
if (quoi === 'mesures' || quoi === 'tout') {
|
|
const MW = +(process.env.MESURE_W || 2560), { ctx, page } = await ouvrir(MW, Math.round(MW * 0.625), 1, 'mesure', '');
|
|
// une image : passer par la boucle (besoin) n'est pas garanti sous mouvement réduit ; on rend par renderer avec la scène du canvas
|
|
const st = await page.evaluate(() => {
|
|
const r = window.__quartier.renderer; const before = { ...r.info.render };
|
|
return { calls: before.calls, triangles: before.triangles, lines: before.lines, geometries: r.info.memory.geometries, programs: r.info.programs?.length };
|
|
});
|
|
res.info0 = st;
|
|
// points de mesure
|
|
const T = D.maisons.map((h, i) => [h, i]).filter(([h]) => h.t === 'transformee');
|
|
const temoinIdx = T[D.temoin.maison][1];
|
|
const ordinaires = D.maisons.map((h, i) => [h, i]).filter(([h]) => h.t === 'existante').map(([, i]) => i);
|
|
const toits = [temoinIdx, ...ordinaires];
|
|
const pts3 = [];
|
|
for (const i of toits) { const [a, b] = pans(D.maisons[i]); pts3.push(a, b); }
|
|
const solsP = ordinaires.map((i) => sols(D.maisons[i]));
|
|
for (const s of solsP) pts3.push(s.ombre, s.lumiere);
|
|
const ecr = await page.evaluate((P) => P.map(([x, y, z]) => { const e = window.__quartier.ecran(x, y, z); return [e.x, e.y]; }), pts3);
|
|
const sh = await page.screenshot();
|
|
writeFileSync(`${out}/mesure-${MW}-${label}.png`, sh);
|
|
const px = await page.evaluate(async ({ b64, pts }) => {
|
|
const img = await new Promise((ok) => { const i = new Image(); i.onload = () => ok(i); i.src = 'data:image/png;base64,' + b64; });
|
|
const c = document.createElement('canvas'); c.width = img.width; c.height = img.height; const x = c.getContext('2d'); x.drawImage(img, 0, 0);
|
|
return pts.map(([X, Y]) => { let s = [0, 0, 0]; for (let dx = -1; dx <= 1; dx++) for (let dy = -1; dy <= 1; dy++) { const d = x.getImageData(Math.round(X) + dx, Math.round(Y) + dy, 1, 1).data; s[0] += d[0]; s[1] += d[1]; s[2] += d[2]; } return s.map((v) => Math.round(v / 9)); });
|
|
}, { b64: sh.toString('base64'), pts: ecr });
|
|
let k = 0; res.toits = [];
|
|
for (const i of toits) { const a = px[k++], b = px[k++]; const la = lum(a), lb = lum(b); res.toits.push({ maison: i, temoin: i === temoinIdx, ecran: [ecr[k - 2].map(Math.round), ecr[k - 1].map(Math.round)], rgb: [a, b], contraste: +(Math.max(la, lb) / Math.min(la, lb)).toFixed(3) }); }
|
|
res.sols = [];
|
|
for (const i of ordinaires) { const o = px[k++], l = px[k++]; res.sols.push({ maison: i, ecran: [ecr[k - 2].map(Math.round), ecr[k - 1].map(Math.round)], ombre: o, lumiere: l, lo: +lum(o).toFixed(4), ll: +lum(l).toFixed(4), sat: +sat(o).toFixed(3) }); }
|
|
// poids : octets transférés au premier affichage
|
|
res.octets = await page.evaluate(() => { const n = performance.getEntriesByType('navigation')[0]; return Math.round((n.transferSize + performance.getEntriesByType('resource').reduce((s, r) => s + (r.transferSize || 0), 0)) / 1024); });
|
|
res.ressources = await page.evaluate(() => performance.getEntriesByType('resource').length);
|
|
await ctx.close();
|
|
}
|
|
if (quoi === 'fond' || quoi === 'tout') {
|
|
for (const [w, h, dpr] of [[1280, 800, 1], [390, 844, 3]]) {
|
|
const { ctx, page } = await ouvrir(w, h, dpr, 'mode=fond', '');
|
|
await page.screenshot({ path: `${out}/fond-${w}-${label}.png` }); await ctx.close();
|
|
}
|
|
}
|
|
if (quoi === 'fps' || quoi === 'tout') {
|
|
res.fps = {};
|
|
for (const cpu of [1, 4]) {
|
|
const { ctx, page } = await ouvrir(1280, 800, 1, 'mesure', '', false);
|
|
const cdp = await ctx.newCDPSession(page); await cdp.send('Emulation.setCPUThrottlingRate', { rate: cpu });
|
|
await page.waitForTimeout(1500);
|
|
res.fps[`x${cpu}`] = await page.evaluate(() => new Promise((ok) => { let n = 0; const t0 = performance.now(); const f = () => { n++; if (performance.now() - t0 < 6000) requestAnimationFrame(f); else ok(+(n / ((performance.now() - t0) / 1000)).toFixed(1)); }; requestAnimationFrame(f); }));
|
|
// appels de dessin et triangles d'une image de la boucle
|
|
res.fps[`info${cpu}`] = await page.evaluate(() => { const i = window.__quartier.renderer.info.render; return { calls: i.calls, triangles: i.triangles, lines: i.lines }; });
|
|
await ctx.close();
|
|
}
|
|
}
|
|
await browser.close();
|
|
writeFileSync(`${out}/mesures-${label}${quoi === 'tout' ? '' : '-' + quoi}.json`, JSON.stringify(res, null, 1));
|
|
console.log(JSON.stringify({ label, info0: res.info0, fps: res.fps, octets: res.octets, erreurs: erreurs.length }, null, 0));
|