Files
back_end_oudelaa/test/performance-capture.cjs

26 lines
1.3 KiB
JavaScript

// Opt-in timing of existing real HTTP scenarios, including writes and negative cases.
// Persist only normalized routes and timings, never bodies, headers, IDs or query values.
if (process.env.PERF_CAPTURE_DIR) {
const { performance } = require('node:perf_hooks');
const { mkdirSync, appendFileSync, readFileSync } = require('node:fs');
const { join, resolve } = require('node:path');
const Test = require('supertest/lib/test');
const { routeFor } = require('../scripts/perf-route-catalog');
const routes = JSON.parse(readFileSync(resolve('docs/performance/route-catalog.json'), 'utf8')).routes;
const directory = resolve(process.env.PERF_CAPTURE_DIR);
mkdirSync(directory, { recursive: true });
const end = Test.prototype.end;
Test.prototype.end = function (callback) {
const started = performance.now();
const route = routeFor(routes, this.method, this.url);
return end.call(this, (error, response) => {
if (route) appendFileSync(join(directory, `http-${process.pid}.jsonl`), JSON.stringify({
method: route.method, path: route.path, status: response?.status ?? 0,
durationMs: +(performance.now() - started).toFixed(3),
contractPassed: !error, source: 'existing-integration-scenario',
}) + '\n');
callback(error, response);
});
};
}