forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer-sizer.ts
More file actions
140 lines (130 loc) · 4.04 KB
/
buffer-sizer.ts
File metadata and controls
140 lines (130 loc) · 4.04 KB
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
import type {LogContext} from '@rocicorp/logger';
import {assert} from './asserts.js';
export class BufferSizer {
#bufferSizeMs: number;
readonly #initialBufferSizeMs: number;
readonly #minBufferSizeMs: number;
readonly #maxBufferSizeMs: number;
readonly #adjustBufferSizeIntervalMs: number;
#bufferNeededMsHistory: number[] = [];
#missableCountSinceLastBufferAdjust = 0;
#missedCountSinceLastBufferAdjust = 0;
#timeOfLastBufferAdjust = -1;
#ignoreNextMissable = false;
constructor(options: {
initialBufferSizeMs: number;
minBufferSizeMs: number;
maxBufferSizeMs: number;
adjustBufferSizeIntervalMs: number;
}) {
assert(options.minBufferSizeMs <= options.maxBufferSizeMs);
assert(options.initialBufferSizeMs >= options.minBufferSizeMs);
assert(options.initialBufferSizeMs <= options.maxBufferSizeMs);
assert(options.adjustBufferSizeIntervalMs > 0);
this.#initialBufferSizeMs = options.initialBufferSizeMs;
this.#minBufferSizeMs = options.minBufferSizeMs;
this.#maxBufferSizeMs = options.maxBufferSizeMs;
this.#adjustBufferSizeIntervalMs = options.adjustBufferSizeIntervalMs;
this.#bufferSizeMs = this.#initialBufferSizeMs;
}
get bufferSizeMs() {
return this.#bufferSizeMs;
}
recordMissable(
now: number,
missed: boolean,
bufferNeededMs: number,
lc: LogContext,
) {
if (this.#ignoreNextMissable) {
this.#ignoreNextMissable = false;
return;
}
lc = lc.withContext('BufferSizer');
this.#bufferNeededMsHistory.push(bufferNeededMs);
this.#missableCountSinceLastBufferAdjust++;
if (missed) {
this.#missedCountSinceLastBufferAdjust++;
}
if (this.#timeOfLastBufferAdjust === -1) {
this.#timeOfLastBufferAdjust = now;
return;
}
if (now - this.#timeOfLastBufferAdjust < this.#adjustBufferSizeIntervalMs) {
return;
}
if (this.#missableCountSinceLastBufferAdjust < 200) {
return;
}
this.#bufferNeededMsHistory.sort((a, b) => a - b);
const targetBufferNeededMs =
this.#bufferNeededMsHistory[
Math.floor((this.#bufferNeededMsHistory.length * 99.5) / 100)
];
const bufferSizeMs = this.#bufferSizeMs;
lc.info?.(
'bufferSizeMs',
bufferSizeMs,
'targetBufferNeededMs',
targetBufferNeededMs,
'this._maxBufferNeededMs.length',
this.#bufferNeededMsHistory.length,
'percentile index',
Math.floor((this.#bufferNeededMsHistory.length * 99.5) / 100),
this.#bufferNeededMsHistory,
);
let newBufferSizeMs = bufferSizeMs;
const missPercent =
this.#missedCountSinceLastBufferAdjust /
this.#missableCountSinceLastBufferAdjust;
if (missPercent > 0.01) {
newBufferSizeMs = Math.min(
this.#maxBufferSizeMs,
Math.max(bufferSizeMs, targetBufferNeededMs),
);
lc.info?.(
'High miss percent',
missPercent,
'over last',
now - this.#timeOfLastBufferAdjust,
'ms.',
);
} else if (missPercent < 0.005) {
newBufferSizeMs = Math.max(
this.#minBufferSizeMs,
Math.min(bufferSizeMs, targetBufferNeededMs),
);
lc.info?.(
'Low miss percent',
missPercent,
'over last',
now - this.#timeOfLastBufferAdjust,
'ms.',
);
}
if (bufferSizeMs !== newBufferSizeMs) {
lc.info?.(
'Adjusting buffer',
newBufferSizeMs > bufferSizeMs ? 'up' : 'down',
'from',
bufferSizeMs,
'to',
newBufferSizeMs,
);
}
this.#bufferNeededMsHistory = [];
this.#missableCountSinceLastBufferAdjust = 0;
this.#missedCountSinceLastBufferAdjust = 0;
this.#timeOfLastBufferAdjust = now;
this.#bufferSizeMs = newBufferSizeMs;
this.#ignoreNextMissable = true;
}
reset() {
this.#bufferSizeMs = this.#initialBufferSizeMs;
this.#bufferNeededMsHistory = [];
this.#missableCountSinceLastBufferAdjust = 0;
this.#missedCountSinceLastBufferAdjust = 0;
this.#timeOfLastBufferAdjust = -1;
this.#ignoreNextMissable = false;
}
}