Skip to content

Commit

Permalink
Intermediate commit, but it works for downloading!
Browse files Browse the repository at this point in the history
  • Loading branch information
pboothe committed May 29, 2020
1 parent 3998c53 commit 7c6db29
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
29 changes: 14 additions & 15 deletions js/ndt7-download-worker.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
/* jshint esversion: 6, asi: true, worker: true */
// WebWorker that runs the ndt7 download test

// When run by Node.js, we need to import the websocket libraries.
if (typeof WebSocket === 'undefined') {
global.WebSocket = require('isomorphic-ws');
}

this.onerror = function (e) { console.log(e); handleException(); }

this.onmessage = function (ev) {
self.onmessage = function (ev) {
'use strict'
console.log("got message:", ev);
// TODO put the choce between secure and insecure here
//let url = new URL(ev.data.href)
//url.protocol = (url.protocol === 'https:') ? 'wss:' : 'ws:'
//url.pathname = '/ndt/v7/download'
const url = ev.data['ws:///ndt/v7/download']
console.log("Connecting to " + url)
return;
const sock = new WebSocket(url, 'net.measurementlab.ndt.v7')
console.log("Made websocket object")

sock.onclose = function () {
postMessage({
Expand All @@ -36,27 +34,28 @@ this.onmessage = function (ev) {
let total = 0

sock.onmessage = function (ev) {
total += (ev.data instanceof Blob) ? ev.data.size : ev.data.length

total += (ev.data.hasOwnProperty('size')) ? ev.data.size : ev.data.length
// Perform a client-side measurement 4 times per second.
let now = new Date().getTime()
const every = 250 // ms
if (now - previous > every) {
postMessage({
Data: {
'ElapsedTime': (now - start) * 1000, // us
'NumBytes': total,
MsgType: 'measurement',
ClientData: {
ElapsedTime: (now - start) * 1000, // us
NumBytes: total,
MeanClientMbps: total*8 / (now - start) / 1000 // Bytes * 8 bits/byte * 1/(duration in ms) * 1000ms/s * 1 Mb / 1000000 bits = Mb/s
},
Source: 'client',
})
previous = now
}

// Pass along every server-side measurement.
if (!(ev.data instanceof Blob)) {
let m = JSON.parse(ev.data)
if (typeof ev.data === 'string') {
postMessage({
Data: m,
MsgType: 'measurement',
ServerMessage: ev.data,
Source: 'server',
})
}
Expand Down
21 changes: 16 additions & 5 deletions js/ndt7-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@

'use strict';

global.fetch = require('node-fetch');
global.Worker = require('webworker-threads').Worker;
global.WebSocket = require('isomorphic-ws');

const ndt7 = require('./ndt7');
ndt7.test();

ndt7.test({
downloadMeasurement: function() {},
serverDiscovery: function() {},
serverChosen: function(server) {
console.log("Testing to:", {
machine: server.machine,
locations: server.location,
});
},
downloadComplete: function(data) {
console.log("Download test is complete:\n\tInstantaneous server bandwidth: ", data.LastServerMeasurement.BBRInfo.BW * 8 / 1000000, "\n\tMean client bandwidth: ", data.LastClientMeasurement.MeanClientMbps)
}
}).then(code => {
process.exit(code);
})
48 changes: 36 additions & 12 deletions js/ndt7.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* jshint esversion: 6, asi: true */
// ndt7 is a simple ndt7 client API.

// Install workalikes for APIs that are defined in browsers but not in node.js
if ('undefined' === typeof fetch) {
global.fetch = require('node-fetch');
}
if ('undefined' === typeof Worker) {
global.Worker = require('workerjs');
}

const ndt7 = (function() {

// cb creates a default-empty callback function, allowing library users to only need to specify callback functions for the events they care about.
Expand Down Expand Up @@ -49,34 +57,50 @@ const ndt7 = (function() {

// Starts the asynchronous process of server discovery, allowing other stuff to proceed in the background.
const urlPromise = discoverServerURLs(callbacks, config);
var clientMeasurement, serverMeasurement;


// Set up the webworker to run.
const downloadWorker = new Worker('./ndt7-download-worker.js');
downloadWorker.terminated = false;
const downloadWorkerPromise = new Promise(resolve => { downloadWorker.resolve = resolve; });
setTimeout(_ => {downloadWorker.terminate(); downloadWorker.resolve()}, 20000) // 20 seconds
downloadWorker.onmessage = function (ev) {
if (ev.data == null || ev.data.MsgType == 'error') {
downloadWorker.terminated = true;
downloadWorker.terminate();
const errMsg = (ev.data == null) ? 'There was a download error' : ev.data.Error;
downloadWorker.resolve();
const errMsg = (ev.data == null) ? 'There was a download error' : ev.data.Error;
callbacks.error(errMsg);
} else if (ev.data.MsgType == 'measurement') {
callbacks.downloadMeasurement({
Source: ev.data.Source,
Data: ev.data.Data
});
if (ev.data.Source == 'server') {
serverMeasurement = JSON.parse(ev.data.ServerMessage);
callbacks.downloadMeasurement({
Source: ev.data.Source,
Data: serverMeasurement,
});
} else {
clientMeasurement = ev.data.ClientData;
callbacks.downloadMeasurement({
Source: ev.data.Source,
Data: ev.data.ClientData,
});
}
} else if (ev.data.MsgType == 'complete') {
downloadWorker.terminate()
downloadWorker.resolve()
callbacks.downloadComplete({
Data: ev.data.Data
LastClientMeasurement: clientMeasurement,
LastServerMeasurement: serverMeasurement,
});
};
};

// TODO: Make sure the worker times out in 13 seconds no matter what.

const urls = await urlPromise;
downloadWorker.postMessage(urls);

// TODO: await the termination of the downloadWorker.
await downloadWorkerPromise;
// Liveness guarantee - once the promise is resolved, .terminate() has
// been called.
return 0;
}
/*,
// run runs the specified test with the specified base URL and calls
Expand Down Expand Up @@ -113,4 +137,4 @@ const ndt7 = (function() {
}
}());

module.exports = ndt7;
module.exports = ndt7;

0 comments on commit 7c6db29

Please sign in to comment.