MCPcopy Index your code
hub / github.com/nodejs/node / RunWorkerPingTest

Function RunWorkerPingTest

deps/v8/test/mjsunit/worker-ping-test.js:33–138  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

31}
32
33function 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 }

Calls 6

postMessageMethod · 0.95
terminateMethod · 0.95
nowMethod · 0.80
printFunction · 0.50
toStringMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected