(config)
| 31 | } |
| 32 | |
| 33 | function RunWorkerPingTest(config) { |
| 34 | let workers = []; |
| 35 | let beforeSend = (typeof config.BeforeSend == "function") ? |
| 36 | config.BeforeSend : |
| 37 | function BeforeSend(msg) { }; |
| 38 | let beforeReceive = (typeof config.BeforeReceive == "function") ? |
| 39 | config.BeforeReceive : |
| 40 | function BeforeReceive(msg) { }; |
| 41 | |
| 42 | // Each worker has a circular buffer of size {config.numThings}, recording |
| 43 | // received things into the buffer and responding with a previous thing. |
| 44 | // Every {config.allocInterval}, a worker creates a new thing by |
| 45 | // {config.AllocThing}. |
| 46 | |
| 47 | function workerCode(config, AllocThing, BeforeReceive) { |
| 48 | eval(AllocThing); |
| 49 | eval(BeforeReceive); |
| 50 | const kNumThings = config.numThings; |
| 51 | const kAllocInterval = config.allocInterval; |
| 52 | let index = 0; |
| 53 | let total = 0; |
| 54 | let id = 0; |
| 55 | let things = new Array(kNumThings); |
| 56 | for (let i = 0; i < kNumThings; i++) { |
| 57 | things[i] = TryAllocThing(); |
| 58 | } |
| 59 | |
| 60 | function TryAllocThing() { |
| 61 | try { |
| 62 | let thing = AllocThing(id++); |
| 63 | if (config.traceAlloc) { |
| 64 | print("alloc success"); |
| 65 | } |
| 66 | return thing; |
| 67 | } catch(e) { |
| 68 | if (config.abortOnFail) { |
| 69 | postMessage({error: e.toString()}); throw e; |
| 70 | } |
| 71 | if (config.traceAlloc) { |
| 72 | print("alloc fail: " + e); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | onmessage = function({data:msg}) { |
| 78 | BeforeReceive(msg); |
| 79 | if (msg.thing !== undefined) { |
| 80 | let reply = things[index]; |
| 81 | if ((total % kAllocInterval) == 0) { |
| 82 | reply = TryAllocThing(); |
| 83 | } |
| 84 | things[index] = msg.thing; |
| 85 | postMessage({thing : reply}); |
| 86 | index = (index + 1) % kNumThings; |
| 87 | total++; |
| 88 | } |
| 89 | } |
| 90 | } |
no test coverage detected