main
1// Write-to-visible lag. After a successful conditional put, FlagControl invalidates the one cache tag.
2// The next read revalidates and observes the new ETag. Without that invalidate, a fresh hit keeps the old revision
3// until max-age. This module measures both with a model of that cache; it does not talk to the network.
4import { setRollout } from './api.ts';
5import { percentile } from './report.ts';
6import { memoryStore, type FlagStore, type InvalidateResult } from './store.ts';
7import { ABSENT_REVISION, FLAGS_MAX_AGE_S, FLAGS_OBJECT_KEY, FLAGS_SWR_S } from './ttl.ts';
8import { basisPointsOf } from './types.ts';
10export type CacheView = { readonly revision: string | null; readonly cache: 'HIT' | 'REVALIDATED' | 'MISS' };
12type Entry = { revision: string; storedAt: number; invalidated: boolean };
14export class RulesetCache {
15 private entry: Entry | null = null;
17 constructor(
18 private readonly readRevision: () => Promise<string | null>,
19 private readonly now: () => number,
20 private readonly maxAgeMs: number,
21 private readonly swrMs: number,
22 ) {}
24 invalidate(): InvalidateResult {
25 if (this.entry) this.entry.invalidated = true;
26 return { kind: 'ok' };
27 }
29 async read(): Promise<CacheView> {
30 const now = this.now();
31 const entry = this.entry;
32 const age = entry ? now - entry.storedAt : Number.POSITIVE_INFINITY;
33 if (entry && !entry.invalidated && age < this.maxAgeMs) return { revision: entry.revision, cache: 'HIT' };
34 const revision = await this.readRevision();
35 if (!revision) {
36 this.entry = null;
37 return { revision: null, cache: 'MISS' };
38 }
39 const revalidated = entry !== null && entry.revision === revision;
40 this.entry = { revision, storedAt: now, invalidated: false };
41 return { revision, cache: revalidated ? 'REVALIDATED' : 'MISS' };
42 }
44 get bounds(): { readonly maxAgeMs: number; readonly swrMs: number } {
45 return { maxAgeMs: this.maxAgeMs, swrMs: this.swrMs };
46 }
47}
49export type PropagationRound = { readonly lagMs: number; readonly from: string; readonly to: string };
51async function storedRevision(store: FlagStore): Promise<string | null> {
52 const stored = await store.get(FLAGS_OBJECT_KEY);
53 if (stored === null) return null;
54 return stored.etag;
55}
57export async function measureRounds(rounds: number, now: () => number = () => performance.now()): Promise<{ readonly rounds: readonly PropagationRound[]; readonly p50: number | null; readonly p95: number | null; readonly p99: number | null; readonly staleWithoutInvalidate: boolean }> {
58 const store = memoryStore();
59 const cache = new RulesetCache(async () => storedRevision(store), now, FLAGS_MAX_AGE_S * 1000, FLAGS_SWR_S * 1000);
60 let revision: string = ABSENT_REVISION;
61 const created = await setRollout(store, { invalidate: async () => cache.invalidate() }, 'ply.usage.note', 0, revision);
62 if (created.kind !== 'ok') throw new Error(`seed failed: ${created.kind}`);
63 revision = created.revision;
64 await cache.read();
65 const samples: PropagationRound[] = [];
66 for (let i = 0; i < rounds; i++) {
67 const basisPoints = i + 1;
68 const before = revision;
69 const started = now();
70 const written = await setRollout(store, { invalidate: async () => cache.invalidate() }, 'ply.usage.note', basisPoints, revision);
71 if (written.kind !== 'ok') throw new Error(`write failed: ${written.kind}`);
72 revision = written.revision;
73 let seen = await cache.read();
74 while (seen.revision !== revision) seen = await cache.read();
75 samples.push({ lagMs: now() - started, from: before, to: revision });
76 }
77 const held = memoryStore();
78 const quiet = new RulesetCache(async () => storedRevision(held), now, FLAGS_MAX_AGE_S * 1000, FLAGS_SWR_S * 1000);
79 const first = await setRollout(held, { invalidate: async () => quiet.invalidate() }, 'ply.usage.note', 1, ABSENT_REVISION);
80 if (first.kind !== 'ok') throw new Error('hold seed failed');
81 await quiet.read();
82 const second = await setRollout(held, { invalidate: async () => ({ kind: 'ok' }) }, 'ply.usage.note', 5, first.revision);
83 if (second.kind !== 'ok') throw new Error('hold write failed');
84 const stale = await quiet.read();
85 const lags = samples.map(sample => sample.lagMs);
86 return {
87 rounds: samples,
88 p50: percentile(lags, 0.5),
89 p95: percentile(lags, 0.95),
90 p99: percentile(lags, 0.99),
91 staleWithoutInvalidate: stale.cache === 'HIT' && stale.revision === first.revision && basisPointsOf(second.config.flags[0]!.rollout) === 5,
92 };
93}
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B5 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=5 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit5 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=5 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=5 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit7 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=7 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2001.5 KiB · hit17 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=17 git.response.bytes=1584 git.response.cache=HIT git.response.duration_ms=17 git.fetch.wants=afb44f680037dbfc4489afcd8214fbe2420bb2d7 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1584 git.pkt.payload.max=1563 git.sideband.band1.bytes=1562 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e310a788b3bdc9
- Request
- 4fa205f8…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format