Skip to content

Commit

Permalink
Merge pull request #18 from joshLong145/0.7.0
Browse files Browse the repository at this point in the history
0.7.0
  • Loading branch information
joshLong145 committed Mar 22, 2024
2 parents 1951a75 + bbeb614 commit f41193f
Show file tree
Hide file tree
Showing 14 changed files with 534 additions and 141 deletions.
125 changes: 125 additions & 0 deletions benchmark/wasm_instance_start_bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//@ts-nocheck

import { InstanceWrapper, WorkerDefinition } from "../src/mod.ts";
import { existsSync } from "https://deno.land/std/fs/mod.ts";
import * as path from "https://deno.land/std@0.188.0/path/mod.ts";

class TestExample extends WorkerDefinition {
public constructor(modulePath: string) {
Expand Down Expand Up @@ -80,3 +82,126 @@ Deno.bench("Wasm Worker Start Rust Module loading", async (_b) => {
example.terminateWorker();
});
});

Deno.bench("Wasm Worker Start Code Gen Bootstrapping Rust", async (_b) => {
if (!existsSync("./public")) {
Deno.mkdirSync("./public");
}

if (!existsSync("./public/bench")) {
Deno.mkdirSync("./public/bench");
}

const example: WorkerDefinition = new TestExample();

const wrapper: InstanceWrapper<TestExample> = new InstanceWrapper<
Example
>(
example,
{
outputPath: "./public/bench",
namespace: "asd",
addons: [
"./lib/wasm_test.js",
],
modulePath: "./examples/wasm/rust/wasm_test_bg.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
moduleLoader: (path: string) => {
const fd = Deno.openSync(path);
const mod = Deno.readAllSync(fd);
fd.close();
return mod;
},
},
);
wrapper.create({
writeFileSync: Deno.writeFileSync,
});
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
await import(__dirname + "/../public/bench/bridge.js");
self["worker"].terminate();
});

Deno.bench("Wasm Worker Start Code Gen Bootstrapping Tiny Go", async (_b) => {
if (!existsSync("./public")) {
Deno.mkdirSync("./public");
}

if (!existsSync("./public/bench")) {
Deno.mkdirSync("./public/bench");
}

const example: WorkerDefinition = new TestExample();

const wrapper: InstanceWrapper<TestExample> = new InstanceWrapper<
Example
>(
example,
{
outputPath: "./public/bench",
namespace: "asd",
addons: [
"./lib/wasm_exec_tiny.js",
],
modulePath: "./examples/wasm/tiny-go/primes-2.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
moduleLoader: (path: string) => {
const fd = Deno.openSync(path);
const mod = Deno.readAllSync(fd);
fd.close();
return mod;
},
},
);
wrapper.create({
writeFileSync: Deno.writeFileSync,
});
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
await import(__dirname + "/../public/bench/bridge.js");
self["worker"].terminate();
});

Deno.bench("Wasm Worker Start Code Gen Bootstrapping Go", async (_b) => {
if (!existsSync("./public")) {
Deno.mkdirSync("./public");
}

if (!existsSync("./public/bench")) {
Deno.mkdirSync("./public/bench");
}

const example: WorkerDefinition = new TestExample();

const wrapper: InstanceWrapper<TestExample> = new InstanceWrapper<
Example
>(
example,
{
outputPath: "./public/bench",
namespace: "asd",
addons: [
"./lib/wasm_exec.js",
],
modulePath: "./examples/wasm/tiny-go/primes-2.wasm",
addonLoader: (path: string) => {
return Deno.readTextFileSync(path);
},
moduleLoader: (path: string) => {
const fd = Deno.openSync(path);
const mod = Deno.readAllSync(fd);
fd.close();
return mod;
},
},
);
wrapper.create({
writeFileSync: Deno.writeFileSync,
});
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
await import(__dirname + "/../public/bench/bridge.js");
self["worker"].terminate();
});
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"./lib/*.js"
],
"name": "@joshlong145/span",
"version": "0.6.0",
"version": "0.7.0",
"exports": "./src/mod.ts"
}
56 changes: 36 additions & 20 deletions examples/js/example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { assertExists } from "https://deno.land/std@0.210.0/assert/assert_exists.ts";
import { InstanceConfiguration } from "../../src/types.ts";
import { InstanceWrapper, WorkerDefinition } from "./../../src/mod.ts";
import { assertIsError } from "https://deno.land/std@0.210.0/assert/assert_is_error.ts";

