Files
psysonic/src/utils/perf/analysisPerfStore.test.ts
T
cucadmuh 81f900c7a6 perf(analysis): measure tpm over trailing 5s window (#948)
* perf(analysis): measure tpm over trailing 5s instead of full minute

Mirror the cover cpm change: a 60s rolling average added too much inertia and
flattened real bursts/stalls. Count completions in the trailing 5s window and
extrapolate to per-minute, so analysis tpm reacts promptly and decays to 0
within the window when idle. Retention stays at 60s.

* docs(changelog): note trailing-window throughput rate (PR #948)
2026-06-02 12:17:10 +03:00

50 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it, beforeEach, vi, afterEach } from 'vitest';
import {
getAnalysisTracksPerMinute,
recordAnalysisTrackPerf,
resetAnalysisPerfStateForTest,
} from './analysisPerfStore';
beforeEach(() => {
resetAnalysisPerfStateForTest();
});
afterEach(() => {
vi.useRealTimers();
});
describe('analysisPerfStore', () => {
const record = (trackId: string): void =>
recordAnalysisTrackPerf({ trackId, fetchMs: 1, seedMs: 1, bpmMs: 1, totalMs: 3 });
it('records last track timings and trailing-window tpm', () => {
vi.useFakeTimers();
vi.setSystemTime(1_000_000);
record('t1');
vi.advanceTimersByTime(100);
// 1 completion in the trailing 5s → 1 / 5s × 60 = 12 tpm.
expect(getAnalysisTracksPerMinute()).toBeCloseTo(12, 0);
});
it('extrapolates a burst over the trailing 5s window', () => {
vi.useFakeTimers();
vi.setSystemTime(2_000_000);
record('a');
vi.advanceTimersByTime(1_000);
record('b');
vi.advanceTimersByTime(1_000);
record('c');
// 3 completions within the last 5s → 3 / 5s × 60 = 36 tpm.
expect(getAnalysisTracksPerMinute()).toBeCloseTo(36, 0);
});
it('decays to 0 once completions fall outside the trailing window', () => {
vi.useFakeTimers();
vi.setSystemTime(3_000_000);
record('old');
expect(getAnalysisTracksPerMinute()).toBeGreaterThan(0);
vi.advanceTimersByTime(6_000);
expect(getAnalysisTracksPerMinute()).toBe(0);
});
});