|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + PerfTiming, |
| 6 | + formatServerTiming, |
| 7 | + runWithPerfTiming, |
| 8 | + currentPerfTiming, |
| 9 | + recordServerTiming, |
| 10 | + startServerTiming, |
| 11 | + measureServerTiming, |
| 12 | +} from './perf-timing.js'; |
| 13 | + |
| 14 | +describe('formatServerTiming', () => { |
| 15 | + it('serializes name + duration', () => { |
| 16 | + expect(formatServerTiming([{ name: 'db', dur: 12.3 }])).toBe('db;dur=12.3'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('rounds duration to 2 decimals', () => { |
| 20 | + expect(formatServerTiming([{ name: 'db', dur: 12.34567 }])).toBe('db;dur=12.35'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('emits a quoted desc when present', () => { |
| 24 | + expect(formatServerTiming([{ name: 'total', dur: 5, desc: 'Total time' }])).toBe( |
| 25 | + 'total;dur=5;desc="Total time"', |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it('joins multiple marks with comma-space', () => { |
| 30 | + expect( |
| 31 | + formatServerTiming([ |
| 32 | + { name: 'parse', dur: 1 }, |
| 33 | + { name: 'handler', dur: 4 }, |
| 34 | + ]), |
| 35 | + ).toBe('parse;dur=1, handler;dur=4'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('sanitizes names into tokens', () => { |
| 39 | + expect(formatServerTiming([{ name: 'db query!', dur: 1 }])).toBe('db_query;dur=1'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('drops marks whose name is empty after sanitization', () => { |
| 43 | + expect(formatServerTiming([{ name: '!!!', dur: 1 }])).toBe(''); |
| 44 | + }); |
| 45 | + |
| 46 | + it('strips quotes/backslashes/control chars from desc (no header injection)', () => { |
| 47 | + const out = formatServerTiming([ |
| 48 | + { name: 'x', dur: 1, desc: 'a"b\\c\r\nInjected: 1' }, |
| 49 | + ]); |
| 50 | + expect(out).toBe('x;dur=1;desc="a b c Injected: 1"'); |
| 51 | + expect(out).not.toContain('\n'); |
| 52 | + expect(out).not.toContain('"a"b"'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('coerces non-finite durations to 0', () => { |
| 56 | + expect(formatServerTiming([{ name: 'x', dur: Number.NaN }])).toBe('x;dur=0'); |
| 57 | + expect(formatServerTiming([{ name: 'x', dur: Number.POSITIVE_INFINITY }])).toBe('x;dur=0'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns empty string for no marks', () => { |
| 61 | + expect(formatServerTiming([])).toBe(''); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('PerfTiming', () => { |
| 66 | + it('records explicit marks in order', () => { |
| 67 | + const t = new PerfTiming(); |
| 68 | + t.record('a', 1); |
| 69 | + t.record('b', 2); |
| 70 | + expect(t.marks().map((m) => m.name)).toEqual(['a', 'b']); |
| 71 | + expect(t.toHeader()).toBe('a;dur=1, b;dur=2'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('start() returns an idempotent end()', () => { |
| 75 | + const t = new PerfTiming(); |
| 76 | + const end = t.start('phase'); |
| 77 | + end(); |
| 78 | + end(); // second call ignored |
| 79 | + expect(t.marks()).toHaveLength(1); |
| 80 | + expect(t.marks()[0].name).toBe('phase'); |
| 81 | + expect(t.marks()[0].dur).toBeGreaterThanOrEqual(0); |
| 82 | + }); |
| 83 | + |
| 84 | + it('measure() records duration and returns the value', async () => { |
| 85 | + const t = new PerfTiming(); |
| 86 | + const value = await t.measure('work', async () => { |
| 87 | + await new Promise((r) => setTimeout(r, 5)); |
| 88 | + return 42; |
| 89 | + }); |
| 90 | + expect(value).toBe(42); |
| 91 | + expect(t.marks()).toHaveLength(1); |
| 92 | + expect(t.marks()[0].dur).toBeGreaterThan(0); |
| 93 | + }); |
| 94 | + |
| 95 | + it('measure() records even when the function throws', async () => { |
| 96 | + const t = new PerfTiming(); |
| 97 | + await expect( |
| 98 | + t.measure('boom', async () => { |
| 99 | + throw new Error('nope'); |
| 100 | + }), |
| 101 | + ).rejects.toThrow('nope'); |
| 102 | + expect(t.marks()).toHaveLength(1); |
| 103 | + expect(t.marks()[0].name).toBe('boom'); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('ambient collector', () => { |
| 108 | + it('currentPerfTiming() is undefined outside a run scope', () => { |
| 109 | + expect(currentPerfTiming()).toBeUndefined(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('free functions are no-ops with no active collector', async () => { |
| 113 | + recordServerTiming('x', 1); // must not throw |
| 114 | + const end = startServerTiming('y'); |
| 115 | + end(); // must not throw |
| 116 | + const v = await measureServerTiming('z', async () => 7); |
| 117 | + expect(v).toBe(7); |
| 118 | + }); |
| 119 | + |
| 120 | + it('records onto the ambient collector inside runWithPerfTiming', async () => { |
| 121 | + const t = new PerfTiming(); |
| 122 | + await runWithPerfTiming(t, async () => { |
| 123 | + expect(currentPerfTiming()).toBe(t); |
| 124 | + recordServerTiming('db', 3, 'Database'); |
| 125 | + const v = await measureServerTiming('compute', async () => 'ok'); |
| 126 | + expect(v).toBe('ok'); |
| 127 | + }); |
| 128 | + const names = t.marks().map((m) => m.name); |
| 129 | + expect(names).toContain('db'); |
| 130 | + expect(names).toContain('compute'); |
| 131 | + expect(t.toHeader()).toContain('db;dur=3;desc="Database"'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('propagates across async boundaries', async () => { |
| 135 | + const t = new PerfTiming(); |
| 136 | + await runWithPerfTiming(t, async () => { |
| 137 | + await new Promise((r) => setTimeout(r, 1)); |
| 138 | + recordServerTiming('after-await', 1); |
| 139 | + }); |
| 140 | + expect(t.marks().map((m) => m.name)).toContain('after-await'); |
| 141 | + }); |
| 142 | +}); |
0 commit comments