class Example extends WorkerDefinition {
public constructor() {
Expand All @@ -10,7 +12,6 @@ class Example extends WorkerDefinition {
buffer: SharedArrayBuffer,
args: Record<string, any>,
): SharedArrayBuffer => {
console.log("param name value: ", args.name);
const arr = new Uint32Array(buffer);
arr[0] += 1;

Expand All @@ -28,7 +29,6 @@ class Example extends WorkerDefinition {

for (i = 2; i <= args.count; i++) {
arr[i] = arr[i - 2] + arr[i - 1];
console.log(arr[i]);
}
return buffer;
};
Expand Down Expand Up @@ -61,6 +61,21 @@ class Example extends WorkerDefinition {

return buffer;
};

undefinedExecution = (
buffer: SharedArrayBuffer,
_args: Record<string, any>,
): Promise<SharedArrayBuffer> => {
let id = 0;
return new Promise<SharedArrayBuffer>((res, _rej) => {
id = setTimeout(() => {
res(buffer);
}, 10_000);
}).finally(() => {
clearTimeout(id);
return buffer;
});
};
}

const example: Example = new Example();
Expand All @@ -72,30 +87,31 @@ const wrapper: InstanceWrapper<Example> = new InstanceWrapper<Example>(

wrapper.start();

example.worker.onerror = (event: any) => {
console.log("an error occured ", event);
};

await example.execute("addOne", { name: "foo" }).then(
(buf: SharedArrayBuffer) => {
console.log("add one result: ", new Int32Array(buf));
},
);
await example.execute("addOne", { name: "foo" }).then(
(buf: SharedArrayBuffer) => {
console.log("add one result ", new Int32Array(buf)[0]);
assertExists(buf);
},
);

await example.execute("getKeyPair");

await example.execute("fib", { count: 46 }).then(
(buffer: SharedArrayBuffer) => {
console.log("fib result ", new Uint32Array(buffer));
console.log("last fib number", new Uint32Array(buffer)[46]);
},
);
await example.execute("getGpuAdapter");
await example.execute("fib", { count: 46 });
await example.execute("getGpuAdapter");

const workerPrms = example.execute("undefinedExecution");

// handle a rejection of the promise due to a timeout
workerPrms.catch((e) => {
assertIsError(e);
});

// timeout the above execution call in 1 second
workerPrms.timeout(1_000);

// you can also use await with a try catch to manage the timeout
try {
await workerPrms;
} catch (e) {
assertIsError(e);
}

example.terminateWorker();
5 changes: 3 additions & 2 deletions examples/wasm/rust/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Example extends WorkerDefinition {

test2 = (buffer: SharedArrayBuffer, _args: Record<string, any>) => {
let arr = new Int8Array(buffer);
arr[0] += 1;
//@ts-ignore
self.greet();
let val = self.getValue();
arr[0] = val;
return arr.buffer;
};
}
Expand Down Expand Up @@ -40,4 +40,5 @@ await wrapper.start();
await example.execute("test2").then((buffer: SharedArrayBuffer) => {
console.log(new Uint8Array(buffer)[0]);
});

example.terminateWorker();
21 changes: 3 additions & 18 deletions examples/wasm/tiny-go/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,18 @@ const wrapper: InstanceWrapper<Example> = new InstanceWrapper<Example>(

wrapper.start();

await example.execute("test1").then((buf: SharedArrayBuffer) => {
console.log("hello", new Int32Array(buf));
});
await example.execute("test2").then((buf: SharedArrayBuffer) => {
let arr = new Int32Array(buf);
console.log("hello1", arr[0]);
});

await example.execute("test1");
await example.execute("test2").then((buf: SharedArrayBuffer) => {
console.log("hello2", new Int32Array(buf)[0]);
});
await example.execute("test2").then((buf: SharedArrayBuffer) => {
console.log("hello3", new Int32Array(buf)[0]);
console.log("first value in buffer ", new Int32Array(buf)[0]);
});

example.terminateWorker();

await wrapper.restart();
console.log("restarting web worker");

await example.execute("test1").then((buf: SharedArrayBuffer) => {
console.log("hello", new Int32Array(buf));
});

await example.execute("test2").then((buf: SharedArrayBuffer) => {
let arr = new Int32Array(buf);
console.log("hello1", new Int32Array(buf)[0]);
console.log("first value in buffer ", new Int32Array(buf)[0]);
});

example.terminateWorker();
Loading

0 comments on commit f41193f

Please sign in to comment.