@drupflare/spool 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1557 -2
- package/dist/cdn/client.js +1 -0
- package/dist/cdn/location.d.ts +11 -0
- package/dist/cdn/location.js +15 -0
- package/dist/cdn/manifest.json +16 -0
- package/dist/cdn/spool-sw.js +1 -0
- package/dist/client/entry.d.ts +1 -0
- package/dist/client/entry.js +25 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +2 -0
- package/dist/client/lifecycle.d.ts +10 -0
- package/dist/client/lifecycle.js +10 -0
- package/dist/client/loads.d.ts +16 -0
- package/dist/client/loads.js +41 -0
- package/dist/client/register.d.ts +34 -0
- package/dist/client/register.js +58 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/node/listener.d.ts +19 -0
- package/dist/node/listener.js +76 -0
- package/dist/protocol/artifact.d.ts +54 -0
- package/dist/protocol/artifact.js +79 -0
- package/dist/protocol/assembly.d.ts +34 -0
- package/dist/protocol/assembly.js +72 -0
- package/dist/protocol/beacon.d.ts +59 -0
- package/dist/protocol/beacon.js +86 -0
- package/dist/protocol/collector.d.ts +41 -0
- package/dist/protocol/collector.js +84 -0
- package/dist/protocol/config.d.ts +89 -0
- package/dist/protocol/config.js +13 -0
- package/dist/protocol/envelope.d.ts +47 -0
- package/dist/protocol/envelope.js +162 -0
- package/dist/protocol/errors.d.ts +7 -0
- package/dist/protocol/errors.js +9 -0
- package/dist/protocol/graph.d.ts +81 -0
- package/dist/protocol/graph.js +201 -0
- package/dist/protocol/index.d.ts +19 -0
- package/dist/protocol/index.js +19 -0
- package/dist/protocol/manifest.d.ts +29 -0
- package/dist/protocol/manifest.js +94 -0
- package/dist/protocol/normalise.d.ts +20 -0
- package/dist/protocol/normalise.js +29 -0
- package/dist/protocol/pattern.d.ts +12 -0
- package/dist/protocol/pattern.js +82 -0
- package/dist/protocol/provision.d.ts +100 -0
- package/dist/protocol/provision.js +105 -0
- package/dist/protocol/route.d.ts +179 -0
- package/dist/protocol/route.js +135 -0
- package/dist/protocol/sync.d.ts +31 -0
- package/dist/protocol/sync.js +87 -0
- package/dist/protocol/tag.d.ts +12 -0
- package/dist/protocol/tag.js +26 -0
- package/dist/protocol/template/compile.d.ts +35 -0
- package/dist/protocol/template/compile.js +82 -0
- package/dist/protocol/template/interpret.d.ts +9 -0
- package/dist/protocol/template/interpret.js +82 -0
- package/dist/protocol/vector.d.ts +32 -0
- package/dist/protocol/vector.js +56 -0
- package/dist/protocol/view.d.ts +110 -0
- package/dist/protocol/view.js +201 -0
- package/dist/proxy/eligible.d.ts +45 -0
- package/dist/proxy/eligible.js +96 -0
- package/dist/proxy/index.d.ts +4 -0
- package/dist/proxy/index.js +4 -0
- package/dist/proxy/proxy.d.ts +86 -0
- package/dist/proxy/proxy.js +388 -0
- package/dist/proxy/purge.d.ts +10 -0
- package/dist/proxy/purge.js +66 -0
- package/dist/proxy/tags.d.ts +8 -0
- package/dist/proxy/tags.js +36 -0
- package/dist/report/derive.d.ts +221 -0
- package/dist/report/derive.js +490 -0
- package/dist/report/economics.d.ts +14 -0
- package/dist/report/economics.js +24 -0
- package/dist/report/index.d.ts +8 -0
- package/dist/report/index.js +8 -0
- package/dist/report/inputs.d.ts +141 -0
- package/dist/report/inputs.js +216 -0
- package/dist/report/measurement.d.ts +48 -0
- package/dist/report/measurement.js +114 -0
- package/dist/report/model.d.ts +42 -0
- package/dist/report/model.js +74 -0
- package/dist/report/thirdparty.d.ts +62 -0
- package/dist/report/thirdparty.js +107 -0
- package/dist/report/traffic.d.ts +140 -0
- package/dist/report/traffic.js +228 -0
- package/dist/report/trim.d.ts +81 -0
- package/dist/report/trim.js +105 -0
- package/dist/server/beacon.d.ts +20 -0
- package/dist/server/beacon.js +47 -0
- package/dist/server/embedded.d.ts +6 -0
- package/dist/server/embedded.js +6 -0
- package/dist/server/ga4.d.ts +70 -0
- package/dist/server/ga4.js +165 -0
- package/dist/server/identity.d.ts +12 -0
- package/dist/server/identity.js +17 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +7 -0
- package/dist/server/sink.d.ts +56 -0
- package/dist/server/sink.js +133 -0
- package/dist/server/size.d.ts +7 -0
- package/dist/server/size.js +18 -0
- package/dist/server/spool.d.ts +97 -0
- package/dist/server/spool.js +463 -0
- package/dist/server/stub.d.ts +9 -0
- package/dist/server/stub.js +19 -0
- package/dist/server/sync.d.ts +12 -0
- package/dist/server/sync.js +48 -0
- package/dist/server/work.d.ts +22 -0
- package/dist/server/work.js +30 -0
- package/dist/stores/durable-object.d.ts +19 -0
- package/dist/stores/durable-object.js +53 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/index.js +2 -0
- package/dist/stores/memory.d.ts +4 -0
- package/dist/stores/memory.js +10 -0
- package/dist/stores/version-store.d.ts +11 -0
- package/dist/stores/version-store.js +1 -0
- package/dist/sw/analytics.d.ts +81 -0
- package/dist/sw/analytics.js +275 -0
- package/dist/sw/collect.d.ts +8 -0
- package/dist/sw/collect.js +33 -0
- package/dist/sw/core.d.ts +1 -0
- package/dist/sw/core.js +3 -0
- package/dist/sw/freshness.d.ts +15 -0
- package/dist/sw/freshness.js +20 -0
- package/dist/sw/graph/index.d.ts +67 -0
- package/dist/sw/graph/index.js +170 -0
- package/dist/sw/graph/invalidate.d.ts +23 -0
- package/dist/sw/graph/invalidate.js +109 -0
- package/dist/sw/graph/projections.d.ts +7 -0
- package/dist/sw/graph/projections.js +9 -0
- package/dist/sw/graph/records.d.ts +7 -0
- package/dist/sw/graph/records.js +9 -0
- package/dist/sw/graph/store.d.ts +32 -0
- package/dist/sw/graph/store.js +52 -0
- package/dist/sw/idb.d.ts +8 -0
- package/dist/sw/idb.js +117 -0
- package/dist/sw/index.d.ts +2 -0
- package/dist/sw/index.js +1 -0
- package/dist/sw/partition.d.ts +21 -0
- package/dist/sw/partition.js +137 -0
- package/dist/sw/policy.d.ts +8 -0
- package/dist/sw/policy.js +24 -0
- package/dist/sw/provision.d.ts +39 -0
- package/dist/sw/provision.js +61 -0
- package/dist/sw/render.d.ts +21 -0
- package/dist/sw/render.js +53 -0
- package/dist/sw/replica.d.ts +77 -0
- package/dist/sw/replica.js +299 -0
- package/dist/sw/router.d.ts +42 -0
- package/dist/sw/router.js +48 -0
- package/dist/sw/routing.d.ts +23 -0
- package/dist/sw/routing.js +48 -0
- package/dist/sw/runtime.d.ts +112 -0
- package/dist/sw/runtime.js +1070 -0
- package/dist/sw/scheduler.d.ts +83 -0
- package/dist/sw/scheduler.js +149 -0
- package/dist/sw/seal.d.ts +37 -0
- package/dist/sw/seal.js +183 -0
- package/dist/sw/serve.d.ts +15 -0
- package/dist/sw/serve.js +63 -0
- package/dist/sw/shadow.d.ts +16 -0
- package/dist/sw/shadow.js +27 -0
- package/dist/sw/static.d.ts +17 -0
- package/dist/sw/static.js +81 -0
- package/dist/sw/storage.d.ts +64 -0
- package/dist/sw/storage.js +34 -0
- package/dist/sw/thirdparty.d.ts +17 -0
- package/dist/sw/thirdparty.js +36 -0
- package/dist/trim/css.d.ts +38 -0
- package/dist/trim/css.js +328 -0
- package/dist/trim/index.d.ts +3 -0
- package/dist/trim/index.js +3 -0
- package/dist/trim/manifest.d.ts +67 -0
- package/dist/trim/manifest.js +134 -0
- package/dist/trim/tokens.d.ts +43 -0
- package/dist/trim/tokens.js +142 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +3 -0
- package/package.json +146 -5
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { classifyOrigin } from './thirdparty.js';
|
|
2
|
+
/** a window is dropped when the other containers on the host used more CPU than this, per second */
|
|
3
|
+
export const CONTAMINATION_MS_PER_S = 100;
|
|
4
|
+
/** the median of a non-empty list */
|
|
5
|
+
export function median(values) {
|
|
6
|
+
if (values.length === 0)
|
|
7
|
+
throw new RangeError('no values');
|
|
8
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
9
|
+
const mid = sorted.length >> 1;
|
|
10
|
+
return sorted.length % 2 === 1
|
|
11
|
+
? sorted[mid]
|
|
12
|
+
: (sorted[mid - 1] + sorted[mid]) / 2;
|
|
13
|
+
}
|
|
14
|
+
/** {@link median}, minimum, maximum and count of a non-empty list */
|
|
15
|
+
export function spread(values) {
|
|
16
|
+
return {
|
|
17
|
+
median: median(values),
|
|
18
|
+
min: Math.min(...values),
|
|
19
|
+
max: Math.max(...values),
|
|
20
|
+
n: values.length
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** the `p` quantile (0 to 1) by linear interpolation between the nearest ranks */
|
|
24
|
+
export function quantile(values, p) {
|
|
25
|
+
if (values.length === 0)
|
|
26
|
+
throw new RangeError('no values');
|
|
27
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
28
|
+
const at = (sorted.length - 1) * p;
|
|
29
|
+
const low = Math.floor(at);
|
|
30
|
+
const high = Math.ceil(at);
|
|
31
|
+
return (sorted[low] + (sorted[high] - sorted[low]) * (at - low));
|
|
32
|
+
}
|
|
33
|
+
/** ordinary least squares over at least two points with distinct x */
|
|
34
|
+
export function linearFit(points) {
|
|
35
|
+
const n = points.length;
|
|
36
|
+
const mx = points.reduce((s, [x]) => s + x, 0) / n;
|
|
37
|
+
const my = points.reduce((s, [, y]) => s + y, 0) / n;
|
|
38
|
+
const sxx = points.reduce((s, [x]) => s + (x - mx) ** 2, 0);
|
|
39
|
+
if (n < 2 || sxx === 0)
|
|
40
|
+
throw new RangeError('a fit needs two distinct x values');
|
|
41
|
+
const slope = points.reduce((s, [x, y]) => s + (x - mx) * (y - my), 0) / sxx;
|
|
42
|
+
const intercept = my - slope * mx;
|
|
43
|
+
const residual = points.reduce((s, [x, y]) => s + (y - (intercept + slope * x)) ** 2, 0);
|
|
44
|
+
const total = points.reduce((s, [, y]) => s + (y - my) ** 2, 0);
|
|
45
|
+
return { intercept, slope, r2: total === 0 ? 1 : 1 - residual / total };
|
|
46
|
+
}
|
|
47
|
+
const num = (row, key) => typeof row[key] === 'number' ? row[key] : undefined;
|
|
48
|
+
const str = (row, key) => String(row[key] ?? '');
|
|
49
|
+
const sumValues = (value) => typeof value === 'object' && value !== null
|
|
50
|
+
? Object.values(value).reduce((s, v) => s + v, 0)
|
|
51
|
+
: undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Joules per operation above idle for every arm of a RAPL run: each window's energy less the idle
|
|
54
|
+
* draw of its own round times its length, over its operations. A round without an idle window
|
|
55
|
+
* uses the median idle draw. Windows over the contamination limit are dropped, idle ones included.
|
|
56
|
+
*/
|
|
57
|
+
export function raplRun(rows, options = {}) {
|
|
58
|
+
const idleArm = options.idleArm ?? 'idle';
|
|
59
|
+
const limit = options.contamination ?? CONTAMINATION_MS_PER_S;
|
|
60
|
+
const seconds = (row) => num(row, 'elapsedMs') / 1000;
|
|
61
|
+
const other = (row) => num(row, 'otherCpuMs') ?? num(row, 'otherContainersCpuMs') ?? 0;
|
|
62
|
+
const kept = rows.filter((row) => other(row) / seconds(row) <= limit);
|
|
63
|
+
const idle = new Map();
|
|
64
|
+
for (const row of kept) {
|
|
65
|
+
if (str(row, 'arm') === idleArm) {
|
|
66
|
+
idle.set(num(row, 'round'), num(row, 'pkgJ') / seconds(row));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const idleWatts = spread([...idle.values()]);
|
|
70
|
+
const arms = {};
|
|
71
|
+
for (const row of kept) {
|
|
72
|
+
if (str(row, 'arm') !== idleArm)
|
|
73
|
+
(arms[str(row, 'arm')] ??= []).push(row);
|
|
74
|
+
}
|
|
75
|
+
const figures = {};
|
|
76
|
+
for (const [arm, windows] of Object.entries(arms)) {
|
|
77
|
+
const per = windows.map((row) => {
|
|
78
|
+
const ops = (num(row, 'navs') ?? num(row, 'n'));
|
|
79
|
+
const watts = idle.get(num(row, 'round')) ?? idleWatts.median;
|
|
80
|
+
const above = num(row, 'pkgJ') - watts * seconds(row);
|
|
81
|
+
const origin = num(row, 'originReqs') ?? sumValues(row.originHits);
|
|
82
|
+
const server = num(row, 'serverCpuMs');
|
|
83
|
+
const client = num(row, 'clientCpuMs');
|
|
84
|
+
return {
|
|
85
|
+
joules: above / ops,
|
|
86
|
+
ms: (seconds(row) * 1000) / ops,
|
|
87
|
+
origin: origin === undefined ? undefined : origin / ops,
|
|
88
|
+
originCpu: num(row, 'originCpuMs') === undefined
|
|
89
|
+
? undefined
|
|
90
|
+
: num(row, 'originCpuMs') / ops,
|
|
91
|
+
server: server === undefined ? undefined : server / ops,
|
|
92
|
+
client: client === undefined ? undefined : client / ops,
|
|
93
|
+
serverJ: server === undefined || client === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: (above * server) / (server + client) / ops
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
const defined = (pick) => per.map(pick).filter((v) => v !== undefined);
|
|
99
|
+
const origin = defined((p) => p.origin);
|
|
100
|
+
const originCpu = defined((p) => p.originCpu);
|
|
101
|
+
const server = defined((p) => p.server);
|
|
102
|
+
const client = defined((p) => p.client);
|
|
103
|
+
const serverJ = defined((p) => p.serverJ);
|
|
104
|
+
figures[arm] = {
|
|
105
|
+
windows: per.length,
|
|
106
|
+
joules: spread(per.map((p) => p.joules)),
|
|
107
|
+
ms: median(per.map((p) => p.ms)),
|
|
108
|
+
...(origin.length > 0 && { originRequests: median(origin) }),
|
|
109
|
+
...(originCpu.length > 0 && { originCpuMs: median(originCpu) }),
|
|
110
|
+
...(server.length > 0 && { serverCpuMs: median(server) }),
|
|
111
|
+
...(client.length > 0 && { clientCpuMs: median(client) }),
|
|
112
|
+
...(serverJ.length > 0 && { serverJoules: spread(serverJ) })
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return { idleWatts, dropped: rows.length - kept.length, arms: figures };
|
|
116
|
+
}
|
|
117
|
+
/** joules per operation `a` costs over `b`, from the medians of the two arms */
|
|
118
|
+
export function saving(a, b) {
|
|
119
|
+
return a.joules.median - b.joules.median;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* {@link InferenceFigures} from the rows of an inference run. A round needs its unloaded and
|
|
123
|
+
* loaded idle windows and its recompute window; rounds over the contamination limit are dropped.
|
|
124
|
+
*/
|
|
125
|
+
export function inferenceFigures(measurement) {
|
|
126
|
+
const cacheOps = Number(measurement.method['requests per cache window']);
|
|
127
|
+
const byRound = new Map();
|
|
128
|
+
for (const row of measurement.rows) {
|
|
129
|
+
const round = num(row, 'round');
|
|
130
|
+
const arms = byRound.get(round) ?? {};
|
|
131
|
+
arms[str(row, 'arm')] = row;
|
|
132
|
+
byRound.set(round, arms);
|
|
133
|
+
}
|
|
134
|
+
const s = (row) => num(row, 'elapsedMs') / 1000;
|
|
135
|
+
const cpuOf = (row) => (num(row, 'otherCpuMs') ?? 0) / s(row);
|
|
136
|
+
const cpu = [];
|
|
137
|
+
const gpu = [];
|
|
138
|
+
const latency = [];
|
|
139
|
+
const tokens = [];
|
|
140
|
+
const ollama = [];
|
|
141
|
+
const origin = [];
|
|
142
|
+
const warmCpu = [];
|
|
143
|
+
const warmGpu = [];
|
|
144
|
+
const cache = [];
|
|
145
|
+
for (const arms of byRound.values()) {
|
|
146
|
+
const unloaded = arms['idle-unloaded'];
|
|
147
|
+
const loaded = arms['idle-loaded'];
|
|
148
|
+
const run = arms['ai-recompute'];
|
|
149
|
+
if (!unloaded || !loaded || !run)
|
|
150
|
+
continue;
|
|
151
|
+
if ([unloaded, loaded, run].some((row) => cpuOf(row) > CONTAMINATION_MS_PER_S))
|
|
152
|
+
continue;
|
|
153
|
+
const calls = num(run, 'aiCalls');
|
|
154
|
+
const loadedWatts = num(loaded, 'pkgJ') / s(loaded);
|
|
155
|
+
const loadedGpuWatts = num(loaded, 'gpuJ') / s(loaded);
|
|
156
|
+
cpu.push((num(run, 'pkgJ') - loadedWatts * s(run)) / calls);
|
|
157
|
+
gpu.push((num(run, 'gpuJ') - loadedGpuWatts * s(run)) / calls);
|
|
158
|
+
latency.push(...run.latMs.map((ms) => ms / 1000));
|
|
159
|
+
tokens.push(num(run, 'aiOutTokens') / calls);
|
|
160
|
+
ollama.push(num(run, 'ollamaCpuMs') / calls);
|
|
161
|
+
origin.push(num(run, 'serverCpuMs') / calls);
|
|
162
|
+
warmCpu.push(loadedWatts - num(unloaded, 'pkgJ') / s(unloaded));
|
|
163
|
+
warmGpu.push(loadedGpuWatts - num(unloaded, 'gpuJ') / s(unloaded));
|
|
164
|
+
const hit = arms['ai-cache'];
|
|
165
|
+
if (hit && Number.isFinite(cacheOps)) {
|
|
166
|
+
cache.push((num(hit, 'pkgJ') - loadedWatts * s(hit)) / cacheOps);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
rounds: cpu.length,
|
|
171
|
+
cpuJoules: spread(cpu),
|
|
172
|
+
gpuJoules: spread(gpu),
|
|
173
|
+
totalJoules: spread(cpu.map((v, i) => v + gpu[i])),
|
|
174
|
+
latencyS: spread(latency),
|
|
175
|
+
outputTokens: median(tokens),
|
|
176
|
+
ollamaCpuMs: median(ollama),
|
|
177
|
+
originCpuMs: median(origin),
|
|
178
|
+
keepWarmCpuWatts: spread(warmCpu),
|
|
179
|
+
keepWarmGpuWatts: spread(warmGpu),
|
|
180
|
+
cacheHitJoules: cache.length > 0 ? spread(cache) : { median: NaN, min: NaN, max: NaN, n: 0 }
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/** {@link PowerSweep} for the rows with the given `set`, `sweep` or `timer` */
|
|
184
|
+
export function powerSweep(rows, set) {
|
|
185
|
+
const mine = rows.filter((row) => row.set === set);
|
|
186
|
+
const idle = spread(mine.filter((r) => str(r, 'cond') === 'idle').map((r) => num(r, 'pkgW')));
|
|
187
|
+
const groups = new Map();
|
|
188
|
+
for (const row of mine) {
|
|
189
|
+
if (str(row, 'cond').startsWith('idle'))
|
|
190
|
+
continue;
|
|
191
|
+
const list = groups.get(str(row, 'cond')) ?? [];
|
|
192
|
+
list.push(row);
|
|
193
|
+
groups.set(str(row, 'cond'), list);
|
|
194
|
+
}
|
|
195
|
+
const conditions = [...groups].map(([cond, list]) => {
|
|
196
|
+
const first = list[0];
|
|
197
|
+
const mj = list
|
|
198
|
+
.filter((r) => num(r, 'reqs') > 0)
|
|
199
|
+
.map((r) => (1000 * (num(r, 'pkgW') - idle.median)) /
|
|
200
|
+
(num(r, 'reqs') / num(r, 'elapsedS')));
|
|
201
|
+
return {
|
|
202
|
+
cond,
|
|
203
|
+
route: str(first, 'route'),
|
|
204
|
+
mode: str(first, 'mode'),
|
|
205
|
+
rate: median(list.map((r) => num(r, 'reqs') / num(r, 'elapsedS'))),
|
|
206
|
+
aboveWatts: spread(list.map((r) => num(r, 'pkgW') - idle.median)),
|
|
207
|
+
mjPerRequest: spread(mj)
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
const fits = {};
|
|
211
|
+
for (const route of ['cms', 'null', 'ana']) {
|
|
212
|
+
for (const mode of ['steady', 'bursty']) {
|
|
213
|
+
const points = mine
|
|
214
|
+
.filter((r) => str(r, 'route') === route && str(r, 'mode') === mode)
|
|
215
|
+
.map((r) => [
|
|
216
|
+
num(r, 'rate'),
|
|
217
|
+
num(r, 'pkgW') - idle.median
|
|
218
|
+
]);
|
|
219
|
+
if (new Set(points.map(([x]) => x)).size < 2)
|
|
220
|
+
continue;
|
|
221
|
+
fits[`${route}-${mode}`] = linearFit(points);
|
|
222
|
+
if (route === 'cms') {
|
|
223
|
+
const low = points.filter(([x]) => x <= 200);
|
|
224
|
+
if (new Set(low.map(([x]) => x)).size >= 2)
|
|
225
|
+
fits[`${route}-${mode}-low`] = linearFit(low);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return { idleWatts: idle, conditions, fits };
|
|
230
|
+
}
|
|
231
|
+
/** {@link PhoneRun} from iPhone rows: `energy_j` over the window, charging windows excluded */
|
|
232
|
+
export function phoneRun(rows) {
|
|
233
|
+
const clean = rows.filter((r) => !r.charging.some(Boolean));
|
|
234
|
+
const idle = new Map();
|
|
235
|
+
for (const row of clean)
|
|
236
|
+
if (str(row, 'arm') === 'idle')
|
|
237
|
+
idle.set(num(row, 'round'), row);
|
|
238
|
+
const watts = (r) => num(r, 'energy_j') / num(r, 'seconds');
|
|
239
|
+
const arms = {};
|
|
240
|
+
const names = [...new Set(clean.map((r) => str(r, 'arm')))].filter((a) => a !== 'idle');
|
|
241
|
+
for (const arm of names) {
|
|
242
|
+
const per = clean
|
|
243
|
+
.filter((r) => str(r, 'arm') === arm && idle.has(num(r, 'round')))
|
|
244
|
+
.map((r) => {
|
|
245
|
+
const base = idle.get(num(r, 'round'));
|
|
246
|
+
const seconds = num(r, 'seconds');
|
|
247
|
+
const navs = num(r, 'navs');
|
|
248
|
+
return {
|
|
249
|
+
joules: (num(r, 'energy_j') - watts(base) * seconds) / navs,
|
|
250
|
+
navs,
|
|
251
|
+
seconds: seconds / navs
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
if (per.length === 0)
|
|
255
|
+
continue;
|
|
256
|
+
arms[arm] = {
|
|
257
|
+
windows: per.length,
|
|
258
|
+
joules: spread(per.map((p) => p.joules)),
|
|
259
|
+
viewsPerWindow: median(per.map((p) => p.navs)),
|
|
260
|
+
secondsPerView: median(per.map((p) => p.seconds))
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
const idleRows = [...idle.values()];
|
|
264
|
+
return {
|
|
265
|
+
dropped: rows.length - clean.length,
|
|
266
|
+
arms,
|
|
267
|
+
idle: {
|
|
268
|
+
joules: spread(idleRows.map((r) => num(r, 'energy_j'))),
|
|
269
|
+
seconds: median(idleRows.map((r) => num(r, 'seconds')))
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* A runtimes reading by runtime and arm. Rows with a `discarded` reason are dropped, and so is
|
|
275
|
+
* a round whose idle window was.
|
|
276
|
+
*/
|
|
277
|
+
export function runtimeRun(rows) {
|
|
278
|
+
const clean = rows.filter((r) => r.discarded === null || r.discarded === undefined);
|
|
279
|
+
const key = (r) => `${str(r, 'runtime')}/${num(r, 'round')}`;
|
|
280
|
+
const idle = new Map(clean.filter((r) => str(r, 'arm') === 'idle').map((r) => [key(r), r]));
|
|
281
|
+
const rate = (r, field) => num(r, field) / num(r, 'windowS');
|
|
282
|
+
const requests = (r) => ['originNet', 'originSpool'].reduce((s, f) => s + (r[f]?.requests ?? 0), 0);
|
|
283
|
+
const out = {};
|
|
284
|
+
const groups = new Map();
|
|
285
|
+
for (const row of clean) {
|
|
286
|
+
if (str(row, 'arm') === 'idle' || !idle.has(key(row)))
|
|
287
|
+
continue;
|
|
288
|
+
const group = `${str(row, 'runtime')}/${str(row, 'arm')}`;
|
|
289
|
+
groups.set(group, [...(groups.get(group) ?? []), row]);
|
|
290
|
+
}
|
|
291
|
+
for (const [group, list] of groups) {
|
|
292
|
+
const [runtime = '', arm = ''] = group.split('/');
|
|
293
|
+
const above = (r, fields) => {
|
|
294
|
+
const base = idle.get(key(r));
|
|
295
|
+
const seconds = num(r, 'windowS');
|
|
296
|
+
const sum = (x) => fields.reduce((s, f) => s + rate(x, f), 0);
|
|
297
|
+
return ((sum(r) - sum(base)) * seconds) / num(r, 'views');
|
|
298
|
+
};
|
|
299
|
+
(out[runtime] ??= {})[arm] = {
|
|
300
|
+
windows: list.length,
|
|
301
|
+
joules: spread(list.map((r) => above(r, ['pkgJ']))),
|
|
302
|
+
overlayBytes: spread(list.map((r) => above(r, ['tailscaleRxB', 'tailscaleTxB']))),
|
|
303
|
+
wireBytes: spread(list.map((r) => above(r, ['wlanRxB', 'wlanTxB']))),
|
|
304
|
+
originRequests: spread(list.map((r) => requests(r) / num(r, 'views')))
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
return out;
|
|
308
|
+
}
|
|
309
|
+
/** a navigation that loaded the site's page; a 4xx or 5xx document is a block page, not the site */
|
|
310
|
+
function loaded(nav) {
|
|
311
|
+
return nav.ok === true && (num(nav, 'docStatus') ?? 200) < 400;
|
|
312
|
+
}
|
|
313
|
+
/** bytes and request counts by navigation kind, over the navigations that loaded */
|
|
314
|
+
export function repeatNavFigures(rows) {
|
|
315
|
+
const kinds = new Map();
|
|
316
|
+
for (const row of rows) {
|
|
317
|
+
for (const nav of row.navs ?? []) {
|
|
318
|
+
if (!loaded(nav))
|
|
319
|
+
continue;
|
|
320
|
+
const list = kinds.get(str(nav, 'kind')) ?? [];
|
|
321
|
+
list.push({ site: str(row, 'site'), nav });
|
|
322
|
+
kinds.set(str(nav, 'kind'), list);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const out = {};
|
|
326
|
+
for (const [kind, list] of kinds) {
|
|
327
|
+
const bytes = list.map(({ nav }) => num(nav, 'bytes'));
|
|
328
|
+
out[kind] = {
|
|
329
|
+
navs: list.length,
|
|
330
|
+
sites: new Set(list.map((l) => l.site)).size,
|
|
331
|
+
medianBytes: median(bytes),
|
|
332
|
+
meanBytes: bytes.reduce((s, b) => s + b, 0) / bytes.length,
|
|
333
|
+
p75Bytes: quantile(bytes, 0.75),
|
|
334
|
+
medianRequests: median(list.map(({ nav }) => num(nav, 'networkRequests'))),
|
|
335
|
+
medianDocBytes: median(list.map(({ nav }) => num(nav, 'docBytes') ?? 0)),
|
|
336
|
+
medianThirdPartyBytes: median(list.map(({ nav }) => num(nav, 'thirdPartyBytes') ?? 0))
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Each site's repeat view of its home page: every byte it moved, and the bytes of its document
|
|
343
|
+
* request, the only request a default local view replaces (0 when the browser's cache answered it).
|
|
344
|
+
*/
|
|
345
|
+
export function repeatBytesBySite(rows) {
|
|
346
|
+
const out = new Map();
|
|
347
|
+
for (const row of rows) {
|
|
348
|
+
for (const nav of row.navs ?? []) {
|
|
349
|
+
if (loaded(nav) && str(nav, 'kind') === 'warm-home-repeat') {
|
|
350
|
+
out.set(str(row, 'site'), {
|
|
351
|
+
bytes: num(nav, 'bytes'),
|
|
352
|
+
document: num(nav, 'docBytes') ?? 0
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
359
|
+
/** resource types a site's own static files arrive as; every other same-origin type is dynamic */
|
|
360
|
+
const STATIC_TYPES = new Set([
|
|
361
|
+
'Script',
|
|
362
|
+
'Stylesheet',
|
|
363
|
+
'Font',
|
|
364
|
+
'Image',
|
|
365
|
+
'Media'
|
|
366
|
+
]);
|
|
367
|
+
/**
|
|
368
|
+
* Splits a repeat view's network bytes from its per-request records: requests the browser cache
|
|
369
|
+
* answered carry no bytes and are skipped. The document is the first Document request, the one
|
|
370
|
+
* `docBytes` counts. Same-origin is the recorded field, an exact origin match.
|
|
371
|
+
*/
|
|
372
|
+
export function splitRequestBytes(requests) {
|
|
373
|
+
const split = {
|
|
374
|
+
document: 0,
|
|
375
|
+
sameOriginStatic: 0,
|
|
376
|
+
sameOriginDynamic: 0,
|
|
377
|
+
thirdParty: {},
|
|
378
|
+
thirdPartyTotal: 0,
|
|
379
|
+
thirdPartyHosts: {},
|
|
380
|
+
total: 0
|
|
381
|
+
};
|
|
382
|
+
let seenDocument = false;
|
|
383
|
+
for (const request of requests) {
|
|
384
|
+
const isDocument = str(request, 'type') === 'Document' && !seenDocument;
|
|
385
|
+
if (isDocument)
|
|
386
|
+
seenDocument = true;
|
|
387
|
+
if (request.fromCache === true)
|
|
388
|
+
continue;
|
|
389
|
+
const bytes = num(request, 'bytes') ?? 0;
|
|
390
|
+
split.total += bytes;
|
|
391
|
+
if (isDocument)
|
|
392
|
+
split.document += bytes;
|
|
393
|
+
else if (request.sameOrigin === true) {
|
|
394
|
+
if (STATIC_TYPES.has(str(request, 'type')))
|
|
395
|
+
split.sameOriginStatic += bytes;
|
|
396
|
+
else
|
|
397
|
+
split.sameOriginDynamic += bytes;
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
const { class: kind } = classifyOrigin(str(request, 'url'));
|
|
401
|
+
split.thirdParty[kind] = (split.thirdParty[kind] ?? 0) + bytes;
|
|
402
|
+
split.thirdPartyTotal += bytes;
|
|
403
|
+
const host = str(request, 'host');
|
|
404
|
+
split.thirdPartyHosts[host] = (split.thirdPartyHosts[host] ?? 0) + bytes;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return split;
|
|
408
|
+
}
|
|
409
|
+
/** {@link splitRequestBytes} for each site's loaded home-page repeat view that carries request records */
|
|
410
|
+
export function repeatSplitBySite(rows) {
|
|
411
|
+
const out = new Map();
|
|
412
|
+
for (const row of rows) {
|
|
413
|
+
for (const nav of row.navs ?? []) {
|
|
414
|
+
if (loaded(nav) &&
|
|
415
|
+
str(nav, 'kind') === 'warm-home-repeat' &&
|
|
416
|
+
Array.isArray(nav.requestList)) {
|
|
417
|
+
out.set(str(row, 'site'), splitRequestBytes(nav.requestList));
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return out;
|
|
422
|
+
}
|
|
423
|
+
/** server hits per service-worker navigation, by registration setting and engine */
|
|
424
|
+
export function zeroNetworkFigures(rows) {
|
|
425
|
+
return rows.map((row) => {
|
|
426
|
+
const views = row.rows ?? [];
|
|
427
|
+
const hits = {};
|
|
428
|
+
let viewsWithHits = 0;
|
|
429
|
+
for (const view of views) {
|
|
430
|
+
const paths = view.serverHits ?? [];
|
|
431
|
+
if (paths.length > 0)
|
|
432
|
+
viewsWithHits += 1;
|
|
433
|
+
for (const path of paths)
|
|
434
|
+
hits[path] = (hits[path] ?? 0) + 1;
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
arm: str(row, 'arm'),
|
|
438
|
+
engine: str(row, 'engine'),
|
|
439
|
+
views: views.length,
|
|
440
|
+
viewsWithHits,
|
|
441
|
+
hits
|
|
442
|
+
};
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
/** CPU milliseconds and wall milliseconds per navigation, by arm, from a device-cpu run */
|
|
446
|
+
export function deviceCpuFigures(measurement) {
|
|
447
|
+
const navs = Number(measurement.method['navigations per run']);
|
|
448
|
+
const arms = {};
|
|
449
|
+
for (const row of measurement.rows)
|
|
450
|
+
(arms[str(row, 'arm')] ??= []).push(row);
|
|
451
|
+
return Object.fromEntries(Object.entries(arms).map(([arm, runs]) => [
|
|
452
|
+
arm,
|
|
453
|
+
{
|
|
454
|
+
runs: runs.length,
|
|
455
|
+
cpuMs: spread(runs.map((r) => (num(r, 'cpuS') * 1000) / navs)),
|
|
456
|
+
wallMs: median(runs.map((r) => num(r, 'wallMsPerNav'))),
|
|
457
|
+
serverHits: median(runs.map((r) => num(r, 'serverHitsPerNav')))
|
|
458
|
+
}
|
|
459
|
+
]));
|
|
460
|
+
}
|
|
461
|
+
/** medians by navigation kind from an on-wire run: rows carry a `wire` summary beside the browser's counts */
|
|
462
|
+
export function onWireFigures(rows) {
|
|
463
|
+
const kinds = new Map();
|
|
464
|
+
for (const row of rows) {
|
|
465
|
+
for (const nav of row.navs ?? []) {
|
|
466
|
+
if (nav.ok !== true || typeof nav.wire !== 'object' || nav.wire === null)
|
|
467
|
+
continue;
|
|
468
|
+
const list = kinds.get(str(nav, 'kind')) ?? [];
|
|
469
|
+
list.push(nav);
|
|
470
|
+
kinds.set(str(nav, 'kind'), list);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const out = {};
|
|
474
|
+
for (const [kind, navs] of kinds) {
|
|
475
|
+
const wire = (nav) => nav.wire;
|
|
476
|
+
const requests = navs.reduce((s, n) => s + num(wire(n), 'requests'), 0);
|
|
477
|
+
const third = navs.reduce((s, n) => s + num(wire(n), 'thirdParty'), 0);
|
|
478
|
+
out[kind] = {
|
|
479
|
+
navs: navs.length,
|
|
480
|
+
browserRequests: median(navs.map((n) => num(n, 'networkRequests'))),
|
|
481
|
+
wireRequests: median(navs.map((n) => num(wire(n), 'requests'))),
|
|
482
|
+
browserBytes: median(navs.map((n) => num(n, 'bytes'))),
|
|
483
|
+
wireResponseBytes: median(navs.map((n) => num(wire(n), 'responseBytes'))),
|
|
484
|
+
equal: navs.filter((n) => n.networkRequests === wire(n).requests).length,
|
|
485
|
+
thirdPartyShare: requests === 0 ? 0 : third / requests,
|
|
486
|
+
thirdPartyRequests: median(navs.map((n) => num(wire(n), 'thirdParty')))
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
return out;
|
|
490
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type HostPrice } from './inputs.js';
|
|
2
|
+
/** egress dollars for `localViews` that no longer send the document, at the host's tier price */
|
|
3
|
+
export declare function egressUsd(localViews: number, host: HostPrice, bytes?: number): number;
|
|
4
|
+
/** request dollars for `localViews`, one request each, where the host bills requests */
|
|
5
|
+
export declare function requestUsd(localViews: number, host: HostPrice): number;
|
|
6
|
+
/** egress plus request dollars, before any free allowance */
|
|
7
|
+
export declare function bandwidthUsd(localViews: number, host: HostPrice, bytes?: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* egress dollars per million views when `share` of them are views that move `bytes` fewer bytes,
|
|
10
|
+
* at the host's tier price. A trimmed file is the same request, so there is no request term.
|
|
11
|
+
*/
|
|
12
|
+
export declare function perMillionViewsUsd(bytes: number, share: number, host: HostPrice): number;
|
|
13
|
+
/** decimal GB a month that `annualLocalViews` no longer move */
|
|
14
|
+
export declare function monthlyGb(annualLocalViews: number, bytes?: number): number;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DOCUMENT_BYTES } from './inputs.js';
|
|
2
|
+
/** egress dollars for `localViews` that no longer send the document, at the host's tier price */
|
|
3
|
+
export function egressUsd(localViews, host, bytes = DOCUMENT_BYTES.value) {
|
|
4
|
+
return ((localViews * bytes) / host.bytesPerUnit) * host.egress.value;
|
|
5
|
+
}
|
|
6
|
+
/** request dollars for `localViews`, one request each, where the host bills requests */
|
|
7
|
+
export function requestUsd(localViews, host) {
|
|
8
|
+
return host.requests ? (localViews / 1e6) * host.requests.value : 0;
|
|
9
|
+
}
|
|
10
|
+
/** egress plus request dollars, before any free allowance */
|
|
11
|
+
export function bandwidthUsd(localViews, host, bytes = DOCUMENT_BYTES.value) {
|
|
12
|
+
return egressUsd(localViews, host, bytes) + requestUsd(localViews, host);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* egress dollars per million views when `share` of them are views that move `bytes` fewer bytes,
|
|
16
|
+
* at the host's tier price. A trimmed file is the same request, so there is no request term.
|
|
17
|
+
*/
|
|
18
|
+
export function perMillionViewsUsd(bytes, share, host) {
|
|
19
|
+
return egressUsd(1e6 * share, host, bytes);
|
|
20
|
+
}
|
|
21
|
+
/** decimal GB a month that `annualLocalViews` no longer move */
|
|
22
|
+
export function monthlyGb(annualLocalViews, bytes = DOCUMENT_BYTES.value) {
|
|
23
|
+
return (annualLocalViews * bytes) / 12 / 1e9;
|
|
24
|
+
}
|