g1t/apps/web/app/lib/perf.test.ts

101 lines4,464 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import {
5 PRIMARY_WINDOW_SECONDS,
6 bookmarkCookie,
7 coveredMs,
8 mayWrite,
9 metricName,
10 readBookmarks,
11 rpcMethodOf,
12 serverTiming,
13 serviceDuration,
14 databaseTime,
15 sessionFor,
16 writeBookmarks,
17} from "./perf.ts";
18
19const BOOKMARK = "0000002c-00000004-00004f95-c7f4a9b2e8d1f0c3b6a5d4e3f2a1b0c9";
20
21test("bookmarks survive the cookie", () => {
22 const value = writeBookmarks({ at: 1_800_000_000, services: { work: BOOKMARK, repos: BOOKMARK } });
23 assert.equal(value, `at:1800000000~repos:${BOOKMARK}~work:${BOOKMARK}`);
24 assert.deepEqual(readBookmarks(`g1t_session=abc; g1t_d1=${value}; other=1`), {
25 at: 1_800_000_000,
26 services: { repos: BOOKMARK, work: BOOKMARK },
27 });
28});
29
30test("a malformed or foreign cookie entry is dropped", () => {
31 assert.deepEqual(readBookmarks(null), { at: null, services: {} });
32 assert.deepEqual(readBookmarks("g1t_d1=at:soon~work:bad bookmark~actions:abc~repos:" + BOOKMARK), {
33 at: null,
34 services: { repos: BOOKMARK },
35 });
36 // Only a service that reads with sessions is kept.
37 assert.equal(writeBookmarks({ at: null, services: { actions: BOOKMARK } }), "");
38});
39
40test("the cookie is HttpOnly, short-lived and Secure over HTTPS", () => {
41 const cookie = bookmarkCookie({ at: 1, services: {} }, true);
42 assert.match(cookie, /^g1t_d1=at:1; Path=\/; HttpOnly; Secure; SameSite=Lax; Max-Age=300$/);
43 assert.doesNotMatch(bookmarkCookie({ at: 1, services: {} }, false), /Secure/);
44});
45
46test("what each call asks for", () => {
47 const now = 1_800_000_000;
48 const none = { at: null, services: {} };
49 // A service without sessions is left alone: its primary, as before.
50 assert.equal(sessionFor("actions", none, false, now), null);
51 // A form post reads the primary everywhere.
52 assert.equal(sessionFor("work", { at: null, services: { work: BOOKMARK } }, true, now), "first-primary");
53 // A page with no recent write reads the nearest copy.
54 assert.equal(sessionFor("work", none, false, now), "first-unconstrained");
55 // Just after a write, every service reads its primary, bookmark or not.
56 const justWrote = { at: now - 5, services: { work: BOOKMARK } };
57 assert.equal(sessionFor("work", justWrote, false, now), "first-primary");
58 assert.equal(sessionFor("repos", justWrote, false, now), "first-primary");
59 // Later, the bookmark keeps the read at least as new as the write.
60 const later = { at: now - PRIMARY_WINDOW_SECONDS, services: { work: BOOKMARK } };
61 assert.equal(sessionFor("work", later, false, now), BOOKMARK);
62 assert.equal(sessionFor("repos", later, false, now), "first-unconstrained");
63});
64
65test("only known reads are taken not to write", () => {
66 for (const method of ["get_pull", "list_pulls", "counts", "user_for_session", "explore", "usage", "get", "list", "queue", "pulls_for_repos"]) {
67 assert.equal(mayWrite(method), false, method);
68 }
69 for (const method of ["merge_pull", "verify_email", "github_finish", "sign_in", "something_new"]) {
70 assert.equal(mayWrite(method), true, method);
71 }
72 assert.equal(rpcMethodOf("https://service/rpc/get_pull"), "get_pull");
73 assert.equal(rpcMethodOf("https://service/other"), "");
74});
75
76test("timings", () => {
77 assert.equal(serviceDuration('svc;dur=12;desc="session"'), 12);
78 assert.equal(serviceDuration("repo;dur=3, svc;dur=7.5"), 7.5);
79 assert.equal(serviceDuration(null), null);
80 assert.deepEqual(databaseTime('svc;dur=40;desc="session", db;dur=22;desc="2 round trips, 21 statements", rpc;dur=18;desc="1 calls"'), { ms: 22, trips: 2 });
81 assert.equal(databaseTime('svc;dur=40;desc="session"'), null);
82 // Overlapping calls are counted once.
83 assert.equal(coveredMs([[0, 100], [50, 120], [200, 210]]), 130);
84 assert.equal(coveredMs([]), 0);
85 assert.equal(metricName("routes/repo/pull"), "repo.pull");
86 const header = serverTiming({
87 totalMs: 180,
88 loaders: [{ id: "routes/repo/pull", ms: 150, kind: "loader" }],
89 rpcMs: 140,
90 services: {
91 work: { calls: 3, wallMs: 120, serviceMs: 90, dbMs: 40, dbTrips: 3 },
92 repos: { calls: 1, wallMs: 30, serviceMs: 0 },
93 },
94 sessions: "work=unconstrained",
95 });
96 assert.equal(
97 header,
98 'total;dur=180;desc="web to first byte", loader.repo.pull;dur=150, rpc;dur=140;desc="4 service calls, overlap counted once", ' +
99 'work;dur=120;desc="3 calls, 90ms inside, db 40ms in 3 round trips", repos;dur=30;desc="1 call", d1;desc="work=unconstrained"',
100 );
101});