-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·135 lines (103 loc) · 3.62 KB
/
main.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
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
$(function() {
var module; // Current Network.js' module
/*
* Tooltips
*/
$('.btn-group').tooltip();
/*
* UI
*/
var UI = {
$btnStart: $('[data-measure]'),
$btnAbort: $('[data-abort]'),
$output: $('output'),
start: function() {
rawModule = $(this).data('measure');
module = rawModule.charAt(0).toUpperCase() + rawModule.slice(1);
UI.$btnStart.prop('disabled', true);
UI.$btnAbort.prop('disabled', false);
net[rawModule].start();
// The latency module doesn't have a start event, we must trigger it manually.
if (rawModule == 'latency') {
net[rawModule].trigger('start');
}
},
restart: function(size) {
UI.notice(UI.delimiter(
'The minimum delay of ' + UI.value(8, 'seconds') + ' has not been reached'
));
UI.notice(UI.delimiter(
'Restarting measures with '
+ UI.value(size / 1024 / 1024, 'MB')
+ ' of data...'
));
},
stop: function() {
UI.notice(UI.delimiter('Finished measures'));
UI.$btnStart.prop('disabled', false);
UI.$btnAbort.prop('disabled', true);
},
abort: function() {
net.upload.abort();
net.download.abort();
},
notice: function(text, newSection) {
var $o = UI.$output,
stickToBottom = ($o.scrollTop() + $o.outerHeight()) == $o.prop('scrollHeight');
$o.append('<br>');
newSection && $o.append('<br>');
$o.append('<span class="yellow">[' + module + ']</span> ' + text);
if (stickToBottom) {
$o.scrollTop($o.prop('scrollHeight'));
}
},
value: function(value, unit) {
if (value != null) {
return '<span class="blue">' + value.toFixed(3) + ' ' + unit + '</span>';
} else {
return '<span class="blue">null</span>';
}
},
delimiter: function(text) {
return '<span class="green">' + text + '</span>';
}
};
/*
* Network.js configuration
*/
var net = new Network();
function start(size) {
UI.notice(UI.delimiter(
'Starting ' + rawModule + ' measures'
+ (rawModule != 'latency' ? (' with ' + UI.value(size / 1024 / 1024, 'MB') + ' of data') : '')
+ '...'
), true);
}
function progress(avg, instant) {
var output = 'Instant speed: ' + UI.value(instant / 1024 / 1024, 'MBps');
output += ' // Average speed: ' + UI.value(avg / 1024 / 1024, 'MBps');
UI.notice(output);
}
function end(avg) {
UI.notice('Final average speed: ' + UI.value(avg / 1024 / 1024, 'MBps'));
UI.stop();
}
net.upload.on('start', start).on('progress', progress).on('restart', UI.restart).on('end', end);
net.download.on('start', start).on('progress', progress).on('restart', UI.restart).on('end', end);
net.latency
.on('start', start)
.on('end', function(avg, all) {
all = all.map(function(latency) {
return UI.value(latency, 'ms');
});
all = '[ ' + all.join(' , ') + ' ]';
UI.notice('Instant latencies: ' + all);
UI.notice('Average latency: ' + UI.value(avg, 'ms'));
UI.stop();
});
/*
* Bindings
*/
UI.$btnStart.on('click', UI.start);
UI.$btnAbort.on('click', UI.abort);
});