-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.js
87 lines (74 loc) · 2.35 KB
/
master.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
const TEST_NUMBER = 10000;
let count = 0;
let startTime;
function beginTest() {
if (USE_LONG_LIVED_CONNECTION) {
let port = chrome.runtime.connect({
name: 'master'
});
port.onMessage.addListener(msg => {
// console.log(msg);
if (msg.status === 'ready') {
setupArray('master');
startTime = new Date();
postDataToSlave(port);
} else if (msg.status === 'received' && count < TEST_NUMBER) {
postDataToSlave(port);
} else if (count >= TEST_NUMBER) {
// End testing
console.log('Testing done');
getStatistics();
}
});
} else {
let receiveCallback = function(response) {
if (response.status === 'received') {
count++;
if (count >= TEST_NUMBER) {
// End testing
console.log('Testing done');
getStatistics();
return;
}
postDataToSlaveForOneTime(receiveCallback);
}
};
chrome.runtime.onMessage.addListener(
function(msg, sender, sendResponse) {
if (!sender.url.endsWith('/slave.html')) {
return;
}
if (msg.status === 'ready') {
setupArray('master');
startTime = new Date();
postDataToSlaveForOneTime(receiveCallback);
}
});
}
}
function postDataToSlaveForOneTime(callback) {
chrome.runtime.sendMessage({
name: 'test',
data: uInt8View.buffer
}, function(response) {
callback(response);
});
}
function postDataToSlave(port) {
if (USE_TRANSFERABLE) {
port.postMessage(uInt8View.buffer, [uInt8View.buffer]);
} else {
// port.postMessage(uInt8View.buffer);
port.postMessage({
name: 'test',
data: uInt8View.buffer
});
}
count++;
}
function getStatistics() {
let elapsed = seconds(startTime);
let rate = Math.round(toMB(SIZE * TEST_NUMBER) / elapsed);
console.log('master postMessage roundtrip took: ' + (elapsed * 1000) + ' ms');
console.log('master postMessage roundtrip rate: ' + rate + ' MB/s');
}