v8/test/mjsunit/worker-ping-test.js
Ben L. Titzer 4a416dbbe1 [mjsunit] Add more tests for shared array buffers / memories
This CL adds more stress-tests for both shared array buffers and
WebAssembly memories. Because of an existing memory leak that will
be fixed in upcoming CLs, some new tests are disabled.

R=mstarzinger@chromium.org
BUG=v8:9380

Change-Id: I2662e3d0a764a032a0c267b2d99e3ccd1a4951d0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1697252
Commit-Queue: Ben Titzer <titzer@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62770}
2019-07-17 14:08:38 +00:00

126 lines
3.9 KiB
JavaScript

// 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();
}
}