aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests/harness/mse_harness.mjs
blob: f0fd04c4a54860e3b68147c5afa2b43c7d4e3b3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Run the SHIPPED player functions against a fake SourceBuffer.
//
// The point is that nothing here is a paraphrase of app.js: `bufferedAhead`,
// `evictBehind`, `flushQueue` and `pump` are lifted out of the file as text and
// executed. A model of a fix, written by whoever wrote the fix, agrees with it
// by construction — which is how a passing test sat next to a player that still
// hung. What is modelled here is the *browser*: a SourceBuffer with a ceiling,
// and `updateend` firing for removals as well as appends.
//
// Usage: node mse_harness.mjs <path to app.js> <json config>
import { readFileSync } from 'fs';

const app = readFileSync(process.argv[2], 'utf8');
const cfg = JSON.parse(process.argv[3] || '{}');

const {
  playing = false,          // does the viewer actually press play
  capMB = 100,              // where the browser refuses the append
  fileMB = 493.5,           // the film, from a real upload
  durationS = 3936,
  netMBs = 35,              // measured node throughput
  wallS = 600,
} = cfg;

const grab = (name) => {
  const start = app.indexOf(`const ${name} = useCallback(`);
  if (start < 0) throw new Error(`${name} not found in app.js`);
  const deps = app.indexOf('\n  }, [', start);
  const end = app.indexOf(');', deps) + 2;
  return app.slice(start, end);
};

const useCallback = (fn) => fn;
// Sorted by where they appear in app.js, not by the order this list happens to
// be written in. Extracting into an order of our own would quietly repair a
// hook declared before its own dependency — a real fault, which reached
// production once, and which the harness is otherwise well placed to catch.
const src = ['currentRange', 'bufferedAhead', 'aheadLimit', 'evictBehind',
             'flushQueue', 'pump']
  .sort((a, b) => app.indexOf(`const ${a} = useCallback(`)
                - app.indexOf(`const ${b} = useCallback(`))
  .map(grab).join('\n');

const SEG = 256 * 1024;
const CAP = capMB * 1048576;
const BITRATE = fileMB * 1048576 / durationS;

// Read from app.js too, so a change to the constants is a change to the test.
// Arithmetic is allowed because some of them are written as one (`48 * 1024 *
// 1024`), and refusing anything that is not arithmetic keeps this a reader
// rather than an evaluator of whatever happens to be on the line.
const constOf = (name) => {
  const m = app.match(new RegExp(`const ${name} = ([^;]+);`));
  if (!m) throw new Error(`${name} not found`);
  if (!/^[\d\s.*/+-]+$/.test(m[1])) throw new Error(`${name} is not a number`);
  return Number(new Function(`return (${m[1]});`)());
};
const BUFFER_BEHIND_S = constOf('BUFFER_BEHIND_S');
const STREAM_WINDOW = constOf('STREAM_WINDOW');
const BUFFER_AHEAD_S = constOf('BUFFER_AHEAD_S');
const BUFFER_AHEAD_STEP_BYTES = constOf('BUFFER_AHEAD_STEP_BYTES');
const BUFFER_AHEAD_MAX_BYTES = constOf('BUFFER_AHEAD_MAX_BYTES');
const BUFFER_AHEAD_MAX_S = constOf('BUFFER_AHEAD_MAX_S');
const QUEUE_HIGH_WATER = constOf('QUEUE_HIGH_WATER');
const CREDIT_KEEPALIVE_MS = constOf('CREDIT_KEEPALIVE_MS');

let bytes = 0, ranges = [], appended = 0, removes = 0;
let granted = 0, keepalives = 0, sent = 0, credit = 0, quotaRefusals = 0;

const sb = {
  updating: false,
  get buffered() {
    return {
      get length() { return ranges.length; },
      start: (i) => ranges[i][0],
      end: (i) => ranges[i][1],
    };
  },
  appendBuffer(chunk) {
    if (bytes + chunk.byteLength > CAP) {
      quotaRefusals++;
      const e = new Error('quota'); e.name = 'QuotaExceededError'; throw e;
    }
    const at = ranges.length ? ranges[ranges.length - 1][1] : 0;
    const end = at + chunk.byteLength / BITRATE;
    // A real SourceBuffer coalesces contiguous ranges: `buffered` reports the
    // spans of media it holds, not the appends that built them. Pushing one
    // range per segment made every range a couple of seconds long, which is
    // invisible to code that reads `end(length - 1)` and fatal to code that
    // looks for the range around the playhead.
    if (ranges.length && Math.abs(ranges[ranges.length - 1][1] - at) < 0.001) {
      ranges[ranges.length - 1][1] = end;
    } else {
      ranges.push([at, end]);
    }
    bytes += chunk.byteLength;
    appended++;
  },
  remove(a, b) {
    removes++;
    // remove(a, b) takes a span out of whatever it overlaps, trimming a range
    // rather than only dropping whole ones — otherwise a coalesced range is
    // never evicted at all and the buffer grows without limit.
    let dropped = 0;
    const kept = [];
    for (const [s, e] of ranges) {
      if (e <= a || s >= b) { kept.push([s, e]); continue; }
      if (s < a) kept.push([s, a]);
      if (e > b) kept.push([b, e]);
      dropped += (Math.min(e, b) - Math.max(s, a)) * BITRATE;
    }
    ranges = kept;
    bytes -= dropped;
    // A real remove() is asynchronous and fires updateend when it lands. That
    // event is indistinguishable from an append's unless the player kept track.
    pendingRemoveEvents++;
  },
};
let pendingRemoveEvents = 0;

