Files
supabase/apps/studio/components/interfaces/QueryPerformance/QueryPerformance.utils.test.ts
kemal.earth e2764f2675 fix(studio): average p95 calculation on query performance (#39725)
* fix: p95 calculation

Something seemed off so had another stab at it using resp_calls

* feat: simplified weighted avg p95

* feat: tweak it slightly

* feat: try one with a fallback

* feat: add test for percentile calculating
2025-10-27 13:07:42 +00:00

46 lines
1.6 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { formatDuration } from './QueryPerformance.utils'
import { calculatePercentilesFromHistogram } from './WithMonitor/WithMonitor.utils'
describe('formatDuration', () => {
it('should format seconds', () => {
expect(formatDuration(1000)).toBe('1.00s')
expect(formatDuration(30000)).toBe('30.00s')
})
it('should format minutes and seconds', () => {
expect(formatDuration(60000)).toBe('1m')
expect(formatDuration(125000)).toBe('2m 5s')
})
it('should format hours, minutes and seconds', () => {
expect(formatDuration(3600000)).toBe('1h')
expect(formatDuration(3661000)).toBe('1h 1m 1s')
})
it('should format days, hours, minutes and seconds', () => {
expect(formatDuration(86400000)).toBe('1d')
expect(formatDuration(90061000)).toBe('1d 1h 1m 1s')
})
})
describe('calculatePercentilesFromHistogram', () => {
it('should return zero for empty histogram', () => {
const result = calculatePercentilesFromHistogram([])
expect(result.p95).toBe(0)
})
it('should return valid p95 for typical distribution', () => {
const result = calculatePercentilesFromHistogram([10, 20, 30, 20, 10, 10])
expect(result.p95).toBeGreaterThan(0)
expect(result.p95).toBeGreaterThanOrEqual(result.p50)
})
it('should return consistent p95 for same input', () => {
const histogram = [10, 20, 30, 20, 10, 10]
const result1 = calculatePercentilesFromHistogram(histogram)
const result2 = calculatePercentilesFromHistogram(histogram)
expect(result1.p95).toBe(result2.p95)
})
})