summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/worker-ping-test.js
blob: 046e217edb6c61592dad1d6cba747386e283dc3c (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A test utility for pinging objects back and forth among a pool of workers.
// Use by calling {RunWorkerPingTest} with a {config} object.
{
// Reference config object for demonstrating the interface.
let config = {
  numThings: 4,       // size of circular buffer
  numWorkers: 4,      // number of workers
  numMessages: 100,   // number of messages sent to each worker
  allocInterval: 11,  // interval for allocating new things per worker
  traceScript: false, // print the script
  traceAlloc: false,  // print each allocation attempt
  traceIteration: 10, // print diagnostics every so many iterations
  abortOnFail: false, // kill worker if allocation fails

  // Note that because the functions are appended to a worker script
  // *as source*, they need to be named properly.

  // The function that allocates things. Required.
  AllocThing: function AllocThing(id) {
    return new Array(2);
  },
  // Before message send behavior. Optional.
  BeforeSend: function BeforeSend(msg) { },
  // Before message reception behavior. Optional.
  BeforeReceive: function BeforeReceive(msg) { },
}
}

function RunWorkerPingTest(config) {
  let workers = [];
  let beforeSend = (typeof config.BeforeSend == "function") ?
      config.BeforeSend :
      function BeforeSend(msg) { };
  let beforeReceive = (typeof config.BeforeReceive == "function") ?
      config.BeforeReceive :
      function BeforeReceive(msg) { };

  // Each worker has a circular buffer of size {config.numThings}, recording
  // received things into the buffer and responding with a previous thing.
  // Every {config.allocInterval}, a worker creates a new thing by
  // {config.AllocThing}.

  let script =
`const kNumThings = ${config.numThings};
  const kAllocInterval = ${config.allocInterval};
  let index = 0;
  let total = 0;
  let id = 0;
  let things = new Array(kNumThings);
  for (let i = 0; i < kNumThings; i++) {
    things[i] = TryAllocThing();
  }

  function TryAllocThing() {
     try {
       let thing = AllocThing(id++);
       ${config.traceAlloc ? "print(\"alloc success\");" : ""}
       return thing;
     } catch(e) {
       ${config.abortOnFail ? "postMessage({error: e.toString()}); throw e;" : "" }
       ${config.traceAlloc ? "print(\"alloc fail: \" + e);" : ""}
     }
  }

  onmessage = function(msg) {
    BeforeReceive(msg);
    if (msg.thing !== undefined) {
      let reply = things[index];
      if ((total % kAllocInterval) == 0) {
        reply = TryAllocThing();
      }
      things[index] = msg.thing;
      postMessage({thing : reply});
      index = (index + 1) % kNumThings;
      total++;
    }
  }
  ${config.AllocThing.toString()}
  ${beforeReceive.toString()}
  `;

  if (config.traceScript) {
    print("========== Worker script ==========");
    print(script);
    print("===================================");
  }

  for (let i = 0; i < config.numWorkers; i++) {
    let worker = new Worker(script, {type : 'string'});
    workers.push(worker);
  }

  let time = performance.now();

  // The main thread posts {config.numMessages} messages to {config.numWorkers}
  // workers, with each message containing a "thing" created by {config.AllocThing}.
  let thing = config.AllocThing(-1);
  for (let i = 0; i < config.numMessages; i++) {
    if ((i % config.traceIteration) == 0) {
      let now = performance.now();
      print(`iteration ${i}, Δ = ${(now - time).toFixed(3)} ms`);
      time = now;
    }

    for (let worker of workers) {
      let msg = {thing: thing};
      beforeSend(msg);
      worker.postMessage(msg);
      msg = worker.getMessage();
      if (msg.thing) {
        thing = msg.thing;
      } else if (msg.error) {
        worker.terminate();
        throw msg.error;
      }
    }
  }
  for (let worker of workers) {
    worker.terminate();
  }
}