docs(perf): preserve source-bound static encoding measurements
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import crypto from 'node:crypto';
|
||||
import os from 'node:os';
|
||||
import assert from 'node:assert/strict';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import { gzipSync, gunzipSync, brotliCompressSync, brotliDecompressSync, constants } from 'node:zlib';
|
||||
const [exportPath, output] = process.argv.slice(2);
|
||||
const root = path.resolve(exportPath);
|
||||
const sha = bytes => crypto.createHash('sha256').update(bytes).digest('hex');
|
||||
function walk(dir) { return fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(path.join(dir, e.name)) : [path.join(dir, e.name)]); }
|
||||
const entries = walk(root).filter(name => /\.(?:js|css|html|svg)$/.test(name)).sort();
|
||||
assert.ok(entries.length > 0);
|
||||
const median = values => [...values].sort((a,b)=>a-b)[Math.floor(values.length/2)];
|
||||
const files = entries.map(name => {
|
||||
const raw = fs.readFileSync(name), relative = path.relative(root, name).replaceAll('\\', '/');
|
||||
const gzipTimes = [], brotliTimes = [];
|
||||
let gzip, brotli;
|
||||
for (let repetition=0; repetition<3; repetition++) {
|
||||
let start = performance.now(); gzip = gzipSync(raw, { level: 6 }); gzipTimes.push(performance.now()-start);
|
||||
start = performance.now(); brotli = brotliCompressSync(raw, { params: { [constants.BROTLI_PARAM_QUALITY]: 5 } }); brotliTimes.push(performance.now()-start);
|
||||
assert.deepEqual(gunzipSync(gzip), raw); assert.deepEqual(brotliDecompressSync(brotli), raw);
|
||||
}
|
||||
return { path: relative, sha256: sha(raw), rawBytes: raw.length, gzip6Bytes: gzip.length, brotli5Bytes: brotli.length, gzipMilliseconds: gzipTimes, brotliMilliseconds: brotliTimes, gzipMedianMilliseconds: median(gzipTimes), brotliMedianMilliseconds: median(brotliTimes) };
|
||||
});
|
||||
const sum = group => ({ files: group.length, rawBytes: group.reduce((n,x)=>n+x.rawBytes,0), gzip6Bytes: group.reduce((n,x)=>n+x.gzip6Bytes,0), brotli5Bytes: group.reduce((n,x)=>n+x.brotli5Bytes,0) });
|
||||
const html = fs.readFileSync(path.join(root, 'index.html'), 'utf8');
|
||||
const scriptPaths = [...new Set([...html.matchAll(/<script\b[^>]*\bsrc="([^"]+)"[^>]*>/g)].map(m=>m[1].split('?')[0].replace(/^\//,'')))];
|
||||
assert.ok(scriptPaths.length > 0);
|
||||
const indexScripts = files.filter(f=>scriptPaths.includes(f.path));
|
||||
assert.equal(indexScripts.length, scriptPaths.length);
|
||||
const report = { source: 'eb2538772e1bfabbeb291e4a5efa177c79074de1', environment: { node: process.version, platform: process.platform, architecture: process.arch, cpu: os.cpus()[0].model }, buildApiBase: 'http://localhost:5302/api', indexSha256: sha(Buffer.from(html)), repetitions: 3, summaries: { javascript: sum(files.filter(x=>x.path.endsWith('.js'))), css: sum(files.filter(x=>x.path.endsWith('.css'))), svg: sum(files.filter(x=>x.path.endsWith('.svg'))), html: sum(files.filter(x=>x.path.endsWith('.html'))), declaredIndexScripts: sum(indexScripts) }, sourceMaps: walk(root).filter(name=>name.endsWith('.map')).length, files, limits: ['Offline warm-file compression cost and content sizes only; excludes HTTP headers/TLS/network', 'No nginx/CDN/API compression configuration or production change', 'Complete export inventory is not a route request budget; root declared scripts exclude later dynamic imports', 'Three local repeats do not establish field latency, server throughput or deployed encoding'] };
|
||||
fs.writeFileSync(output, JSON.stringify(report, null, 2)+'\n');
|
||||
console.log(JSON.stringify({ summaries: report.summaries, sourceMaps: report.sourceMaps, verifiedRoundTrips: files.length * 3 * 2 }));
|
||||
Reference in New Issue
Block a user