Skip to content

Commit

Permalink
bwtest - congestion control and stats using channel.bufferedAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
guymguym committed Apr 3, 2015
1 parent 6bbb583 commit bb21ac5
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions test/bwtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,35 @@ var SimplePeer = require('simple-peer');

function bwtest(peer1, peer2) {
var START_TIME = Date.now();
var BUFFERED_DELAY_MS = 10;
var NPACKETS = 10000;
var BUFFERED_DELAY_MS = 5;
var NPACKETS = 5000;
var PACKET_SIZE = 16 * 1024;
var CONGEST_HIGH_THRESHOLD = 1 * 1024 * 1024;
var CONGEST_LOW_THRESHOLD = 256 * 1024;
var buffer = new ArrayBuffer(PACKET_SIZE);
var n = 0;
var congested = 0;
var stats = {
count: 0,
bytes: 0,
congestCount: 0,
congestTime: 0
};

function info() {
var took = (Date.now() - START_TIME) / 1000;
var buffered = peer1._channel && peer1._channel.bufferedAmount;
return 'sent ' + n + ' received ' + stats.count +
(buffered ? ' buffered ' + buffered : '') +
' took ' + took.toFixed(3) + ' seconds' +
' bandwidth ' + (stats.bytes / took / 1024).toFixed(0) + ' KB/s';
var now = Date.now();
if (congested) {
stats.congestTime += Date.now() - congested;
congested = now;
}
var took = (now - START_TIME) / 1000;
var bufferedAmount = peer1._channel && peer1._channel.bufferedAmount;
return 'sent ' + n + ' received ' + stats.count + '. ' +
'congestion #' + stats.congestCount + ' ' +
(stats.congestTime / 1024).toFixed(3) + ' seconds. ' +
(bufferedAmount ? 'bufferedAmount ' + bufferedAmount + '. ' : '') +
'took ' + took.toFixed(3) + ' seconds. ' +
'bandwidth ' + (stats.bytes / took / 1024).toFixed(0) + ' KB/s. ';
}

peer2.on('data', function(data) {
Expand All @@ -49,6 +61,23 @@ function bwtest(peer1, peer2) {
}
});

function congestion() {
var bufferedAmount = peer1._channel && peer1._channel.bufferedAmount || 0;
if ((bufferedAmount > CONGEST_HIGH_THRESHOLD) ||
(congested && bufferedAmount > CONGEST_LOW_THRESHOLD)) {
if (!congested) {
congested = Date.now();
}
stats.congestCount += 1;
} else {
if (congested) {
stats.congestTime += Date.now() - congested;
}
congested = 0;
}
return congested;
}

function send() {
if (n >= NPACKETS) {
console.log('SEND DONE!', info());
Expand All @@ -57,8 +86,7 @@ function bwtest(peer1, peer2) {
if (n % 100 === 0) {
console.log('SENDING:', info());
}
if (peer1._channel && peer1._channel.bufferedAmount) {
console.log('WAITING:', info());
if (congestion()) {
setTimeout(send, BUFFERED_DELAY_MS);
return;
}
Expand All @@ -81,7 +109,25 @@ function bwtest(peer1, peer2) {

function init(callback) {
var INITIATOR_CONFIG = {
initiator: true
initiator: true,
config: {
iceServers: [{
url: 'stun:23.21.150.121'
}],

/*
* data channels are ordered by default - using unordered channel improves
* performance but will likely require ordering in another app layer
*/
ordered: false,

/*
* data channels are reliable by default - using either maxRetransmits
* or maxPacketLifeTime will change to unreliable mode
*/
// maxRetransmits: 5,
// maxPacketLifeTime: 3000,
}
};
var peer1 = new SimplePeer(INITIATOR_CONFIG);
var peer2 = new SimplePeer();
Expand Down

0 comments on commit bb21ac5

Please sign in to comment.