-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerTesting.js
More file actions
112 lines (99 loc) · 3.83 KB
/
Copy pathworkerTesting.js
File metadata and controls
112 lines (99 loc) · 3.83 KB
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
var worker = null,
startTime = 0,
supported = false,
enable_transferable = document.getElementById('enable-transferable'),
size;
function log(str) {
var elem = document.getElementById('result');
var log = function (s) {
// var data = ''.concat(time(), ' ', s, '\n');
elem.innerHTML = ''.concat(time(), ' ', s, '\n') + elem.innerHTML;
};
log(str);
}
function init() {
size = document.getElementById('size').value;
worker = new Worker('transferable.js');
worker.onmessage = function (e) {
console.timeEnd('actual postMessage round trip was');
// capture elapsed time since the original postMessage();
if (!e.data.type) {
var elapsed = seconds(startTime);
}
var data = e.data;
if (data.type && data.type == 'debug') {
log(data.msg);
} else {
var rate = Math.round(toMB(data.byteLength) / elapsed);
log(source() + 'postMessage roundtrip took: ' + (elapsed * 1000) + ' ms');
log(source() + 'postMessage roundtrip rate: ' + rate + ' MB/s');
}
};
// To feature detect: send a small ArrayBuffer. If transferable objects are
// supported, the ArrayBuffer will be neutered (cleared out) after sent.
var ab = new ArrayBuffer(1);
if (USE_TRANSFERABLE && enable_transferable.checked) {
try {
worker.postMessage(ab, [ab]);
if (ab.byteLength) {
alert('Transferables are not supported in your browser!');
log(source() + 'USING STRUCTURED CLONE (copy) :(');
} else {
log(source() + 'USING TRANSFERABLE OBJECTS :)');
supported = true;
}
} catch (e) {
alert('Transferables are not supported in your browser!');
}
} else {
worker.postMessage(ab); // send anyway to init worker.
log(source() + 'USING STRUCTURED CLONE (copy) :(');
supported = false;
}
log(source() + 'READY!');
}
function test() {
setupArray(); // Need to do this on every run for the repeated runs with transferable arrays. They're cleared out after they're transferred.
startTime = new Date();
console.time('actual postMessage round trip was');
if (USE_TRANSFERABLE && supported) {
// Note: clears the uInt8View and it's underlying ArrayBuffer, transfering it
// out of this view, to the worker.
// Passing multiple transferables:
// worker.postMessage({view1: int8View, buffer2: anotherBuffer}, [int8View.buffer, anotherBuffer]);
// window.postMessage(arrayBuffer, targetOrigin, [arrayBuffer]);
worker.postMessage(uInt8View.buffer, [uInt8View.buffer]);
} else {
worker.postMessage(uInt8View.buffer);
}
}
function seconds(since) {
return (new Date() - since) / 1000.0;
}
var workerCreationLog = [];
var iterations = 0;
function workerCreationAvg() {
var workerCreationAvg = 0;
for (i = 0; i < workerCreationLog.length; i++) {
workerCreationAvg += workerCreationLog[i];
}
workerCreationAvg = workerCreationAvg / iterations;
log('Worker Creation Average time of ' + iterations + ' worker: ' + workerCreationAvg + 'ms')
}
function workerCreation() {
var blob = new Blob(['onmessage = function() {postMessage(0);};']);
var timeBefore = window.performance.now();
var worker = new Worker(window.URL.createObjectURL(blob));
worker.postMessage(0);
worker.onmessage = function () {
var timeAfter = window.performance.now();
worker.terminate();
var creationDuration = timeAfter - timeBefore;
workerCreationLog.push(creationDuration);
log('Creation of the worker took ' + creationDuration + 'ms');
iterations++;
}
}
// window.addEventListener('load', function (e) {
// init();
// }, false);