1 | sS hjs_coreEstimator() { |
2 | ret hjavascript([[ |
3 | // create worker concurrency estimation code as blob |
4 | var blobUrl = URL.createObjectURL(new Blob(['(', |
5 | function() { |
6 | self.addEventListener('message', function(e) { |
7 | // run worker for 4 ms |
8 | var st = Date.now(); |
9 | var et = st + 4; |
10 | while(Date.now() < et); |
11 | self.postMessage({st: st, et: et}); |
12 | }); |
13 | }.toString(), |
14 | ')()'], {type: 'application/javascript'})); |
15 | |
16 | document.getElementById('click').addEventListener('click', function() { |
17 | document.getElementById('max').innerHTML = 'Testing concurrency...'; |
18 | |
19 | // take 10 samples using 16 workers |
20 | sample([], 10, 16, function(err, cores) { |
21 | document.getElementById('max').innerHTML = 'Max concurrency: ~' + cores + ' cores'; |
22 | }); |
23 | }); |
24 | |
25 | function sample(max, samples, numWorkers, callback) { |
26 | if(samples === 0) { |
27 | // get overlap average |
28 | var avg = Math.floor(max.reduce(function(avg, x) { |
29 | return avg + x; |
30 | }, 0) / max.length); |
31 | avg = Math.max(1, avg); |
32 | return callback(null, avg); |
33 | } |
34 | map(numWorkers, function(err, results) { |
35 | max.push(reduce(numWorkers, results)); |
36 | sample(max, samples - 1, numWorkers, callback); |
37 | }); |
38 | } |
39 | |
40 | function map(numWorkers, callback) { |
41 | var workers = []; |
42 | var results = []; |
43 | for(var i = 0; i < numWorkers; ++i) { |
44 | var worker = new Worker(blobUrl); |
45 | worker.addEventListener('message', function(e) { |
46 | results.push(e.data); |
47 | if(results.length === numWorkers) { |
48 | for(var i = 0; i < numWorkers; ++i) { |
49 | workers[i].terminate(); |
50 | } |
51 | callback(null, results); |
52 | } |
53 | }); |
54 | workers.push(worker); |
55 | } |
56 | for(var i = 0; i < numWorkers; ++i) { |
57 | workers[i].postMessage(i); |
58 | } |
59 | } |
60 | |
61 | function reduce(numWorkers, results) { |
62 | // find overlapping time windows |
63 | var overlaps = []; |
64 | for(var n = 0; n < numWorkers; ++n) { |
65 | var r1 = results[n]; |
66 | var overlap = overlaps[n] = []; |
67 | for(var i = 0; i < numWorkers; ++i) { |
68 | if(n === i) { |
69 | continue; |
70 | } |
71 | var r2 = results[i]; |
72 | if((r1.st > r2.st && r1.st < r2.et) || |
73 | (r2.st > r1.st && r2.st < r1.et)) { |
74 | overlap.push(i); |
75 | } |
76 | } |
77 | } |
78 | // get maximum overlaps ... don't include overlapping worker itself |
79 | // as the main JS process was also being scheduled during the work and |
80 | // would have to be subtracted from the estimate anyway |
81 | return overlaps.reduce(function(max, overlap) { |
82 | return Math.max(max, overlap.length); |
83 | }, 0); |
84 | } |
85 | ]]); |
86 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1018562 |
Snippet name: | hjs_coreEstimator |
Eternal ID of this version: | #1018562/1 |
Text MD5: | f2e5a8f3e6da5fb9a26474d8fef0efcc |
Author: | stefan |
Category: | javax / javascript |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-10-05 19:33:13 |
Source code size: | 2870 bytes / 86 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 300 / 341 |
Referenced in: | [show references] |