Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : agentilab-web8 * File : scripts/measure-lighthouse.mjs9 * Module : Scripts10 * Repository : https://git.spboucher.ai/agentilab-web11 * Project Website : Not specified12 * Created : 2026-12-0513 * Last Modified : 2026-12-0514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : Proprietary34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Mesures Lighthouse (profil mobile par défaut de Lighthouse : Moto G Power émulé,44// réseau 4G lent simulé, processeur ralenti 4×). Trois passes par page, médiane retenue.45// Mesures de laboratoire : elles ne remplacent pas les données de terrain.46// Usage : node scripts/measure-lighthouse.mjs [baseUrl] [--user u --password p] [--out fichier.json]47import { execFileSync } from 'node:child_process';48import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs';49import { tmpdir } from 'node:os';50import path from 'node:path';51import { chromium } from '@playwright/test';5253const args = process.argv.slice(2);54const opt = (name) => (args.includes(name) ? args[args.indexOf(name) + 1] : undefined);55const base = (args[0] && !args[0].startsWith('--') ? args[0] : 'http://localhost:8190').replace(/\/$/, '');56const PAGES = ['/', '/ecosysteme', '/ecosysteme/orix', '/entreprise', '/en'];57const RUNS = 3;58const dir = mkdtempSync(path.join(tmpdir(), 'agl-lh-'));59const headers = opt('--user') ? { Authorization: `Basic ${Buffer.from(`${opt('--user')}:${opt('--password')}`).toString('base64')}` } : null;60const median = (values) => [...values].sort((a, b) => a - b)[Math.floor(values.length / 2)];6162const report = { measuredAt: new Date().toISOString(), base: base.replace(/\/\/[^@/]*@/, '//'), profile: 'Lighthouse mobile (défauts)', runs: RUNS, pages: {} };63for (const page of PAGES) {64 const runs = [];65 for (let i = 0; i < RUNS; i++) {66 const out = path.join(dir, `r${i}.json`);67 execFileSync('pnpm', [68 'dlx', 'lighthouse@12', base + page, '--quiet', '--output=json', `--output-path=${out}`,69 '--only-categories=performance,accessibility,best-practices,seo',70 '--chrome-flags=--headless=new --no-sandbox',71 ...(headers ? [`--extra-headers=${JSON.stringify(headers)}`] : []),72 ], { stdio: ['ignore', 'ignore', 'inherit'], env: { ...process.env, CHROME_PATH: chromium.executablePath() } });73 const lhr = JSON.parse(readFileSync(out, 'utf8'));74 runs.push({75 performance: Math.round(lhr.categories.performance.score * 100),76 accessibility: Math.round(lhr.categories.accessibility.score * 100),77 bestPractices: Math.round(lhr.categories['best-practices'].score * 100),78 seo: Math.round(lhr.categories.seo.score * 100),79 lcpMs: Math.round(lhr.audits['largest-contentful-paint'].numericValue),80 cls: Number(lhr.audits['cumulative-layout-shift'].numericValue.toFixed(3)),81 tbtMs: Math.round(lhr.audits['total-blocking-time'].numericValue),82 failed: Object.values(lhr.audits).filter((a) => a.score !== null && a.score < 0.9 && a.scoreDisplayMode === 'binary').map((a) => a.id),83 });84 }85 const keys = ['performance', 'accessibility', 'bestPractices', 'seo', 'lcpMs', 'cls', 'tbtMs'];86 report.pages[page] = { median: Object.fromEntries(keys.map((k) => [k, median(runs.map((r) => r[k]))])), runs };87 console.log(page, JSON.stringify(report.pages[page].median), [...new Set(runs.flatMap((r) => r.failed))].join(',') || '');88}89if (opt('--out')) writeFileSync(opt('--out'), JSON.stringify(report, null, 2) + '\n');90