@drupflare/spool 0.2.0 → 0.4.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/README.md +499 -43
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/activity.d.ts +37 -0
- package/dist/client/activity.js +114 -0
- package/dist/client/entry.js +34 -1
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/client/register.d.ts +23 -1
- package/dist/client/register.js +53 -7
- package/dist/media/embeds.d.ts +3 -2
- package/dist/media/embeds.js +16 -1
- package/dist/protocol/config.d.ts +16 -1
- package/dist/protocol/config.js +3 -1
- package/dist/protocol/dataset.d.ts +183 -0
- package/dist/protocol/dataset.js +206 -0
- package/dist/protocol/envelope.d.ts +3 -0
- package/dist/protocol/envelope.js +14 -3
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/graph.d.ts +23 -4
- package/dist/protocol/graph.js +34 -3
- package/dist/protocol/hash.d.ts +2 -0
- package/dist/protocol/hash.js +8 -0
- package/dist/protocol/index.d.ts +7 -0
- package/dist/protocol/index.js +7 -0
- package/dist/protocol/inline.d.ts +14 -0
- package/dist/protocol/inline.js +43 -0
- package/dist/protocol/navigation.d.ts +81 -0
- package/dist/protocol/navigation.js +249 -0
- package/dist/protocol/normalise.d.ts +4 -3
- package/dist/protocol/normalise.js +6 -4
- package/dist/protocol/plan.d.ts +100 -0
- package/dist/protocol/plan.js +661 -0
- package/dist/protocol/provision.d.ts +52 -1
- package/dist/protocol/provision.js +53 -5
- package/dist/protocol/query.d.ts +141 -0
- package/dist/protocol/query.js +252 -0
- package/dist/protocol/route.d.ts +85 -5
- package/dist/protocol/route.js +91 -0
- package/dist/protocol/write.d.ts +146 -0
- package/dist/protocol/write.js +101 -0
- package/dist/proxy/bases.d.ts +53 -0
- package/dist/proxy/bases.js +76 -0
- package/dist/proxy/index.d.ts +1 -0
- package/dist/proxy/index.js +1 -0
- package/dist/proxy/proxy.js +8 -2
- package/dist/report/capacity.d.ts +59 -0
- package/dist/report/capacity.js +76 -0
- package/dist/report/channels.d.ts +60 -0
- package/dist/report/channels.js +65 -0
- package/dist/report/dataset.d.ts +48 -0
- package/dist/report/dataset.js +93 -0
- package/dist/report/derive.d.ts +285 -0
- package/dist/report/derive.js +676 -1
- package/dist/report/economics.d.ts +11 -1
- package/dist/report/economics.js +19 -1
- package/dist/report/index.d.ts +5 -0
- package/dist/report/index.js +5 -0
- package/dist/report/inputs.d.ts +97 -0
- package/dist/report/inputs.js +133 -0
- package/dist/report/institution.d.ts +284 -0
- package/dist/report/institution.js +225 -0
- package/dist/report/ledger.d.ts +28 -0
- package/dist/report/ledger.js +30 -0
- package/dist/report/loops.d.ts +73 -0
- package/dist/report/loops.js +101 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +6 -1
- package/dist/report/model.d.ts +2 -0
- package/dist/report/model.js +4 -0
- package/dist/report/thirdparty.d.ts +57 -0
- package/dist/report/thirdparty.js +111 -0
- package/dist/report/traffic.d.ts +11 -2
- package/dist/report/traffic.js +13 -3
- package/dist/report/trim.d.ts +56 -0
- package/dist/report/trim.js +83 -1
- package/dist/server/dataset.d.ts +48 -0
- package/dist/server/dataset.js +308 -0
- package/dist/server/embedded.d.ts +3 -3
- package/dist/server/embedded.js +3 -3
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +3 -0
- package/dist/server/spool.d.ts +40 -0
- package/dist/server/spool.js +184 -23
- package/dist/server/write.d.ts +27 -0
- package/dist/server/write.js +127 -0
- package/dist/sw/analytics.d.ts +6 -0
- package/dist/sw/analytics.js +14 -0
- package/dist/sw/dataset-sync.d.ts +76 -0
- package/dist/sw/dataset-sync.js +149 -0
- package/dist/sw/dataset.d.ts +91 -0
- package/dist/sw/dataset.js +318 -0
- package/dist/sw/graph/index.d.ts +31 -1
- package/dist/sw/graph/index.js +57 -5
- package/dist/sw/graph/invalidate.d.ts +15 -3
- package/dist/sw/graph/invalidate.js +42 -4
- package/dist/sw/idb.js +78 -9
- package/dist/sw/partition.d.ts +6 -1
- package/dist/sw/partition.js +21 -2
- package/dist/sw/principal-dataset.d.ts +64 -0
- package/dist/sw/principal-dataset.js +254 -0
- package/dist/sw/render.d.ts +8 -1
- package/dist/sw/render.js +28 -1
- package/dist/sw/replica.d.ts +14 -3
- package/dist/sw/replica.js +46 -5
- package/dist/sw/router.js +2 -2
- package/dist/sw/runtime.d.ts +43 -2
- package/dist/sw/runtime.js +440 -29
- package/dist/sw/seal.d.ts +13 -0
- package/dist/sw/seal.js +13 -4
- package/dist/sw/static.d.ts +18 -0
- package/dist/sw/static.js +84 -1
- package/dist/sw/storage.d.ts +51 -1
- package/dist/sw/storage.js +33 -1
- package/dist/sw/write.d.ts +107 -0
- package/dist/sw/write.js +442 -0
- package/package.json +5 -2
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** the path of a URL up to and including its last slash */
|
|
2
|
+
export function dirOf(url) {
|
|
3
|
+
const path = new URL(url).pathname;
|
|
4
|
+
return path.slice(0, path.lastIndexOf('/') + 1);
|
|
5
|
+
}
|
|
6
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
7
|
+
/**
|
|
8
|
+
* The member of `members` that minimizes the sum, over the other members, of the smaller of their
|
|
9
|
+
* delta against it and their plain size, plus the member's own plain size under `first-visit`.
|
|
10
|
+
* Ties keep the member listed first. `score` is the minimized sum and `others` its part without the
|
|
11
|
+
* base's own size.
|
|
12
|
+
*/
|
|
13
|
+
export function pickBase(members, delta, plain, policy = 'held') {
|
|
14
|
+
let best = -1;
|
|
15
|
+
let bestScore = Infinity;
|
|
16
|
+
let bestOthers = Infinity;
|
|
17
|
+
for (const c of members) {
|
|
18
|
+
let others = 0;
|
|
19
|
+
for (const f of members) {
|
|
20
|
+
if (f !== c)
|
|
21
|
+
others += Math.min(delta(f, c), plain(f));
|
|
22
|
+
}
|
|
23
|
+
const score = policy === 'first-visit' ? others + plain(c) : others;
|
|
24
|
+
if (score < bestScore) {
|
|
25
|
+
best = c;
|
|
26
|
+
bestScore = score;
|
|
27
|
+
bestOthers = others;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { best, score: bestScore, others: bestOthers };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* One base per directory and kind, scored by `policy` (default `held`). Members are ordered by URL
|
|
34
|
+
* before scoring, so a tie goes to the URL that sorts first whatever the input order. A group of
|
|
35
|
+
* one file has no base.
|
|
36
|
+
*/
|
|
37
|
+
export async function chooseBases(files, delta, options = {}) {
|
|
38
|
+
const at = (i) => files[i];
|
|
39
|
+
const groups = new Map();
|
|
40
|
+
for (const [i, f] of files.entries()) {
|
|
41
|
+
const key = JSON.stringify([f.kind, dirOf(f.url)]);
|
|
42
|
+
const members = groups.get(key);
|
|
43
|
+
if (members)
|
|
44
|
+
members.push(i);
|
|
45
|
+
else
|
|
46
|
+
groups.set(key, [i]);
|
|
47
|
+
}
|
|
48
|
+
const choices = [];
|
|
49
|
+
for (const members of groups.values()) {
|
|
50
|
+
if (members.length < 2)
|
|
51
|
+
continue;
|
|
52
|
+
const sorted = [...members].sort((a, b) => compare(at(a).url, at(b).url));
|
|
53
|
+
const sizes = new Map();
|
|
54
|
+
for (const f of sorted) {
|
|
55
|
+
for (const c of sorted) {
|
|
56
|
+
if (f !== c)
|
|
57
|
+
sizes.set(`${f}|${c}`, await delta(f, c));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const { best, others } = pickBase(sorted, (f, c) => sizes.get(`${f}|${c}`), (f) => at(f).plain, options.policy);
|
|
61
|
+
const { kind, url } = at(best);
|
|
62
|
+
choices.push({
|
|
63
|
+
kind,
|
|
64
|
+
directory: dirOf(url),
|
|
65
|
+
url,
|
|
66
|
+
files: sorted.length,
|
|
67
|
+
meanDelta: others / (sorted.length - 1)
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return choices.sort((a, b) => compare(a.directory, b.directory) || compare(a.kind, b.kind));
|
|
71
|
+
}
|
|
72
|
+
/** true for the exact pathnames of the chosen base URLs; a query string does not count */
|
|
73
|
+
export function baseMatcher(choices) {
|
|
74
|
+
const paths = new Set(choices.map((choice) => new URL(choice.url).pathname));
|
|
75
|
+
return (pathname) => paths.has(pathname);
|
|
76
|
+
}
|
package/dist/proxy/index.d.ts
CHANGED
package/dist/proxy/index.js
CHANGED
package/dist/proxy/proxy.js
CHANGED
|
@@ -211,9 +211,11 @@ export function spoolProxy(options) {
|
|
|
211
211
|
...(read || !request.body ? {} : { body: request.body, duplex: 'half' })
|
|
212
212
|
});
|
|
213
213
|
}
|
|
214
|
-
/** the headers the browser gets: bodies arrive decoded, so the encoding and its length go */
|
|
214
|
+
/** the headers the browser gets: bodies arrive decoded, so the encoding and its length go, and so does what only described the hop to the proxy */
|
|
215
215
|
function tidy(source, url) {
|
|
216
216
|
const headers = new Headers(source);
|
|
217
|
+
for (const name of HOP_BY_HOP)
|
|
218
|
+
headers.delete(name);
|
|
217
219
|
if (headers.has('content-encoding')) {
|
|
218
220
|
headers.delete('content-encoding');
|
|
219
221
|
headers.delete('content-length');
|
|
@@ -371,6 +373,7 @@ export function spoolProxy(options) {
|
|
|
371
373
|
if (url.pathname === PATHS.worker && request.method === 'GET')
|
|
372
374
|
return spool.stub();
|
|
373
375
|
const converting = images !== undefined && imageRequest(images, request);
|
|
376
|
+
let visitorRewrite = false;
|
|
374
377
|
let upstream;
|
|
375
378
|
try {
|
|
376
379
|
upstream = await call(forward(request, url, converting));
|
|
@@ -427,6 +430,7 @@ export function spoolProxy(options) {
|
|
|
427
430
|
videoStats.rewritten += result.rewritten;
|
|
428
431
|
// a page rewritten for one visitor's connection is not another's copy
|
|
429
432
|
if (video.when !== 'always') {
|
|
433
|
+
visitorRewrite = true;
|
|
430
434
|
makePrivate(headers);
|
|
431
435
|
mergeList(headers, 'vary', ['Save-Data', 'ECT']);
|
|
432
436
|
}
|
|
@@ -488,7 +492,9 @@ export function spoolProxy(options) {
|
|
|
488
492
|
path: url.pathname,
|
|
489
493
|
page: Promise.resolve({ text, tags: effective })
|
|
490
494
|
});
|
|
491
|
-
|
|
495
|
+
// a visitor-signal rewrite is not in the copy /.spool/artifact builds, so the worker asks for it
|
|
496
|
+
const same = !visitorRewrite && !data && !principal;
|
|
497
|
+
return spool.decorate(request, new Response(text, init), same ? { assembled: text } : {});
|
|
492
498
|
}
|
|
493
499
|
return Object.assign(handle, {
|
|
494
500
|
spool,
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { WorkSample } from './traffic.js';
|
|
2
|
+
/**
|
|
3
|
+
* What one node can do within the latency SLO, per resource. A resource is any name the trace uses,
|
|
4
|
+
* such as `busyMs` or `memoryMb`. `perNode.busyMs` is handler milliseconds a node can do per second
|
|
5
|
+
* within the SLO, for example 1000 times cores times a target utilisation, so callers pass busy time
|
|
6
|
+
* as a per-second rate.
|
|
7
|
+
*/
|
|
8
|
+
export type NodeLimits = {
|
|
9
|
+
/** what one node serves per second within the SLO, per resource; must be positive */
|
|
10
|
+
perNode: Record<string, number>;
|
|
11
|
+
/** fewest nodes the fleet runs; default 1 */
|
|
12
|
+
floor?: number;
|
|
13
|
+
/** nodes kept above the peak need; default 0 */
|
|
14
|
+
spares?: number;
|
|
15
|
+
/** regions the fleet spans; default 1 */
|
|
16
|
+
regions?: number;
|
|
17
|
+
/** nodes each region must run; default 1 */
|
|
18
|
+
minPerRegion?: number;
|
|
19
|
+
};
|
|
20
|
+
/** demand in one interval: what the origin did, and what spool removed of it, per resource */
|
|
21
|
+
export type IntervalDemand = {
|
|
22
|
+
/** when the interval starts, on the clock of the samples it came from */
|
|
23
|
+
start: number;
|
|
24
|
+
/** length of the interval in seconds */
|
|
25
|
+
seconds: number;
|
|
26
|
+
/** the origin's need per resource, per second */
|
|
27
|
+
demand: Record<string, number>;
|
|
28
|
+
/** the part of demand spool removed, per resource and per second; missing means none */
|
|
29
|
+
avoided?: Record<string, number>;
|
|
30
|
+
};
|
|
31
|
+
/** `bindingBefore` and `bindingAfter` name the resource that set the worst interval's need */
|
|
32
|
+
export type CapacityPlan = {
|
|
33
|
+
/** nodes the worst interval needs without spool */
|
|
34
|
+
before: number;
|
|
35
|
+
/** nodes the worst interval needs once avoided work is removed */
|
|
36
|
+
after: number;
|
|
37
|
+
/** nodes spool makes unneeded: before minus after */
|
|
38
|
+
retirable: number;
|
|
39
|
+
/** resource that sets the need before spool; null when no interval needs a node */
|
|
40
|
+
bindingBefore: string | null;
|
|
41
|
+
/** resource that sets the need after spool; null when no interval needs a node */
|
|
42
|
+
bindingAfter: string | null;
|
|
43
|
+
/** number of intervals in the plan */
|
|
44
|
+
intervals: number;
|
|
45
|
+
};
|
|
46
|
+
/** nodes one interval's demand needs, and the resource with the largest need */
|
|
47
|
+
export declare function nodesNeeded(demand: Record<string, number>, limits: NodeLimits): {
|
|
48
|
+
/** nodes needed: peak need plus spares, not below the floor or the regional minimum */
|
|
49
|
+
nodes: number;
|
|
50
|
+
/** resource with the largest need, first by name on a tie; null when none needs a node */
|
|
51
|
+
binding: string | null;
|
|
52
|
+
};
|
|
53
|
+
/** the fleet the intervals need before and after spool removes its avoided work */
|
|
54
|
+
export declare function capacityPlan(intervals: readonly IntervalDemand[], limits: NodeLimits): CapacityPlan;
|
|
55
|
+
/**
|
|
56
|
+
* Per-second demand for each sample, with `busyMs` summed over its routes. `avoidedBusyMs` holds the
|
|
57
|
+
* handler milliseconds spool removed in each interval, in the order of `samples` sorted by start.
|
|
58
|
+
*/
|
|
59
|
+
export declare function intervalsFromWork(samples: readonly WorkSample[], avoidedBusyMs: readonly number[]): IntervalDemand[];
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** nodes one interval's demand needs, and the resource with the largest need */
|
|
2
|
+
export function nodesNeeded(demand, limits) {
|
|
3
|
+
for (const [name, per] of Object.entries(limits.perNode)) {
|
|
4
|
+
if (!Number.isFinite(per) || per <= 0) {
|
|
5
|
+
throw new RangeError(`perNode.${name} must be a positive finite number`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
for (const [name, need] of Object.entries(demand)) {
|
|
9
|
+
if (!Number.isFinite(need) || need < 0) {
|
|
10
|
+
throw new RangeError(`demand.${name} must be a finite, non-negative number`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
let peak = 0;
|
|
14
|
+
let binding = null;
|
|
15
|
+
// sorted so a tie goes to the name that sorts first
|
|
16
|
+
const names = Object.entries(limits.perNode).sort(([a], [b]) => (a < b ? -1 : 1));
|
|
17
|
+
for (const [name, per] of names) {
|
|
18
|
+
const need = Math.ceil((demand[name] ?? 0) / per);
|
|
19
|
+
if (need > peak) {
|
|
20
|
+
peak = need;
|
|
21
|
+
binding = name;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const nodes = Math.max(peak + (limits.spares ?? 0), limits.floor ?? 1, (limits.regions ?? 1) * (limits.minPerRegion ?? 1));
|
|
25
|
+
return { nodes, binding };
|
|
26
|
+
}
|
|
27
|
+
/** the interval that needs the most nodes, first on a tie; an empty list needs the base fleet */
|
|
28
|
+
function worstOf(demands, limits) {
|
|
29
|
+
let worst = null;
|
|
30
|
+
for (const demand of demands) {
|
|
31
|
+
const got = nodesNeeded(demand, limits);
|
|
32
|
+
if (worst === null || got.nodes > worst.nodes)
|
|
33
|
+
worst = got;
|
|
34
|
+
}
|
|
35
|
+
return worst ?? nodesNeeded({}, limits);
|
|
36
|
+
}
|
|
37
|
+
/** demand after avoided work, clamped at zero; a resource with no avoided keeps its need */
|
|
38
|
+
function afterAvoided({ demand, avoided = {} }) {
|
|
39
|
+
const out = {};
|
|
40
|
+
for (const [name, need] of Object.entries(demand)) {
|
|
41
|
+
out[name] = Math.max(0, need - (avoided[name] ?? 0));
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
/** the fleet the intervals need before and after spool removes its avoided work */
|
|
46
|
+
export function capacityPlan(intervals, limits) {
|
|
47
|
+
const before = worstOf(intervals.map((interval) => interval.demand), limits);
|
|
48
|
+
const after = worstOf(intervals.map(afterAvoided), limits);
|
|
49
|
+
return {
|
|
50
|
+
before: before.nodes,
|
|
51
|
+
after: after.nodes,
|
|
52
|
+
retirable: before.nodes - after.nodes,
|
|
53
|
+
bindingBefore: before.binding,
|
|
54
|
+
bindingAfter: after.binding,
|
|
55
|
+
intervals: intervals.length
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Per-second demand for each sample, with `busyMs` summed over its routes. `avoidedBusyMs` holds the
|
|
60
|
+
* handler milliseconds spool removed in each interval, in the order of `samples` sorted by start.
|
|
61
|
+
*/
|
|
62
|
+
export function intervalsFromWork(samples, avoidedBusyMs) {
|
|
63
|
+
if (avoidedBusyMs.length !== samples.length) {
|
|
64
|
+
throw new RangeError('avoidedBusyMs needs one entry per sample');
|
|
65
|
+
}
|
|
66
|
+
const ordered = [...samples].sort((a, b) => a.start - b.start);
|
|
67
|
+
return ordered.map((sample, i) => {
|
|
68
|
+
const busy = Object.values(sample.busyMs).reduce((total, ms) => total + ms, 0);
|
|
69
|
+
return {
|
|
70
|
+
start: sample.start,
|
|
71
|
+
seconds: sample.seconds,
|
|
72
|
+
demand: { busyMs: busy / sample.seconds },
|
|
73
|
+
avoided: { busyMs: avoidedBusyMs[i] / sample.seconds }
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Layer } from './inputs.js';
|
|
2
|
+
import type { InstitutionReport } from './institution.js';
|
|
3
|
+
/** a figure with the layer it is at and the source it came from */
|
|
4
|
+
export type Labelled<T> = {
|
|
5
|
+
/** the figure, in the shape T gives it */
|
|
6
|
+
value: T;
|
|
7
|
+
/** L0 published, L1 measured here, L2 derived, L3 scenario */
|
|
8
|
+
layer: Layer;
|
|
9
|
+
/** where the figure came from, so a reader can check it */
|
|
10
|
+
source: string;
|
|
11
|
+
};
|
|
12
|
+
/** what spool kept off the origin and the network, per measure; null where nothing was measured */
|
|
13
|
+
export type AvoidedDemand = {
|
|
14
|
+
/** bytes not sent over the network */
|
|
15
|
+
transferredBytes: number | null;
|
|
16
|
+
/** requests not made */
|
|
17
|
+
requests: number | null;
|
|
18
|
+
/** origin CPU milliseconds not spent */
|
|
19
|
+
serverCpuMs: number | null;
|
|
20
|
+
/** browser CPU milliseconds not spent */
|
|
21
|
+
browserCpuMs: number | null;
|
|
22
|
+
/** data centre work milliseconds not done */
|
|
23
|
+
dataCentreWorkMs: number | null;
|
|
24
|
+
/** nodes the peak no longer needs */
|
|
25
|
+
peakNodes: number | null;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The four channels. They are reported side by side and never summed, so there is no total across
|
|
29
|
+
* them.
|
|
30
|
+
*/
|
|
31
|
+
export type Channels = {
|
|
32
|
+
/** energy a meter read, in joules; null when nothing was read */
|
|
33
|
+
metered: Labelled<{
|
|
34
|
+
/** joules the meter read */
|
|
35
|
+
joules: number;
|
|
36
|
+
}> | null;
|
|
37
|
+
/** energy attributed to the workload; null when none is attributed */
|
|
38
|
+
attributional: Labelled<{
|
|
39
|
+
/** joules attributed to the workload */
|
|
40
|
+
joules: number;
|
|
41
|
+
}> | null;
|
|
42
|
+
/** the institution report's capacity figures; null without a report */
|
|
43
|
+
capacity: Labelled<InstitutionReport> | null;
|
|
44
|
+
/** what spool kept off the origin and the network; null when not measured */
|
|
45
|
+
avoided: Labelled<AvoidedDemand> | null;
|
|
46
|
+
};
|
|
47
|
+
export type ChannelRow = {
|
|
48
|
+
/** the channel the number belongs to */
|
|
49
|
+
channel: keyof Channels;
|
|
50
|
+
/** what the number measures, as a short label */
|
|
51
|
+
measure: string;
|
|
52
|
+
/** the number, in unit */
|
|
53
|
+
value: number;
|
|
54
|
+
/** unit of value, such as J, nodes or MWh */
|
|
55
|
+
unit: string;
|
|
56
|
+
/** the layer the number is at */
|
|
57
|
+
layer: Layer;
|
|
58
|
+
};
|
|
59
|
+
/** one row per present number, in channel order; rows from different channels are never combined */
|
|
60
|
+
export declare function channelRows(channels: Channels): ChannelRow[];
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** one row per present number, in channel order; rows from different channels are never combined */
|
|
2
|
+
export function channelRows(channels) {
|
|
3
|
+
const rows = [];
|
|
4
|
+
for (const channel of ['metered', 'attributional']) {
|
|
5
|
+
const labelled = channels[channel];
|
|
6
|
+
if (labelled) {
|
|
7
|
+
rows.push({
|
|
8
|
+
channel,
|
|
9
|
+
measure: 'energy',
|
|
10
|
+
value: labelled.value.joules,
|
|
11
|
+
unit: 'J',
|
|
12
|
+
layer: labelled.layer
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const capacity = channels.capacity;
|
|
17
|
+
if (capacity) {
|
|
18
|
+
const { potential, financial, physical } = capacity.value;
|
|
19
|
+
if (potential) {
|
|
20
|
+
rows.push({
|
|
21
|
+
channel: 'capacity',
|
|
22
|
+
measure: 'potential retirable nodes',
|
|
23
|
+
value: potential.plan.retirable,
|
|
24
|
+
unit: 'nodes',
|
|
25
|
+
layer: potential.layer
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
if (financial) {
|
|
29
|
+
rows.push({
|
|
30
|
+
channel: 'capacity',
|
|
31
|
+
measure: 'financial saving',
|
|
32
|
+
value: financial.saving,
|
|
33
|
+
unit: financial.currency,
|
|
34
|
+
layer: financial.layer
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (physical) {
|
|
38
|
+
rows.push({
|
|
39
|
+
channel: 'capacity',
|
|
40
|
+
measure: 'physical energy',
|
|
41
|
+
value: physical.mwh,
|
|
42
|
+
unit: 'MWh',
|
|
43
|
+
layer: physical.layer
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const avoided = channels.avoided;
|
|
48
|
+
if (avoided) {
|
|
49
|
+
const fields = [
|
|
50
|
+
['transferredBytes', 'B'],
|
|
51
|
+
['requests', 'requests'],
|
|
52
|
+
['serverCpuMs', 'ms'],
|
|
53
|
+
['browserCpuMs', 'ms'],
|
|
54
|
+
['dataCentreWorkMs', 'ms'],
|
|
55
|
+
['peakNodes', 'nodes']
|
|
56
|
+
];
|
|
57
|
+
for (const [measure, unit] of fields) {
|
|
58
|
+
const value = avoided.value[measure];
|
|
59
|
+
if (value !== null) {
|
|
60
|
+
rows.push({ channel: 'avoided', measure, value, unit, layer: avoided.layer });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return rows;
|
|
65
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Row } from './measurement.js';
|
|
2
|
+
/** bytes of the 304 a plain HTTP cache gets back, socket bytes (L1, `measurements/dataset-baseline/2026-11-15.json`) */
|
|
3
|
+
export declare const NOT_MODIFIED_SOCKET_BYTES = 220;
|
|
4
|
+
/** bytes of the first load of a seed, per collection size, for the three arms */
|
|
5
|
+
export type DatasetSeed = {
|
|
6
|
+
v03: number;
|
|
7
|
+
v04: number;
|
|
8
|
+
native: number;
|
|
9
|
+
};
|
|
10
|
+
/** bytes of one update to a collection of `rows`, for the three arms; `v03Records` is the records shape with `have` */
|
|
11
|
+
export type DatasetUpdate = {
|
|
12
|
+
rows: number;
|
|
13
|
+
change: string;
|
|
14
|
+
changedRows: number;
|
|
15
|
+
v03: number;
|
|
16
|
+
v03Records: number | null;
|
|
17
|
+
v04: number;
|
|
18
|
+
native: number;
|
|
19
|
+
};
|
|
20
|
+
export type DatasetFigures = {
|
|
21
|
+
seeds: Record<number, DatasetSeed>;
|
|
22
|
+
updates: DatasetUpdate[];
|
|
23
|
+
};
|
|
24
|
+
/** the three arms' bytes from the raw rows of a dataset-baseline reading, gate open and list shape for v0.3 */
|
|
25
|
+
export declare function datasetFigures(rows: readonly Row[]): DatasetFigures;
|
|
26
|
+
/** a site's traffic and updates, declared per site (L3) */
|
|
27
|
+
export type Site = {
|
|
28
|
+
name: string;
|
|
29
|
+
viewsPerDay: number;
|
|
30
|
+
updatesPerDay: number;
|
|
31
|
+
devices: number;
|
|
32
|
+
};
|
|
33
|
+
export type SiteFigures = {
|
|
34
|
+
/** views per device per day that a held copy answers: views per device times the return survival */
|
|
35
|
+
served: number;
|
|
36
|
+
/** patches or refetches per device per day, at most one per answered view */
|
|
37
|
+
fetches: number;
|
|
38
|
+
bytesPerDay: Record<'v03' | 'v04' | 'native', number>;
|
|
39
|
+
joulesPerDay: Record<'v03' | 'v04' | 'native', number>;
|
|
40
|
+
/** updates per device over one hold that repay the v0.4 seed's extra bytes against the v0.3 control */
|
|
41
|
+
breakEven: number;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Bytes per device per day for each arm at a site. A device holds one copy for `DATASET_DEFAULTS.holdDays`
|
|
45
|
+
* and refetches the seed each time. Views that find a copy are `views x survival`; a native cache
|
|
46
|
+
* revalidates on each of those views and sends the full resource when a change landed since the last one.
|
|
47
|
+
*/
|
|
48
|
+
export declare function siteFigures(site: Site, seed: DatasetSeed, update: DatasetUpdate): SiteFigures;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { DATASET_DEFAULTS } from '../protocol/provision.js';
|
|
2
|
+
import { attributionalJoules } from './model.js';
|
|
3
|
+
/** bytes of the 304 a plain HTTP cache gets back, socket bytes (L1, `measurements/dataset-baseline/2026-11-15.json`) */
|
|
4
|
+
export const NOT_MODIFIED_SOCKET_BYTES = 220;
|
|
5
|
+
const at = (row, key) => {
|
|
6
|
+
const value = row[key];
|
|
7
|
+
if (typeof value !== 'object' || value === null)
|
|
8
|
+
throw new TypeError(`no ${key} in the row`);
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
const num = (row, key) => {
|
|
12
|
+
const value = row[key];
|
|
13
|
+
if (typeof value !== 'number')
|
|
14
|
+
throw new TypeError(`no numeric ${key} in the row`);
|
|
15
|
+
return value;
|
|
16
|
+
};
|
|
17
|
+
/** the three arms' bytes from the raw rows of a dataset-baseline reading, gate open and list shape for v0.3 */
|
|
18
|
+
export function datasetFigures(rows) {
|
|
19
|
+
const scenarios = rows.filter((r) => r.kind === 'scenario' && r.arm === 'gate open');
|
|
20
|
+
const sizes = [...new Set(scenarios.map((r) => num(r, 'n')))].sort((a, b) => a - b);
|
|
21
|
+
const seeds = {};
|
|
22
|
+
const updates = [];
|
|
23
|
+
const find = (pick) => {
|
|
24
|
+
const found = rows.find(pick);
|
|
25
|
+
if (!found)
|
|
26
|
+
throw new RangeError('the reading has no row for this collection and change');
|
|
27
|
+
return found;
|
|
28
|
+
};
|
|
29
|
+
for (const n of sizes) {
|
|
30
|
+
for (const change of ['change-1', 'change-1pct']) {
|
|
31
|
+
const list = find((r) => r.kind === 'scenario' &&
|
|
32
|
+
r.shape === 'list' &&
|
|
33
|
+
r.arm === 'gate open' &&
|
|
34
|
+
r.n === n &&
|
|
35
|
+
r.change === change);
|
|
36
|
+
const records = rows.find((r) => r.kind === 'scenario' &&
|
|
37
|
+
r.shape === 'records' &&
|
|
38
|
+
r.arm === 'gate open' &&
|
|
39
|
+
r.n === n &&
|
|
40
|
+
r.change === change);
|
|
41
|
+
const v04 = find((r) => r.kind === 'v04' && r.n === n && r.change === change);
|
|
42
|
+
const native = find((r) => r.kind === 'native' && r.n === n && r.change === change);
|
|
43
|
+
if (change === 'change-1') {
|
|
44
|
+
seeds[n] = {
|
|
45
|
+
v03: num(at(list, 'seed'), 'brotli'),
|
|
46
|
+
v04: num(at(v04, 'seed'), 'brotli'),
|
|
47
|
+
native: num(at(native, 'firstLoad'), 'socketBytes')
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
updates.push({
|
|
51
|
+
rows: n,
|
|
52
|
+
change,
|
|
53
|
+
changedRows: num(list, 'changedRows'),
|
|
54
|
+
v03: num(at(list, 'refetchWithout'), 'brotli'),
|
|
55
|
+
v03Records: records?.refetchWithHave
|
|
56
|
+
? num(at(records, 'refetchWithHave'), 'brotli')
|
|
57
|
+
: null,
|
|
58
|
+
v04: num(at(v04, 'patch'), 'brotli'),
|
|
59
|
+
native: num(at(native, 'revalidateChanged'), 'socketBytes')
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return { seeds, updates };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Bytes per device per day for each arm at a site. A device holds one copy for `DATASET_DEFAULTS.holdDays`
|
|
67
|
+
* and refetches the seed each time. Views that find a copy are `views x survival`; a native cache
|
|
68
|
+
* revalidates on each of those views and sends the full resource when a change landed since the last one.
|
|
69
|
+
*/
|
|
70
|
+
export function siteFigures(site, seed, update) {
|
|
71
|
+
const { survival, holdDays } = DATASET_DEFAULTS;
|
|
72
|
+
const served = (site.viewsPerDay / site.devices) * survival;
|
|
73
|
+
const fetches = Math.min(site.updatesPerDay, served);
|
|
74
|
+
const changed = served > 0 ? 1 - Math.exp(-site.updatesPerDay / served) : 0;
|
|
75
|
+
const bytesPerDay = {
|
|
76
|
+
v03: seed.v03 / holdDays + fetches * update.v03,
|
|
77
|
+
v04: seed.v04 / holdDays + fetches * update.v04,
|
|
78
|
+
native: served * (changed * update.native + (1 - changed) * NOT_MODIFIED_SOCKET_BYTES)
|
|
79
|
+
};
|
|
80
|
+
const saved = update.v03 - update.v04;
|
|
81
|
+
const extra = Math.max(0, seed.v04 - seed.v03);
|
|
82
|
+
return {
|
|
83
|
+
served,
|
|
84
|
+
fetches,
|
|
85
|
+
bytesPerDay,
|
|
86
|
+
joulesPerDay: {
|
|
87
|
+
v03: attributionalJoules(bytesPerDay.v03),
|
|
88
|
+
v04: attributionalJoules(bytesPerDay.v04),
|
|
89
|
+
native: attributionalJoules(bytesPerDay.native)
|
|
90
|
+
},
|
|
91
|
+
breakEven: saved > 0 ? extra / saved : Infinity
|
|
92
|
+
};
|
|
93
|
+
}
|