-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws-client.js
64 lines (56 loc) · 1.61 KB
/
ws-client.js
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
/**
* Module dependencies.
*/
var crypto = require('crypto');
var WebSocket = require('ws');
/**
* ARGV Set
*/
var Size = parseInt(process.argv[2].toString().trim()); // the first argument
var MaxIter = process.argv[3] ? parseInt(process.argv[3].toString().trim()) : 20; // the second argument
var Protocol = process.argv[4] ? 'wss': 'ws' // the third argument
/**
* WevSocketClient connect
*/
//var ServerName = 'server.info';
var ServerName = 'localhost';
var PortN = 8082
var ws = new WebSocket(Protocol + '://' + ServerName + ':' + PortN);
ws.on('open', function() {
var start = undefined;
var totalTime = 0;
var i = MaxIter;
/**
* Download Event
*/
ws.on('message', function(message) {
var time = Date.now() - start;
//*console.log(message);
console.log('Data size: ' + Size + ', roudtrip time: ' + time + ' ms');
totalTime += time;
if (--i) {
uploadStart(Size);
} else {
var ave = totalTime / MaxIter;
console.log('Average roundtrip time: ' + ave + ' ms');
/**
* Transfer ratio: Size(Bytes) / (ave(ms) / 2(roundtrip)) * 1000 = (Bytes per Second)
* (Bytes per Second) * 8 / 1024 / 1024= (Mbps)
*/
console.log('Transfer ratio: ' + (Size / ave * 2000).toFixed(1) + '[Bytes per Second] = ' + (Size / ave * 0.0152587890625).toFixed(1) + '[Mbps]');
process.exit(0);
}
});
/**
* Upload File
*/
function uploadStart(size) {
crypto.randomBytes(size, function(ex, buf) {
if (ex) throw ex;
//*console.log(buf);
start = Date.now();
ws.send(buf, {binary: true});
});
}
uploadStart(Size);
});