71 lines
3.3 KiB
JavaScript
71 lines
3.3 KiB
JavaScript
const { performance } = require('node:perf_hooks');
|
|
const { writeFileSync } = require('node:fs');
|
|
const { FeedService } = require('../dist/legacy/src/modules/feed/feed.service');
|
|
const baseline = require('../test/fixtures/feed-diversity-baseline.cjs');
|
|
const previous = require('../test/fixtures/feed-diversity-scan-baseline.cjs');
|
|
const service = Object.create(FeedService.prototype);
|
|
|
|
function measure(operation) {
|
|
for (let i = 0; i < 10; i++) operation();
|
|
const times = [];
|
|
for (let i = 0; i < 50; i++) {
|
|
const start = performance.now();
|
|
operation();
|
|
times.push(performance.now() - start);
|
|
}
|
|
times.sort((a, b) => a - b);
|
|
return { medianMs: +times[24].toFixed(3), p95Ms: +times[47].toFixed(3) };
|
|
}
|
|
|
|
function paired(before, after) {
|
|
for (let i = 0; i < 10; i++) { before(); after(); }
|
|
const samples = [[], []];
|
|
for (let i = 0; i < 50; i++) {
|
|
for (const index of i % 2 ? [1, 0] : [0, 1]) {
|
|
const start = performance.now();
|
|
[before, after][index]();
|
|
samples[index].push(performance.now() - start);
|
|
}
|
|
}
|
|
const stats = samples.map((times) => {
|
|
times.sort((a, b) => a - b);
|
|
return { medianMs: +times[24].toFixed(3), p95Ms: +times[47].toFixed(3) };
|
|
});
|
|
return { before: stats[0], after: stats[1],
|
|
medianReductionPercent: +((1 - stats[1].medianMs / stats[0].medianMs) * 100).toFixed(2) };
|
|
}
|
|
|
|
const results = [];
|
|
for (const size of [100, 400, 1000]) {
|
|
const rows = Array.from({ length: size }, (_, i) => ({ score: size - i,
|
|
post: { _id: String(i), authorId: { id: String(i % 50) },
|
|
postType: i % 2 ? 'audio' : 'video', hashtags: ['Music', 'oud', 'live'] } }));
|
|
const expected = baseline.call(service, rows);
|
|
const actual = service.diversifyRankedPosts(rows, 20);
|
|
if (JSON.stringify(expected.slice(0, 20)) !== JSON.stringify(actual)) {
|
|
throw new Error(`Ranking output changed at ${size} candidates`);
|
|
}
|
|
const full = service.diversifyRankedPosts(rows);
|
|
if (JSON.stringify(expected) !== JSON.stringify(full) ||
|
|
JSON.stringify(previous.call(service, rows)) !== JSON.stringify(full)) {
|
|
throw new Error(`Full snapshot ranking output changed at ${size} candidates`);
|
|
}
|
|
const before = measure(() => baseline.call(service, rows));
|
|
const after = measure(() => service.diversifyRankedPosts(rows, 20));
|
|
const fullAfter = measure(() => service.diversifyRankedPosts(rows));
|
|
results.push({ candidates: size, pageSize: 20, before, after, fullAfter,
|
|
speedup: +(before.medianMs / after.medianMs).toFixed(2),
|
|
currentChange: {
|
|
firstPage: paired(() => previous.call(service, rows, 20), () => service.diversifyRankedPosts(rows, 20)),
|
|
fullSnapshot: paired(() => previous.call(service, rows), () => service.diversifyRankedPosts(rows)),
|
|
} });
|
|
}
|
|
const report = { node: process.version, runsPerMeasurement: 50,
|
|
description: 'CPU only. Legacy before/after compares original full ranking with a first-page prefix. currentChange compares the immediately preceding scan with the new implementation at equal output sizes, using alternating paired measurements. Full IDs, tie order and scores verified.', results };
|
|
const outputIndex = process.argv.indexOf('--output');
|
|
if (outputIndex >= 0) {
|
|
if (!process.argv[outputIndex + 1]) throw new Error('--output requires a path');
|
|
writeFileSync(process.argv[outputIndex + 1], JSON.stringify(report, null, 2) + '\n');
|
|
}
|
|
console.log(JSON.stringify(report, null, 2));
|