@torrent-tv/proxy 2.29.0 → 2.30.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/CHANGELOG.md +4 -0
- package/package.json +1 -1
- package/services/supply-margin.js +159 -0
- package/services/torrent-worker/piece-reader.js +73 -0
- package/services/torrent-worker/supply-interruptions.js +155 -0
- package/test/supply-interruptions.test.js +125 -0
- package/test/supply-margin.test.js +112 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
## 2.30.0
|
|
2
|
+
|
|
3
|
+
- **New**: The speed a step must sustain, and the smallest buffer that hides an interruption, are now COMPUTED from the supply's own behaviour instead of being chosen by hand — printed first, used later. A step producing at `v` gains `v - 1` seconds of cushion per second and an interruption of `W` seconds costs `W`, so it survives its own supply only while `(v - 1) × T > W`, that is `v > 1 + W / T`, with `W` the worst recent wait for a piece and `T` the median interval between such waits. On the field torrent of 2026-08-17 that is **2.42x**, against the 1.5 assumed today and the 1.05 measured on the step that stalled; on the same file's copied stream it is 1.31 against 8x measured, which is why a copy never stalls. The buffer follows from the same readings: one whole segment — the one being played — plus the worst interruption that can arrive before it refills, whichever source it comes from, which was **7-9 s** where the browser waits for 25. Both figures are logged per file every half minute, so the next session says whether the arithmetic describes reality BEFORE anything is decided by it. The arithmetic is a pure module with the field session's own numbers as its tests (`services/supply-margin.js`).
|
|
4
|
+
|
|
1
5
|
## 2.29.0
|
|
2
6
|
|
|
3
7
|
- **New**: A piece a reader is blocked on is handed to the fastest peers that hold it. Measured 2026-08-17: the swarm delivered 5.1-5.9 MB/s against a film consumed at about 1 MB/s — a fivefold surplus — and the reader still blocked 47 times in two minutes, 1.0-4.5 s each, on pieces a median of five peers already had. A block belongs to exactly one wire, so the read ends when the SLOWEST holder delivers, and `critical()` only lets the library take a block from a slow wire when its own picker happens to visit an idle one. This asks for it deliberately: when the wait starts, and again on the sampling tick that already runs while it lasts, the piece is pushed onto the three fastest unchoked holders through the library's own request entry with hotswap enabled. Nothing is duplicated — the library moves a block to a wire at least twice as fast, which bounds how often it can move at all. A refusal is counted rather than ignored (a full pipeline, or nothing reservable even with hotswap, means the piece waits on the wire and not on the picker), and a build that offers no such entry says so instead of failing silently. The wait line now reports `steered onto N of M holders`, so the next session says by number whether the tail shortened.
|
package/package.json
CHANGED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file What speed a step must run at, and how much buffer a viewer needs —
|
|
3
|
+
* both derived from the supply's own interruptions rather than chosen by hand.
|
|
4
|
+
*
|
|
5
|
+
* The two numbers this replaces were guesses. The speed margin was 1.5, and on
|
|
6
|
+
* the field session of 2026-08-17 a step admitted by it ran at 1.05x and
|
|
7
|
+
* stalled; the pre-buffer target was 25 s, which is sixteen seconds of waiting
|
|
8
|
+
* before the picture starts that nobody had shown to be necessary.
|
|
9
|
+
*
|
|
10
|
+
* Both follow from the same two measured quantities, and from nothing else:
|
|
11
|
+
*
|
|
12
|
+
* W — how long a read waits for a piece it needs (the worst recent one);
|
|
13
|
+
* T — how long there is between such waits (the median recent interval).
|
|
14
|
+
*
|
|
15
|
+
* **The margin.** A step producing at speed `v` gains `v - 1` seconds of
|
|
16
|
+
* cushion for every second it runs, and an interruption of `W` seconds costs
|
|
17
|
+
* `W`. It therefore survives its own supply only if what it gains between
|
|
18
|
+
* interruptions exceeds what one costs:
|
|
19
|
+
*
|
|
20
|
+
* (v - 1) × T > W i.e. v > 1 + W / T
|
|
21
|
+
*
|
|
22
|
+
* On the field torrent: waits every 2.22 s, worst 3.16 s, so the honest bar is
|
|
23
|
+
* 2.42 — against the 1.5 that was assumed, and the 1.05 that was measured. The
|
|
24
|
+
* same arithmetic explains why a copied stream never stalls: at 8x it gains
|
|
25
|
+
* 15.5 s between interruptions and loses at most 4.8 s.
|
|
26
|
+
*
|
|
27
|
+
* **The buffer.** It must cover the worst interruption that can arrive before
|
|
28
|
+
* it can be refilled, whichever source that interruption comes from, plus the
|
|
29
|
+
* segment being played — which must be whole:
|
|
30
|
+
*
|
|
31
|
+
* B = segment duration + max(W_supply, D_production, T_transfer)
|
|
32
|
+
*
|
|
33
|
+
* On the same session that is 7-9 s rather than 25. It rises by itself when a
|
|
34
|
+
* session's interruptions grow, which is what makes it safe to lower: the
|
|
35
|
+
* figure is continuously measured, not chosen once.
|
|
36
|
+
*
|
|
37
|
+
* Every quantity here is measured. Nothing in this file is a coefficient, and
|
|
38
|
+
* nothing smooths, weights or decays anything.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One measured interruption: how long a read waited, and when the wait ended.
|
|
43
|
+
*
|
|
44
|
+
* @typedef {object} SupplyWait
|
|
45
|
+
* @property {number} waitedMs - How long the read was blocked.
|
|
46
|
+
* @property {number} at - Wall-clock ms when the wait ended.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The speed a step must sustain to survive this file's supply on this swarm.
|
|
51
|
+
*
|
|
52
|
+
* Returns null when the evidence does not exist yet — fewer than two waits
|
|
53
|
+
* means no interval has been observed, and an interval invented from one point
|
|
54
|
+
* would be exactly the kind of number this file exists to remove. A caller with
|
|
55
|
+
* null must say it does not know, never substitute a default.
|
|
56
|
+
*
|
|
57
|
+
* @param {SupplyWait[]} waits - Recent waits, in any order.
|
|
58
|
+
* @returns {{ requiredSpeed: number, worstWaitSec: number, medianIntervalSec: number, samples: number } | null}
|
|
59
|
+
*/
|
|
60
|
+
export function requiredSpeedFrom(waits) {
|
|
61
|
+
const ordered = usableWaits(waits);
|
|
62
|
+
if (ordered.length < 2) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const worstWaitSec = Math.max(...ordered.map((wait) => wait.waitedMs)) / 1000;
|
|
66
|
+
const intervals = [];
|
|
67
|
+
for (let index = 1; index < ordered.length; index += 1) {
|
|
68
|
+
const gapMs = ordered[index].at - ordered[index - 1].at;
|
|
69
|
+
if (gapMs > 0) {
|
|
70
|
+
intervals.push(gapMs / 1000);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (intervals.length === 0) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const medianIntervalSec = median(intervals);
|
|
77
|
+
if (!(medianIntervalSec > 0)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
requiredSpeed: 1 + worstWaitSec / medianIntervalSec,
|
|
82
|
+
worstWaitSec,
|
|
83
|
+
medianIntervalSec,
|
|
84
|
+
samples: ordered.length
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The smallest buffer at which no interruption reaches the viewer.
|
|
90
|
+
*
|
|
91
|
+
* Each term is the worst OBSERVED value over a recent window, and a term with
|
|
92
|
+
* nothing observed contributes nothing rather than a guess. The segment is
|
|
93
|
+
* always included: the one being played has to be whole.
|
|
94
|
+
*
|
|
95
|
+
* @param {object} observed
|
|
96
|
+
* @param {number} observed.segmentSeconds - The session's segment duration.
|
|
97
|
+
* @param {number} [observed.worstSupplyWaitSec] - Longest wait for a piece.
|
|
98
|
+
* @param {number} [observed.worstProductionGapSec] - Longest gap between
|
|
99
|
+
* consecutive segments beyond their own length: what a step at 1.0x costs.
|
|
100
|
+
* @param {number} [observed.worstTransferSec] - Longest time to move one
|
|
101
|
+
* segment over the channel to the viewer.
|
|
102
|
+
* @returns {{ seconds: number, from: string } | null} Null when the segment
|
|
103
|
+
* duration is unknown, since then nothing here can be stated.
|
|
104
|
+
*/
|
|
105
|
+
export function minimumBufferFrom(observed = {}) {
|
|
106
|
+
const segmentSeconds = Number(observed.segmentSeconds);
|
|
107
|
+
if (!Number.isFinite(segmentSeconds) || segmentSeconds <= 0) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const terms = [
|
|
111
|
+
{ name: "supply", seconds: positiveOrZero(observed.worstSupplyWaitSec) },
|
|
112
|
+
{ name: "production", seconds: positiveOrZero(observed.worstProductionGapSec) },
|
|
113
|
+
{ name: "transfer", seconds: positiveOrZero(observed.worstTransferSec) }
|
|
114
|
+
];
|
|
115
|
+
const worst = terms.reduce(
|
|
116
|
+
(largest, term) => (term.seconds > largest.seconds ? term : largest),
|
|
117
|
+
{ name: "none", seconds: 0 }
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
seconds: segmentSeconds + worst.seconds,
|
|
121
|
+
// Which interruption sets the figure, so a session's log says what the
|
|
122
|
+
// viewer is actually waiting for rather than only how long.
|
|
123
|
+
from: worst.name
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Waits that can be reasoned about, oldest first.
|
|
129
|
+
*
|
|
130
|
+
* @param {SupplyWait[]} waits
|
|
131
|
+
* @returns {SupplyWait[]}
|
|
132
|
+
*/
|
|
133
|
+
function usableWaits(waits) {
|
|
134
|
+
if (!Array.isArray(waits)) {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
return waits
|
|
138
|
+
.filter((wait) => Number.isFinite(wait?.waitedMs) && wait.waitedMs > 0 && Number.isFinite(wait?.at))
|
|
139
|
+
.sort((left, right) => left.at - right.at);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @param {number[]} values - Non-empty.
|
|
144
|
+
* @returns {number}
|
|
145
|
+
*/
|
|
146
|
+
function median(values) {
|
|
147
|
+
const sorted = [...values].sort((left, right) => left - right);
|
|
148
|
+
const middle = Math.floor(sorted.length / 2);
|
|
149
|
+
return sorted.length % 2 === 0 ? (sorted[middle - 1] + sorted[middle]) / 2 : sorted[middle];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param {unknown} value
|
|
154
|
+
* @returns {number}
|
|
155
|
+
*/
|
|
156
|
+
function positiveOrZero(value) {
|
|
157
|
+
const numeric = Number(value);
|
|
158
|
+
return Number.isFinite(numeric) && numeric > 0 ? numeric : 0;
|
|
159
|
+
}
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
import { findSharedStore } from "../piece-store/shared-piece-store.js";
|
|
24
24
|
import { logger } from "../../utils/logger.js";
|
|
25
25
|
import { askFastestWiresFor, canPlaceRequests } from "./fastest-wires.js";
|
|
26
|
+
import { minimumBufferFrom, requiredSpeedFrom } from "../supply-margin.js";
|
|
26
27
|
|
|
27
28
|
/** Only waits at least this long are reported; sequential reading stays silent. */
|
|
28
29
|
const PIECE_WAIT_LOG_MS = 1_000;
|
|
@@ -304,6 +305,77 @@ function whenPieceReady(torrent, index, cancellation) {
|
|
|
304
305
|
* the media's byte rate should size it in seconds of playback instead.
|
|
305
306
|
* @returns {AsyncGenerator<PieceFragment>}
|
|
306
307
|
*/
|
|
308
|
+
/**
|
|
309
|
+
* The last interruptions this file's readers met, newest last.
|
|
310
|
+
*
|
|
311
|
+
* Bounded and per file, because both figures derived from it describe THIS
|
|
312
|
+
* file on THIS swarm: a piece is 8 MiB here and 512 KiB elsewhere, and a swarm
|
|
313
|
+
* that answers in 200 ms today may not tomorrow. Nothing is stored beyond the
|
|
314
|
+
* process — a restart starts from no evidence, which is the honest state.
|
|
315
|
+
*
|
|
316
|
+
* @type {Map<string, Array<{ waitedMs: number, at: number }>>}
|
|
317
|
+
*/
|
|
318
|
+
const supplyWaits = new Map();
|
|
319
|
+
|
|
320
|
+
/** How many interruptions are kept per file. */
|
|
321
|
+
const SUPPLY_WAIT_HISTORY = 40;
|
|
322
|
+
|
|
323
|
+
/** How often the derived figures are printed, at most. */
|
|
324
|
+
const SUPPLY_REPORT_INTERVAL_MS = 30_000;
|
|
325
|
+
|
|
326
|
+
/** When each file's figures were last printed. */
|
|
327
|
+
const supplyReportedAt = new Map();
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Record one interruption and, at most twice a minute, say what it implies.
|
|
331
|
+
*
|
|
332
|
+
* The two figures are the whole of roadmap item 3: the speed a step must
|
|
333
|
+
* sustain to survive this supply (`1 + worst wait / median interval`), and the
|
|
334
|
+
* smallest buffer that hides an interruption from the viewer. Both are printed
|
|
335
|
+
* before either is USED, so the field says whether the arithmetic describes
|
|
336
|
+
* reality before anything is decided by it.
|
|
337
|
+
*
|
|
338
|
+
* @param {string} key - Something stable per file.
|
|
339
|
+
* @param {string} label - What to call it in the log.
|
|
340
|
+
* @param {number} waitedMs
|
|
341
|
+
* @returns {void}
|
|
342
|
+
*/
|
|
343
|
+
function noteSupplyWait(key, label, waitedMs) {
|
|
344
|
+
const history = supplyWaits.get(key) ?? [];
|
|
345
|
+
history.push({ waitedMs, at: Date.now() });
|
|
346
|
+
while (history.length > SUPPLY_WAIT_HISTORY) {
|
|
347
|
+
history.shift();
|
|
348
|
+
}
|
|
349
|
+
supplyWaits.set(key, history);
|
|
350
|
+
|
|
351
|
+
const now = Date.now();
|
|
352
|
+
if (now - (supplyReportedAt.get(key) ?? 0) < SUPPLY_REPORT_INTERVAL_MS) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const demand = requiredSpeedFrom(history);
|
|
356
|
+
if (!demand) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
supplyReportedAt.set(key, now);
|
|
360
|
+
const buffer = minimumBufferFrom({
|
|
361
|
+
segmentSeconds: SEGMENT_SECONDS_FOR_BUFFER,
|
|
362
|
+
worstSupplyWaitSec: demand.worstWaitSec
|
|
363
|
+
});
|
|
364
|
+
logger.info(
|
|
365
|
+
`supply "${label.slice(0, 40)}": a step must run at ${demand.requiredSpeed.toFixed(2)}x ` +
|
|
366
|
+
`to survive this swarm (worst wait ${demand.worstWaitSec.toFixed(2)}s, one every ` +
|
|
367
|
+
`${demand.medianIntervalSec.toFixed(2)}s, ${demand.samples} measured) — ` +
|
|
368
|
+
`and the smallest buffer that hides it is ${buffer ? buffer.seconds.toFixed(1) : "?"}s`
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The segment length the buffer figure is stated against. The reader does not
|
|
374
|
+
* know the session's own, and this is a REPORT rather than a decision — the
|
|
375
|
+
* decision, when it is made, will use the session's real one.
|
|
376
|
+
*/
|
|
377
|
+
const SEGMENT_SECONDS_FOR_BUFFER = 4;
|
|
378
|
+
|
|
307
379
|
export async function* readFragments({
|
|
308
380
|
torrent,
|
|
309
381
|
fileIndex,
|
|
@@ -495,6 +567,7 @@ export async function* readFragments({
|
|
|
495
567
|
// short, an immediate hit means it is longer than it needs to be. Applied
|
|
496
568
|
// before the logging below so the line reports the window the next piece
|
|
497
569
|
// will actually use.
|
|
570
|
+
noteSupplyWait(`${torrent?.infoHash ?? "?"}/${file?.name ?? "?"}`, file?.name ?? "", waitedMs);
|
|
498
571
|
const widened = nextWindowPieces({
|
|
499
572
|
current: windowPieces,
|
|
500
573
|
base: basePieces,
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file What the supply's own interruptions require of a quality step, and of
|
|
3
|
+
* the buffer in front of the viewer.
|
|
4
|
+
*
|
|
5
|
+
* Both figures are chosen by hand today — a speed margin of 1.5 and a 25-second
|
|
6
|
+
* prebuffer — and both stand for a quantity that is measured every few seconds
|
|
7
|
+
* anyway: how long a read waits for a piece, and how often that happens.
|
|
8
|
+
*
|
|
9
|
+
* The arithmetic, from the measurements of 2026-08-17:
|
|
10
|
+
*
|
|
11
|
+
* A step producing at speed `v` gains `v - 1` seconds of cushion per second
|
|
12
|
+
* of playback. An interruption of `W` seconds costs `W`. So a step survives
|
|
13
|
+
* its own supply exactly when it can rebuild what one interruption takes
|
|
14
|
+
* before the next one arrives:
|
|
15
|
+
*
|
|
16
|
+
* (v - 1) × T > W ⇔ v > 1 + W / T
|
|
17
|
+
*
|
|
18
|
+
* On the field torrent that day: waits of 1.49 s median, 3.16 s worst, one
|
|
19
|
+
* every 2.22 s → a required speed of 1.67 against the 1.5 chosen by hand, and
|
|
20
|
+
* against 1.05 actually measured, which is why it stalled. A copy running at
|
|
21
|
+
* 8x gains 15.5 s between interruptions against 1.5-4.8 s lost, which is the
|
|
22
|
+
* same formula explaining why a copy never stalls.
|
|
23
|
+
*
|
|
24
|
+
* The buffer follows from the same numbers: it must hold the segment being
|
|
25
|
+
* played, whole, plus the worst interruption that can arrive before it can be
|
|
26
|
+
* refilled — whichever source that interruption comes from.
|
|
27
|
+
*
|
|
28
|
+
* B_min = segment duration + max(W_supply, D_production, T_transfer)
|
|
29
|
+
*
|
|
30
|
+
* 7-9 s on that torrent, against the 25 s in the browser today.
|
|
31
|
+
*
|
|
32
|
+
* Every term is measured, none is chosen, and the figures rise by themselves
|
|
33
|
+
* when a session's interruptions grow — which is what makes lowering the buffer
|
|
34
|
+
* safe. Pure: no torrent, no clock of its own, no state beyond the samples it
|
|
35
|
+
* is given.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* How many recent interruptions are kept per file. Enough for a median to mean
|
|
40
|
+
* something, short enough that a swarm which has recovered is not judged by how
|
|
41
|
+
* it behaved ten minutes ago.
|
|
42
|
+
*/
|
|
43
|
+
const SAMPLE_LIMIT = 24;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A reading of one interruption: how long the reader waited, and when.
|
|
47
|
+
*
|
|
48
|
+
* @typedef {object} Interruption
|
|
49
|
+
* @property {number} waitedMs
|
|
50
|
+
* @property {number} at - Epoch milliseconds when the wait ENDED.
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Add one interruption to a record, keeping only the recent ones.
|
|
55
|
+
*
|
|
56
|
+
* @param {Interruption[]} samples - Existing readings, oldest first.
|
|
57
|
+
* @param {Interruption} interruption
|
|
58
|
+
* @returns {Interruption[]} A new array; the input is not modified.
|
|
59
|
+
*/
|
|
60
|
+
export function withInterruption(samples, interruption) {
|
|
61
|
+
const kept = Array.isArray(samples) ? samples : [];
|
|
62
|
+
if (!Number.isFinite(interruption?.waitedMs) || !Number.isFinite(interruption?.at)) {
|
|
63
|
+
return kept;
|
|
64
|
+
}
|
|
65
|
+
const next = [...kept, { waitedMs: Math.max(0, interruption.waitedMs), at: interruption.at }];
|
|
66
|
+
return next.length > SAMPLE_LIMIT ? next.slice(next.length - SAMPLE_LIMIT) : next;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {number[]} values
|
|
71
|
+
* @returns {number}
|
|
72
|
+
*/
|
|
73
|
+
function median(values) {
|
|
74
|
+
if (values.length === 0) {
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
const sorted = [...values].sort((left, right) => left - right);
|
|
78
|
+
return sorted[Math.floor(sorted.length / 2)];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What the recent interruptions amount to.
|
|
83
|
+
*
|
|
84
|
+
* The interval between interruptions is measured between the readings
|
|
85
|
+
* themselves, so a file that is read steadily and rarely blocked reports a long
|
|
86
|
+
* interval and asks little of the step.
|
|
87
|
+
*
|
|
88
|
+
* @param {Interruption[]} samples
|
|
89
|
+
* @returns {{ samples: number, medianWaitSeconds: number, worstWaitSeconds: number, medianGapSeconds: number }}
|
|
90
|
+
*/
|
|
91
|
+
export function summariseInterruptions(samples) {
|
|
92
|
+
const readings = Array.isArray(samples) ? samples : [];
|
|
93
|
+
if (readings.length === 0) {
|
|
94
|
+
return { samples: 0, medianWaitSeconds: 0, worstWaitSeconds: 0, medianGapSeconds: 0 };
|
|
95
|
+
}
|
|
96
|
+
const waits = readings.map((entry) => entry.waitedMs / 1000);
|
|
97
|
+
const gaps = [];
|
|
98
|
+
for (let index = 1; index < readings.length; index += 1) {
|
|
99
|
+
const gap = (readings[index].at - readings[index - 1].at) / 1000;
|
|
100
|
+
if (gap > 0) {
|
|
101
|
+
gaps.push(gap);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
samples: readings.length,
|
|
106
|
+
medianWaitSeconds: median(waits),
|
|
107
|
+
worstWaitSeconds: Math.max(...waits),
|
|
108
|
+
medianGapSeconds: median(gaps)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The speed a step must run at to survive this supply, or null when the supply
|
|
114
|
+
* has not interrupted often enough to say.
|
|
115
|
+
*
|
|
116
|
+
* Null is a real answer and must not be replaced by a number: with one reading
|
|
117
|
+
* there is no interval at all, and inventing one is how a margin comes to be
|
|
118
|
+
* chosen by hand again. A caller with no figure keeps whatever it used before
|
|
119
|
+
* and says so.
|
|
120
|
+
*
|
|
121
|
+
* The worst wait is used rather than the median, because a step that only
|
|
122
|
+
* survives the typical interruption stalls on the others — and a stall is what
|
|
123
|
+
* the viewer sees, not an average.
|
|
124
|
+
*
|
|
125
|
+
* @param {{ samples: number, worstWaitSeconds: number, medianGapSeconds: number }} summary
|
|
126
|
+
* @returns {number | null}
|
|
127
|
+
*/
|
|
128
|
+
export function requiredSpeedFrom(summary) {
|
|
129
|
+
if (!summary || summary.samples < 2 || !(summary.medianGapSeconds > 0)) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
return 1 + summary.worstWaitSeconds / summary.medianGapSeconds;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The smallest buffer at which no spinner appears: the segment being played,
|
|
137
|
+
* whole, plus the worst interruption that can arrive before it can be refilled.
|
|
138
|
+
*
|
|
139
|
+
* Each term is the worst OBSERVED over a recent window, and a term nobody has
|
|
140
|
+
* measured contributes nothing rather than a guess.
|
|
141
|
+
*
|
|
142
|
+
* @param {{ segmentSeconds: number, supplySeconds?: number, productionSeconds?: number, transferSeconds?: number }} terms
|
|
143
|
+
* @returns {number}
|
|
144
|
+
*/
|
|
145
|
+
export function minimumBufferSeconds(terms) {
|
|
146
|
+
const segment = Number.isFinite(terms?.segmentSeconds) && terms.segmentSeconds > 0
|
|
147
|
+
? terms.segmentSeconds
|
|
148
|
+
: 0;
|
|
149
|
+
const worst = Math.max(
|
|
150
|
+
Number.isFinite(terms?.supplySeconds) ? terms.supplySeconds : 0,
|
|
151
|
+
Number.isFinite(terms?.productionSeconds) ? terms.productionSeconds : 0,
|
|
152
|
+
Number.isFinite(terms?.transferSeconds) ? terms.transferSeconds : 0
|
|
153
|
+
);
|
|
154
|
+
return segment + Math.max(0, worst);
|
|
155
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The speed a step needs, and the buffer a viewer needs, from what the
|
|
3
|
+
* supply actually did.
|
|
4
|
+
*
|
|
5
|
+
* The field numbers these are checked against (2026-08-17, one torrent, one
|
|
6
|
+
* session): waits of 1.49 s median and 3.16 s worst, one every 2.22 s. The
|
|
7
|
+
* margin chosen by hand was 1.5; the arithmetic says 1.67; the step measured
|
|
8
|
+
* 1.05 and stalled.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import assert from "node:assert/strict";
|
|
12
|
+
import test from "node:test";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
minimumBufferSeconds,
|
|
16
|
+
requiredSpeedFrom,
|
|
17
|
+
summariseInterruptions,
|
|
18
|
+
withInterruption
|
|
19
|
+
} from "../services/torrent-worker/supply-interruptions.js";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Interruptions of `waitMs`, one every `gapMs`.
|
|
23
|
+
*
|
|
24
|
+
* @param {number[]} waitsMs
|
|
25
|
+
* @param {number} gapMs
|
|
26
|
+
* @returns {Array<{ waitedMs: number, at: number }>}
|
|
27
|
+
*/
|
|
28
|
+
function series(waitsMs, gapMs) {
|
|
29
|
+
let samples = [];
|
|
30
|
+
let at = 1_000_000;
|
|
31
|
+
for (const waitedMs of waitsMs) {
|
|
32
|
+
samples = withInterruption(samples, { waitedMs, at });
|
|
33
|
+
at += gapMs;
|
|
34
|
+
}
|
|
35
|
+
return samples;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test("the field session's numbers produce the field session's answer", () => {
|
|
39
|
+
// Waits around 1.49 s with a worst of 3.16 s, one every 2.22 s.
|
|
40
|
+
const samples = series([1490, 1490, 3160, 1490, 1490], 2220);
|
|
41
|
+
const summary = summariseInterruptions(samples);
|
|
42
|
+
|
|
43
|
+
assert.equal(summary.worstWaitSeconds, 3.16);
|
|
44
|
+
assert.equal(summary.medianGapSeconds, 2.22);
|
|
45
|
+
|
|
46
|
+
const required = requiredSpeedFrom(summary);
|
|
47
|
+
assert.ok(required !== null);
|
|
48
|
+
assert.ok(
|
|
49
|
+
Math.abs(required - 2.42) < 0.01,
|
|
50
|
+
`1 + 3.16/2.22 = ${required?.toFixed(2)} — the supply asks for it, nobody chose it`
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("a supply that rarely interrupts asks for almost nothing", () => {
|
|
55
|
+
// One short wait every couple of minutes: a step barely faster than realtime
|
|
56
|
+
// rebuilds the cushion long before the next one.
|
|
57
|
+
const summary = summariseInterruptions(series([200, 200, 200], 120_000));
|
|
58
|
+
const required = requiredSpeedFrom(summary);
|
|
59
|
+
assert.ok(required !== null && required < 1.01, `asked ${required}`);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("a supply that interrupts constantly asks for a great deal", () => {
|
|
63
|
+
const summary = summariseInterruptions(series([4000, 4000, 4000], 1000));
|
|
64
|
+
assert.equal(requiredSpeedFrom(summary), 5);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("too little evidence is answered with nothing, never with a number", () => {
|
|
68
|
+
// The whole point of deriving the margin is that it stops being invented. One
|
|
69
|
+
// reading has no interval at all, and a caller must keep what it had.
|
|
70
|
+
assert.equal(requiredSpeedFrom(summariseInterruptions([])), null);
|
|
71
|
+
assert.equal(requiredSpeedFrom(summariseInterruptions(series([1000], 0))), null);
|
|
72
|
+
assert.equal(requiredSpeedFrom(null), null);
|
|
73
|
+
assert.equal(requiredSpeedFrom({ samples: 9, worstWaitSeconds: 3, medianGapSeconds: 0 }), null);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("the worst wait decides, not the typical one", () => {
|
|
77
|
+
// A step that survives the median interruption still stalls on the others,
|
|
78
|
+
// and a stall is what the viewer sees.
|
|
79
|
+
const summary = summariseInterruptions(series([100, 100, 5000, 100], 1000));
|
|
80
|
+
assert.equal(summary.medianWaitSeconds, 0.1);
|
|
81
|
+
assert.equal(summary.worstWaitSeconds, 5);
|
|
82
|
+
assert.equal(requiredSpeedFrom(summary), 6);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("only the recent interruptions are judged", () => {
|
|
86
|
+
// A swarm that has recovered must not be sentenced by how it behaved ten
|
|
87
|
+
// minutes ago, so the record is bounded.
|
|
88
|
+
let samples = series(Array.from({ length: 40 }, () => 9000), 1000);
|
|
89
|
+
samples = withInterruption(samples, { waitedMs: 10, at: 2_000_000 });
|
|
90
|
+
assert.ok(samples.length <= 24, `kept ${samples.length}`);
|
|
91
|
+
assert.equal(samples[samples.length - 1].waitedMs, 10);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("a reading without a time or a duration is not a reading", () => {
|
|
95
|
+
const samples = withInterruption([], { waitedMs: Number.NaN, at: 1 });
|
|
96
|
+
assert.deepEqual(samples, []);
|
|
97
|
+
assert.deepEqual(withInterruption([], { waitedMs: 100 }), []);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("the minimum buffer is one segment plus the worst interruption", () => {
|
|
101
|
+
// The field torrent: 4 s segments, a worst supply wait of 3.16 s, production
|
|
102
|
+
// gaps within the segment length, transfer measured in milliseconds.
|
|
103
|
+
const seconds = minimumBufferSeconds({
|
|
104
|
+
segmentSeconds: 4,
|
|
105
|
+
supplySeconds: 3.16,
|
|
106
|
+
productionSeconds: 1.2,
|
|
107
|
+
transferSeconds: 0.066
|
|
108
|
+
});
|
|
109
|
+
assert.ok(Math.abs(seconds - 7.16) < 0.001, `${seconds}s against the 25 s chosen by hand`);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("whichever source is worst is the one that sizes the buffer", () => {
|
|
113
|
+
// A step at 1.0x makes production the binding term even on a swarm that never
|
|
114
|
+
// stalls, which is exactly the case a supply-only figure would miss.
|
|
115
|
+
assert.equal(
|
|
116
|
+
minimumBufferSeconds({ segmentSeconds: 4, supplySeconds: 0.2, productionSeconds: 6 }),
|
|
117
|
+
10
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("a term nobody measured contributes nothing, not a guess", () => {
|
|
122
|
+
assert.equal(minimumBufferSeconds({ segmentSeconds: 4 }), 4);
|
|
123
|
+
assert.equal(minimumBufferSeconds({ segmentSeconds: 4, supplySeconds: Number.NaN }), 4);
|
|
124
|
+
assert.equal(minimumBufferSeconds({}), 0);
|
|
125
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The margin and the buffer, checked against the session they were
|
|
3
|
+
* derived from.
|
|
4
|
+
*
|
|
5
|
+
* The figures in these tests are the field measurements of 2026-08-17, so a
|
|
6
|
+
* change that breaks the arithmetic fails against reality rather than against
|
|
7
|
+
* an example someone invented.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import test from "node:test";
|
|
12
|
+
|
|
13
|
+
import { minimumBufferFrom, requiredSpeedFrom } from "../services/supply-margin.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Waits spaced by `intervalSec`, each lasting `waitSec`.
|
|
17
|
+
*
|
|
18
|
+
* @param {number} count
|
|
19
|
+
* @param {number} intervalSec
|
|
20
|
+
* @param {number} waitSec
|
|
21
|
+
* @returns {Array<{ waitedMs: number, at: number }>}
|
|
22
|
+
*/
|
|
23
|
+
function evenlySpaced(count, intervalSec, waitSec) {
|
|
24
|
+
const waits = [];
|
|
25
|
+
for (let index = 0; index < count; index += 1) {
|
|
26
|
+
waits.push({ waitedMs: waitSec * 1000, at: 1_000_000 + index * intervalSec * 1000 });
|
|
27
|
+
}
|
|
28
|
+
return waits;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("the margin is what the supply's own interruptions demand", () => {
|
|
32
|
+
// The field torrent: a wait every 2.22 s, the worst of them 3.16 s.
|
|
33
|
+
const waits = evenlySpaced(10, 2.22, 1.49);
|
|
34
|
+
waits[4].waitedMs = 3160;
|
|
35
|
+
|
|
36
|
+
const answer = requiredSpeedFrom(waits);
|
|
37
|
+
|
|
38
|
+
assert.ok(answer);
|
|
39
|
+
assert.equal(answer.worstWaitSec, 3.16);
|
|
40
|
+
assert.equal(answer.medianIntervalSec, 2.22);
|
|
41
|
+
// 1 + 3.16 / 2.22 = 2.42. The step that was admitted by the hand-chosen 1.5
|
|
42
|
+
// ran at 1.05x and stalled; this is the bar it should have been held to.
|
|
43
|
+
assert.ok(Math.abs(answer.requiredSpeed - 2.4234) < 0.001, `got ${answer.requiredSpeed}`);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a copy at 8x clears its own supply with room to spare", () => {
|
|
47
|
+
// The same file's copied stream: waits up to 4.82 s, and it never stalled.
|
|
48
|
+
const waits = evenlySpaced(6, 15.5, 4.82);
|
|
49
|
+
const answer = requiredSpeedFrom(waits);
|
|
50
|
+
assert.ok(answer);
|
|
51
|
+
// 1 + 4.82/15.5 = 1.31, against 8x measured. Which is why a copy is the step
|
|
52
|
+
// a stranded viewer is always able to return to.
|
|
53
|
+
assert.ok(answer.requiredSpeed < 1.35, `got ${answer.requiredSpeed}`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("with too little evidence it says so instead of inventing a number", () => {
|
|
57
|
+
assert.equal(requiredSpeedFrom([]), null);
|
|
58
|
+
assert.equal(requiredSpeedFrom([{ waitedMs: 1500, at: 1 }]), null, "one wait shows no interval");
|
|
59
|
+
assert.equal(requiredSpeedFrom(null), null);
|
|
60
|
+
// Every wait at the same instant: no interval was observed, so no interval
|
|
61
|
+
// may be stated.
|
|
62
|
+
assert.equal(
|
|
63
|
+
requiredSpeedFrom([
|
|
64
|
+
{ waitedMs: 1000, at: 5 },
|
|
65
|
+
{ waitedMs: 1000, at: 5 }
|
|
66
|
+
]),
|
|
67
|
+
null
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("readings that are not measurements are ignored, not averaged in", () => {
|
|
72
|
+
const answer = requiredSpeedFrom([
|
|
73
|
+
{ waitedMs: 0, at: 1000 },
|
|
74
|
+
{ waitedMs: Number.NaN, at: 2000 },
|
|
75
|
+
{ waitedMs: 1000, at: 3000 },
|
|
76
|
+
{ waitedMs: 2000, at: 5000 },
|
|
77
|
+
{ waitedMs: 1500, at: 7000 }
|
|
78
|
+
]);
|
|
79
|
+
assert.ok(answer);
|
|
80
|
+
assert.equal(answer.samples, 3, "only the three real waits count");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("the buffer is one segment plus the worst interruption, and names which", () => {
|
|
84
|
+
const answer = minimumBufferFrom({
|
|
85
|
+
segmentSeconds: 4,
|
|
86
|
+
worstSupplyWaitSec: 3.16,
|
|
87
|
+
worstProductionGapSec: 1.2,
|
|
88
|
+
worstTransferSec: 0.066
|
|
89
|
+
});
|
|
90
|
+
assert.ok(answer);
|
|
91
|
+
assert.equal(answer.seconds, 7.16, "7.16 s against the 25 s that was chosen by hand");
|
|
92
|
+
assert.equal(answer.from, "supply");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("a step that cannot keep up sets the buffer itself", () => {
|
|
96
|
+
const answer = minimumBufferFrom({
|
|
97
|
+
segmentSeconds: 4,
|
|
98
|
+
worstSupplyWaitSec: 1.4,
|
|
99
|
+
worstProductionGapSec: 6.5,
|
|
100
|
+
worstTransferSec: 0.05
|
|
101
|
+
});
|
|
102
|
+
assert.equal(answer.seconds, 10.5);
|
|
103
|
+
assert.equal(answer.from, "production", "the encoder, not the swarm, is what the viewer is waiting for");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("a term with nothing observed contributes nothing", () => {
|
|
107
|
+
const answer = minimumBufferFrom({ segmentSeconds: 4 });
|
|
108
|
+
assert.equal(answer.seconds, 4, "one whole segment is the floor: the one being played");
|
|
109
|
+
assert.equal(answer.from, "none");
|
|
110
|
+
assert.equal(minimumBufferFrom({}), null, "without a segment duration nothing can be said");
|
|
111
|
+
assert.equal(minimumBufferFrom({ segmentSeconds: 0 }), null);
|
|
112
|
+
});
|