:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { Counter, register } from 'prom-client'
import { RefDedupCache, registerNativeRefDedupCache } from './ref-dedup-cache'
describe('RefDedupCache', () => {
async function probe(cache: string, verdict: 'would_hit' | 'would_miss'): Promise<number> {
const metric = register.getSingleMetric('ml_mirror_ref_cache_capacity_probe_total') as Counter | undefined
const data = await metric?.get()
const sample = data?.values.find((v) => v.labels.cache === cache && v.labels.verdict === verdict)
return sample?.value ?? 0
}
it('separates misses a bigger cache would have caught from ones it would not', async () => {
// The whole point of the probe is to tell "the working set does not fit" apart from "these
// duplicates were always too far apart to cache". Both verdicts have to move: a probe stuck
// on would_miss reads as a correctly sized cache and we stop looking, and one stuck on
// would_hit sends us buying memory that buys nothing. Sample every ref so it is deterministic.
const name = 'test_capacity_probe'
const cache = new RefDedupCache(name, 2, 1)
cache.add('a')
cache.add('b')
cache.add('c') // evicts 'a'
expect(cache.has('a')).toBe(false)
expect(await probe(name, 'would_hit')).toBe(1)
expect(cache.has('never-seen')).toBe(false)
expect(await probe(name, 'would_miss')).toBe(1)
expect(await probe(name, 'would_hit')).toBe(1)
})
it('credits one eviction with one verdict, however often the evicted ref is probed', async () => {
// Reading the ghost through `get` renews its recency, so a ref that is probed but never
// re-added (the sidecar rejected it, or its produce failed) pins itself and scores would_hit
// on every later batch. That inflates the ratio in the "buy more memory" direction, on the
// one metric whose whole job is deciding whether to spend memory.
const name = 'test_repeat_probe'
const cache = new RefDedupCache(name, 2, 1)
cache.add('a')
cache.add('b')
cache.add('c') // evicts 'a'
for (let i = 0; i < 5; i++) {
expect(cache.has('a')).toBe(false)
}
expect(await probe(name, 'would_hit')).toBe(1)
expect(await probe(name, 'would_miss')).toBe(4)
})
it.each([
['a typo parsed to NaN', NaN],
['the source default copied verbatim, which parseFloat truncates', 1.5],
['a negative that would otherwise silently disable dedup', -1],
])('rejects %s at construction rather than at first use', (_case, max) => {
// lru-cache throws on these too, but names neither the cache nor the knob, so the lane
// crash-loops on a message an operator cannot act on.
expect(() => new RefDedupCache('test_validation', max)).toThrow(/test_validation ref cache max/)
})
it('reports the running totals of a native cache once, however often it is scraped', async () => {
// The addon keeps running totals, and the counter is fed what grew since the last scrape. A
// counter fed the whole total on every scrape would grow with the scrape count instead.
const name = 'test_native_totals'
const stats = { entries: 3, evictions: 0, wouldHit: 1, wouldMiss: 2 }
registerNativeRefDedupCache(name, 10, () => stats)
expect(await probe(name, 'would_hit')).toBe(1)
expect(await probe(name, 'would_miss')).toBe(2)
stats.wouldMiss = 5
expect(await probe(name, 'would_hit')).toBe(1)
expect(await probe(name, 'would_miss')).toBe(5)
})
it('does not probe while the cache still has room, so an unfilled cache reads as sized correctly', async () => {
// Evictions are the only thing that can put a ref in the ghost list, so a cache that never
// fills must report no would_hit at all rather than blaming capacity for ordinary misses.
const name = 'test_unfilled'
const cache = new RefDedupCache(name, 100, 1)
cache.add('a')
expect(cache.has('b')).toBe(false)
expect(await probe(name, 'would_hit')).toBe(0)
expect(await probe(name, 'would_miss')).toBe(1)
})
})