// `paused` is part of the element the player reads, not decoration: the
// read-ahead budget only grows while the film is running, and a fake video
// that never reports being paused would exercise the growing branch in a run
// whose whole point is that nobody pressed play.
const video = { currentTime: 0, paused: !playing };
const sbRef = { current: sb }, videoRef = { current: video };
const msRef = { current: { readyState: 'open', endOfStream() {} } };
const queueRef = { current: [] };
const appendingRef = { current: false }, endedRef = { current: false };
const outstandingRef = { current: 0 }, lastPokeRef = { current: 0 };
const quotaRef = { current: 0 }, stalledRef = { current: false };
const awaitingInitRef = { current: false };
// The film's own average bitrate, as `stream_init` gives the player, and the
// budget state the read-ahead walks up from. Both start where the component
// starts them.
const bitrateRef = { current: BITRATE };
const aheadBytesRef = { current: 0 };
const aheadCapRef = { current: BUFFER_AHEAD_MAX_BYTES };
const quotaHoldRef = { current: false };
const transportRef = {
  current: {
    connected: true,
    grantStreamCredit(n) {
      if (n === 0) { keepalives++; return; }
      granted += n; credit += n;
    },
  },
};

const fns = new Function(
  'sbRef,videoRef,msRef,queueRef,appendingRef,endedRef,outstandingRef,' +
  'lastPokeRef,transportRef,BUFFER_BEHIND_S,BUFFER_AHEAD_S,QUEUE_HIGH_WATER,' +
  'CREDIT_KEEPALIVE_MS,STREAM_WINDOW,quotaRef,stalledRef,awaitingInitRef,' +
  'bitrateRef,aheadBytesRef,aheadCapRef,quotaHoldRef,' +
  'BUFFER_AHEAD_STEP_BYTES,' +
  'BUFFER_AHEAD_MAX_BYTES,BUFFER_AHEAD_MAX_S,console,useCallback',
  src + '\n return {currentRange, bufferedAhead, aheadLimit, evictBehind,'
      + ' flushQueue, pump};'
)(sbRef, videoRef, msRef, queueRef, appendingRef, endedRef, outstandingRef,
  lastPokeRef, transportRef, BUFFER_BEHIND_S, BUFFER_AHEAD_S, QUEUE_HIGH_WATER,
  CREDIT_KEEPALIVE_MS, STREAM_WINDOW, quotaRef, stalledRef, awaitingInitRef,
  bitrateRef, aheadBytesRef, aheadCapRef, quotaHoldRef, BUFFER_AHEAD_STEP_BYTES,
  BUFFER_AHEAD_MAX_BYTES, BUFFER_AHEAD_MAX_S, console, useCallback);

// The player's own `updateend` listener, transcribed — the one part of the
// component that is a listener rather than a callback, and the place the
// remove/append distinction lives.
const updateend = () => {
  appendingRef.current = false;
  fns.pump();
};

credit = STREAM_WINDOW;
outstandingRef.current = STREAM_WINDOW;
let wall = 0;
const TICK = 0.05;
// Bytes the link has carried but not yet spent on a whole segment. This used
// to be re-created inside the loop and thrown away at the end of every tick,
// so a link slower than one segment per tick — 5 MB/s at this resolution,
// which is every mobile network there is — delivered *nothing at all* and the
// run reported a player that had simply never been fed. Anything below about
// forty megabits was unmeasurable here, which is most of the cases worth
// measuring. Carried over instead, and capped at one window's worth, because
// a link cannot bank a burst larger than what the node may have in flight.
let netBudget = 0;
while (wall < wallS) {
  wall += TICK;
  if (playing) {
    const end = ranges.length ? ranges[ranges.length - 1][1] : 0;
    video.currentTime = Math.min(video.currentTime + TICK, end);
  }
  fns.pump();                                    // the 1 s timer and timeupdate
  while (pendingRemoveEvents > 0) { pendingRemoveEvents--; updateend(); }

  netBudget = Math.min(netBudget + netMBs * 1048576 * TICK, SEG * STREAM_WINDOW);
  while (credit > 0 && netBudget >= SEG) {
    credit--; netBudget -= SEG; sent++;
    outstandingRef.current = Math.max(0, outstandingRef.current - 1);
    queueRef.current.push({ byteLength: SEG });
    fns.flushQueue();
    if (appendingRef.current) updateend();
    while (pendingRemoveEvents > 0) { pendingRemoveEvents--; updateend(); }
  }
}

console.log(JSON.stringify({
  sentMB: +(sent * SEG / 1048576).toFixed(1),
  heldInBufferMB: +(bytes / 1048576).toFixed(1),
  queueDepth: queueRef.current.length,
  bufferedAheadS: +fns.bufferedAhead().toFixed(1),
  aheadLimitS: +fns.aheadLimit().toFixed(1),
  budgetMB: +(aheadBytesRef.current / 1048576).toFixed(1),
  budgetCapMB: +(aheadCapRef.current / 1048576).toFixed(1),
  bitrateMbits: +(BITRATE * 8 / 1e6).toFixed(2),
  watchedS: +video.currentTime.toFixed(1),
  grants: granted,
  keepalives,
  removes,
  quotaRefusals: quotaRef.current,
  hitCeiling: bytes >= CAP * 0.99,
}));