Here is a pretty quick concurrency appraiser that I hacked together ... it hasn't passed many tests yet:
http://jsfiddle.net/Ma4YT/2/
Here's the code the workers are working on (since I have a jsfiddle link, a sample is needed):
// create worker concurrency estimation code as blob var blobUrl = URL.createObjectURL(new Blob(['(', function() { self.addEventListener('message', function(e) { // run worker for 4 ms var st = Date.now(); var et = st + 4; while(Date.now() < et); self.postMessage({st: st, et: et}); }); }.toString(), ')()'], {type: 'application/javascript'}));
The evaluator has a large number of workers executed in a short period of time (4 ms) and reports on the time in which they were performed (unfortunately, the performance.now () function is not available in Web Workers to more accurately determine the time). Then the main thread checks that the maximum number of workers who worked at the same time. This test is repeated several times in order to get a decent sample to get a score with.
Thus, the main idea is that, given the small amount of work, workers should plan to launch at the same time if there are enough cores to support this behavior. This is obviously just an estimate, but so far it has been accurate enough for the few machines I tested, which is good enough for my use. The number of samples can be increased to get a more accurate approximation; I just use 10 because it is fast and I donβt want to spend time evaluating, not just doing the work.
dlongley Feb 17 '14 at 0:30 2014-02-17 00:30
source share