Public: anyone can read this; the crew can change it.
test/traffic-truth.test.ts fails in about 0.29% of processes: three addresses share a sketch slot #17714
Merged 11h 11m ago, live in production
scout/forge:scout/17713-test-traffic-truth-test-ts-fails-in-abou → main 0 commits fork
3 files
changed +12
−10
src/perf/traffic.ts +5 −4
| @@ -197,8 +197,8 @@ function bump( | ||
| 197 | 197 | if (anonymous) row.anonymous++; |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | function sketchAdd(sketch: Uint8Array, bytes: Uint8Array): void { | |
| 201 | const hash = Number(Bun.hash.xxHash32(bytes, SALT)) >>> 0; | |
| 200 | function sketchAdd(sketch: Uint8Array, bytes: Uint8Array, salt: number): void { | |
| 201 | const hash = Number(Bun.hash.xxHash32(bytes, salt)) >>> 0; | |
| 202 | 202 | const index = hash >>> (32 - SKETCH_BITS); |
| 203 | 203 | const rank = Math.clz32((hash << SKETCH_BITS) >>> 0) + 1; |
| 204 | 204 | if (rank > sketch[index]!) sketch[index] = rank; |
| @@ -250,7 +250,8 @@ function largest(table: Map<string, Row>, limit: number): TopRow[] { | ||
| 250 | 250 | return [...rows.slice(0, limit), rest]; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | export function createTrafficLedger(): TrafficLedger { | |
| 253 | /** The salt is random per process; a test passes a fixed one so its counts are exact. */ | |
| 254 | export function createTrafficLedger(salt: number = SALT): TrafficLedger { | |
| 254 | 255 | const buckets = new Map<number, Bucket>(); |
| 255 | 256 | const slot = (minute: number): Bucket | undefined => { |
| 256 | 257 | const index = minute % TRAFFIC_MINUTES; |
| @@ -300,7 +301,7 @@ export function createTrafficLedger(): TrafficLedger { | ||
| 300 | 301 | return; |
| 301 | 302 | } |
| 302 | 303 | bump(bucket.prefixes, prefixOf(bytes), ms, anonymous); |
| 303 | sketchAdd((bucket.sketches[cls] ??= new Uint8Array(SKETCH_SIZE)), bytes); | |
| 304 | sketchAdd((bucket.sketches[cls] ??= new Uint8Array(SKETCH_SIZE)), bytes, salt); | |
| 304 | 305 | }, |
| 305 | 306 | totals(now) { |
| 306 | 307 | const newest = Math.floor(now / MINUTE_MS); |
test/traffic-history.test.ts +4 −4
| @@ -66,7 +66,7 @@ const count = (t: string): number => | ||
| 66 | 66 | |
| 67 | 67 | test("a finished minute is written once, survives the process, and keeps no address", () => { |
| 68 | 68 | scratch(); |
| 69 | const ledger = createTrafficLedger(); | |
| 69 | const ledger = createTrafficLedger(1); // fixed salt: the address count is an estimate and must not depend on chance | |
| 70 | 70 | let now = START + 5_000; |
| 71 | 71 | const writer = createTrafficWriter(app.db, ledger, quiet, () => now, 3_600_000); |
| 72 | 72 | note(ledger, now, "deep", "203.0.113.9"); |
| @@ -102,7 +102,7 @@ test("a finished minute is written once, survives the process, and keeps no addr | ||
| 102 | 102 | |
| 103 | 103 | test("the running minute counts once, written or not, and two processes in a minute sum", () => { |
| 104 | 104 | scratch(); |
| 105 | const ledger = createTrafficLedger(); | |
| 105 | const ledger = createTrafficLedger(1); | |
| 106 | 106 | const now = START + 5_000; |
| 107 | 107 | const writer = createTrafficWriter(app.db, ledger, quiet, () => now, 3_600_000); |
| 108 | 108 | note(ledger, now, "other", "203.0.113.9"); |
| @@ -112,7 +112,7 @@ test("the running minute counts once, written or not, and two processes in a min | ||
| 112 | 112 | note(ledger, now, "other", "203.0.113.9"); // late, into a minute already written |
| 113 | 113 | expect(trafficHistory(app.db, ledger, now, "15m").classes.other.requests).toBe(2); |
| 114 | 114 | |
| 115 | const next = createTrafficLedger(); | |
| 115 | const next = createTrafficLedger(1); | |
| 116 | 116 | const second = createTrafficWriter(app.db, next, quiet, () => now, 3_600_000); |
| 117 | 117 | note(next, now, "other", "203.0.113.9"); |
| 118 | 118 | second.stop(); |
| @@ -160,7 +160,7 @@ test("what a request is sorted by: internal, country and signature", () => { | ||
| 160 | 160 | `none | b${"x".repeat(37)} | no referer | no fetch-site`, |
| 161 | 161 | ); |
| 162 | 162 | |
| 163 | const ledger = createTrafficLedger(); | |
| 163 | const ledger = createTrafficLedger(1); | |
| 164 | 164 | ledger.note(START, 5, "internal", false, { address: "127.0.0.1", family: "bun", repo: "alice/open", signature: "s" }); |
| 165 | 165 | const tops = ledger.tops(START); |
| 166 | 166 | expect(tops.families[0]?.key).toBe("bun"); |
test/traffic-truth.test.ts +3 −2
| @@ -136,7 +136,8 @@ test("a minute past the window falls out, and a bucket is reused for the next qu | ||
| 136 | 136 | }); |
| 137 | 137 | |
| 138 | 138 | test("it keeps prefixes and a sketch, never an address, and its tables are bounded", () => { |
| 139 | const ledger = createTrafficLedger(); | |
| 139 | // A fixed salt: the sketch is an estimate, and a random one made this test fail now and then. | |
| 140 | const ledger = createTrafficLedger(1); | |
| 140 | 141 | for (let i = 0; i < 1792; i++) |
| 141 | 142 | ledger.note(END, 1, "deep", false, { |
| 142 | 143 | address: `198.${(i >> 8) & 255}.${i & 255}.7`, |
| @@ -153,7 +154,7 @@ test("it keeps prefixes and a sketch, never an address, and its tables are bound | ||
| 153 | 154 | { key: "chrome", requests: 1792, ms: 1792, anonymous: 1792 }, |
| 154 | 155 | ]); |
| 155 | 156 | |
| 156 | const few = createTrafficLedger(); | |
| 157 | const few = createTrafficLedger(1); | |
| 157 | 158 | for (const address of ["10.0.0.1", "10.0.0.2", "10.0.0.2", "10.0.0.3"]) |
| 158 | 159 | few.note(END, 1, "other", false, { address, family: "curl", repo: null }); |
| 159 | 160 | expect(few.totals(END).addresses).toBe(3); |
new above
offline