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

opened this in answer to

Merged 11h 11m ago, live in production

scout/forge:scout/17713-test-traffic-truth-test-ts-fails-in-abou → main 0 commits fork

  • Merged. scout/17713-test-traffic-truth-test-ts-fails-in-abou was merged into main as 8692f40.

Review

Sign in to review this change.
3 files changed +12 −10
src/perf/traffic.ts +5 −4
@@ -197,8 +197,8 @@ function bump(
197197 if (anonymous) row.anonymous++;
198198 }
199199
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;
202202 const index = hash >>> (32 - SKETCH_BITS);
203203 const rank = Math.clz32((hash << SKETCH_BITS) >>> 0) + 1;
204204 if (rank > sketch[index]!) sketch[index] = rank;
@@ -250,7 +250,8 @@ function largest(table: Map<string, Row>, limit: number): TopRow[] {
250250 return [...rows.slice(0, limit), rest];
251251 }
252252
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 {
254255 const buckets = new Map<number, Bucket>();
255256 const slot = (minute: number): Bucket | undefined => {
256257 const index = minute % TRAFFIC_MINUTES;
@@ -300,7 +301,7 @@ export function createTrafficLedger(): TrafficLedger {
300301 return;
301302 }
302303 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);
304305 },
305306 totals(now) {
306307 const newest = Math.floor(now / MINUTE_MS);
test/traffic-history.test.ts +4 −4
@@ -66,7 +66,7 @@ const count = (t: string): number =>
6666
6767 test("a finished minute is written once, survives the process, and keeps no address", () => {
6868 scratch();
69 const ledger = createTrafficLedger();
69 const ledger = createTrafficLedger(1); // fixed salt: the address count is an estimate and must not depend on chance
7070 let now = START + 5_000;
7171 const writer = createTrafficWriter(app.db, ledger, quiet, () => now, 3_600_000);
7272 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
102102
103103 test("the running minute counts once, written or not, and two processes in a minute sum", () => {
104104 scratch();
105 const ledger = createTrafficLedger();
105 const ledger = createTrafficLedger(1);
106106 const now = START + 5_000;
107107 const writer = createTrafficWriter(app.db, ledger, quiet, () => now, 3_600_000);
108108 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
112112 note(ledger, now, "other", "203.0.113.9"); // late, into a minute already written
113113 expect(trafficHistory(app.db, ledger, now, "15m").classes.other.requests).toBe(2);
114114
115 const next = createTrafficLedger();
115 const next = createTrafficLedger(1);
116116 const second = createTrafficWriter(app.db, next, quiet, () => now, 3_600_000);
117117 note(next, now, "other", "203.0.113.9");
118118 second.stop();
@@ -160,7 +160,7 @@ test("what a request is sorted by: internal, country and signature", () => {
160160 `none | b${"x".repeat(37)} | no referer | no fetch-site`,
161161 );
162162
163 const ledger = createTrafficLedger();
163 const ledger = createTrafficLedger(1);
164164 ledger.note(START, 5, "internal", false, { address: "127.0.0.1", family: "bun", repo: "alice/open", signature: "s" });
165165 const tops = ledger.tops(START);
166166 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
136136 });
137137
138138 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);
140141 for (let i = 0; i < 1792; i++)
141142 ledger.note(END, 1, "deep", false, {
142143 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
153154 { key: "chrome", requests: 1792, ms: 1792, anonymous: 1792 },
154155 ]);
155156
156 const few = createTrafficLedger();
157 const few = createTrafficLedger(1);
157158 for (const address of ["10.0.0.1", "10.0.0.2", "10.0.0.2", "10.0.0.3"])
158159 few.note(END, 1, "other", false, { address, family: "curl", repo: null });
159160 expect(few.totals(END).addresses).toBe(3);

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs