Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update uploader() to slow down initial message doubling #45

Merged
merged 12 commits into from
May 14, 2021
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@m-lab/ndt7",
"version": "0.0.4",
"version": "0.0.5-beta.0",
"description": "NDT7 client for measuring networks",
"main": "src/ndt7.js",
"scripts": {
Expand Down
33 changes: 11 additions & 22 deletions src/ndt7-upload-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,37 +74,26 @@ const uploadTest = function(sock, postMessage, now) {
// observed this behaviour with pre-Chromium Edge.
return;
}
let t = now();
const t = now();
if (t >= end) {
sock.close();
return;
}

const maxMessageSize = 16777216; /* = (1<<24) = 16MB */
const nextSizeIncrement =
(data.length >= maxMessageSize) ? Infinity : 16 * data.length;
if (total >= nextSizeIncrement) {
// Optional todo: fill this message with randomness.
const maxMessageSize = 8388608; /* = (1<<23) = 8MB */
const clientMeasurementInterval = 250; // ms

// Message size is doubled every 16 messages, up to maxMessageSize.
if (data.length < maxMessageSize &&
data.length < (total - sock.bufferedAmount) / 16) {
data = new Uint8Array(data.length * 2);
}

const clientMeasurementInterval = 250; // ms
const loopEndTime = Math.min(previous + clientMeasurementInterval, end);
const desiredBuffer = 8 * data.length;

// While we would still like to buffer more messages, and we haven't been
// running for too long, and we don't need to resize the message... keep
// sending.
//
// The buffering bound prevents us from wasting local memory, the time bound
// prevents us from stalling the UI event loop, and the sizeIncrement bound
// allows us to dynamically respond to fast connections.
while (sock.bufferedAmount < desiredBuffer &&
t < loopEndTime &&
total < nextSizeIncrement
) {
// We keep 7 messages in the send buffer, so there is always some more
// data to send. The maximum buffer size is 8 * 8MB - 1 byte ~= 64M.
const desiredBuffer = 7 * data.length;
if (sock.bufferedAmount < desiredBuffer) {
sock.send(data);
t = now();
total += data.length;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ndt7-upload-worker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.