@torrent-tv/proxy 2.78.0 → 2.80.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.
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @file What starting and stopping an encoder costs on THIS host.
3
+ *
4
+ * Two figures, and both are terms in the one decision anybody makes about a
5
+ * running encoder: let it drive on through material that already exists, or
6
+ * stop it and start another where the material is missing.
7
+ *
8
+ * Before this file neither was measured here. The start was a single reading
9
+ * taken once on one machine and written into the code as a constant; the stop
10
+ * and the wait for the first output were not counted at all, so the comparison
11
+ * priced only one side of itself and always answered the same way.
12
+ *
13
+ * Readings are kept and read as their median: one starved start must not decide
14
+ * the rule, and a host that has since become busy must be able to change the
15
+ * answer.
16
+ */
17
+
18
+ /**
19
+ * How many recent readings a figure is taken from. The same reasoning as the
20
+ * other learned figures in this proxy: long enough that one reading does not
21
+ * move the answer, short enough that the answer still follows the host.
22
+ */
23
+ const RECENT_READINGS = 20;
24
+
25
+ /**
26
+ * The middle of a set of readings, or null when there are none.
27
+ *
28
+ * Written here rather than borrowed from the general helper one level up: this
29
+ * layer states facts and imports nothing above itself, and four lines of
30
+ * arithmetic are not worth breaking that for.
31
+ *
32
+ * @param {number[]} values
33
+ * @returns {number | null}
34
+ */
35
+ function middleOf(values) {
36
+ if (values.length === 0) {
37
+ return null;
38
+ }
39
+ const sorted = [...values].sort((left, right) => left - right);
40
+ const middle = Math.floor(sorted.length / 2);
41
+ return sorted.length % 2 === 0 ? (sorted[middle - 1] + sorted[middle]) / 2 : sorted[middle];
42
+ }
43
+
44
+ export class RunCosts {
45
+ /** How long dying took, in milliseconds. @type {number[]} */
46
+ #dying = [];
47
+
48
+ /** How long the first output took to appear, in milliseconds. @type {number[]} */
49
+ #firstOutput = [];
50
+
51
+ /**
52
+ * Take the two readings a finished run carries. Either may be absent — a run
53
+ * that was never told to stop did not die on command, and one that produced
54
+ * nothing has no first output — and an absent reading is not a zero.
55
+ *
56
+ * @param {{ dyingMs?: number | null, firstOutputMs?: number | null }} ended
57
+ */
58
+ note(ended) {
59
+ if (Number.isFinite(ended?.dyingMs)) {
60
+ RunCosts.#keep(this.#dying, /** @type {number} */ (ended.dyingMs));
61
+ }
62
+ if (Number.isFinite(ended?.firstOutputMs)) {
63
+ RunCosts.#keep(this.#firstOutput, /** @type {number} */ (ended.firstOutputMs));
64
+ }
65
+ }
66
+
67
+ /**
68
+ * @param {number[]} readings
69
+ * @param {number} value
70
+ */
71
+ static #keep(readings, value) {
72
+ readings.push(value);
73
+ while (readings.length > RECENT_READINGS) {
74
+ readings.shift();
75
+ }
76
+ }
77
+
78
+ /**
79
+ * The two costs in seconds, from this host's own readings.
80
+ *
81
+ * Zero where nothing has been measured yet. Zero understates both, so a plan
82
+ * that has no readings prices moving an encoder as cheaper than it is — which
83
+ * is why the plan keeps a run it cannot compare rather than moving it.
84
+ *
85
+ * @returns {{ killCostSec: number, firstByteWaitSec: number, samples: number }}
86
+ */
87
+ seconds() {
88
+ return {
89
+ killCostSec: (middleOf(this.#dying) ?? 0) / 1000,
90
+ firstByteWaitSec: (middleOf(this.#firstOutput) ?? 0) / 1000,
91
+ samples: Math.min(this.#dying.length, this.#firstOutput.length)
92
+ };
93
+ }
94
+ }