Skip to content

Commit

Permalink
Merge pull request #9 from lambdalisue/add-benchmark-script
Browse files Browse the repository at this point in the history
Add benchmark script
  • Loading branch information
lambdalisue authored Jun 13, 2021
2 parents 2d81963 + 888db55 commit e208ef6
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,52 @@ async function main(): Promise<void> {
main().catch((e) => console.error(e));
```

## Benchmark

You can run benchmark of `WorkerReader` and `WorkerWriter` with the following
command:

```
$ deno run --no-check --unstable --allow-read --allow-net ./benchmark/benchmark.ts
===========================================================
Transfer: 1 MiB
N: 5 times
===========================================================
Relaxing 1 sec ...
Start benchmark
1 Reader: 136 [ms] Writer: 44 [ms]
2 Reader: 134 [ms] Writer: 42 [ms]
3 Reader: 131 [ms] Writer: 44 [ms]
4 Reader: 136 [ms] Writer: 45 [ms]
5 Reader: 125 [ms] Writer: 40 [ms]
===========================================================
Reader: Avg. 132.40 msec (63.358 Mbps)
Writer: Avg. 43.000 msec (195.08 Mbps)
===========================================================
```

Use `-n` to change the number of tries and `-size` to the size of the buffer (in
MB) like:

```
$ deno run --no-check --unstable --allow-read --allow-net ./benchmark/benchmark.ts -n 3 --size 8
===========================================================
Transfer: 8 MiB
N: 3 times
===========================================================
Relaxing 1 sec ...
Start benchmark
1 Reader: 1404 [ms] Writer: 393 [ms]
2 Reader: 1326 [ms] Writer: 351 [ms]
3 Reader: 1310 [ms] Writer: 366 [ms]
===========================================================
Reader: Avg. 1346.7 msec (49.833 Mbps)
Writer: Avg. 370.00 msec (181.38 Mbps)
===========================================================
```

See [Benchmark](./wiki/Benchmark) for various benchmarks.

## License

The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
Expand Down
99 changes: 99 additions & 0 deletions benchmark/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { parse } from "https://deno.land/std@0.98.0/flags/mod.ts";
import { assertEquals, delay, io } from "../deps_test.ts";
import { WorkerReader, WorkerWriter } from "../mod.ts";

async function timeout(d: number): Promise<never> {
return await delay(d).then(() => Promise.reject("Timeout"));
}

async function timeIt(fn: () => Promise<void>): Promise<number> {
const start = new Date();
await fn();
const end = new Date();
const elapsed = end.getTime() - start.getTime();
return elapsed;
}

async function benchmark(
buffer: Uint8Array,
reader: Deno.Reader,
writer: Deno.Writer,
): Promise<[number, number]> {
const size = buffer.length;

const consumer = async () => {
const r = await io.readAll(new io.LimitedReader(reader, size));
assertEquals(r.length, size);
};

const producer = async () => {
await io.writeAll(writer, buffer);
};

const [rt, wt] = await Promise.race([
Promise.all([
timeIt(consumer),
timeIt(producer),
]),
timeout(30000),
]);

return [rt, wt];
}

async function main(): Promise<void> {
const args = parse(Deno.args, {
string: ["n", "size"],
default: {
n: 5,
size: 1,
},
});
const worker = new Worker(
new URL("../test_worker.ts", import.meta.url).href,
{
type: "module",
deno: {
namespace: true,
},
},
);
try {
const size = parseFloat(args.size) * 2 ** 20;
const n = parseInt(args.n, 10);
const buffer = new Uint8Array(size);
const reader = new WorkerReader(worker);
const writer = new WorkerWriter(worker);
const rts = [];
const wts = [];
console.log("===========================================================");
console.log(`Transfer: ${size / 2 ** 20} MiB`);
console.log(`N: ${n} times`);
console.log("===========================================================");
console.log("Relaxing 1 sec ...");
await delay(1000);
console.log("Start benchmark");
for (const i of Array(n).keys()) {
const [rt, wt] = await benchmark(buffer, reader, writer);
rts.push(rt);
wts.push(wt);
console.log(i + 1, `Reader: ${rt} [ms]`, `Writer: ${wt} [ms]`);
}
console.log("===========================================================");
const rt = rts.reduce((a, v) => a + v, 0) / n;
const wt = wts.reduce((a, v) => a + v, 0) / n;
const rbps = size * 8 * 1000 / rt;
const wbps = size * 8 * 1000 / wt;
const srt = rt.toPrecision(5).padStart(5);
const swt = wt.toPrecision(5).padStart(5);
const srbps = (rbps / 10 ** 6).toPrecision(5).padStart(5);
const swbps = (wbps / 10 ** 6).toPrecision(5).padStart(5);
console.log(`Reader: Avg. ${srt} msec (${srbps} Mbps)`);
console.log(`Writer: Avg. ${swt} msec (${swbps} Mbps)`);
console.log("===========================================================");
} finally {
worker.terminate();
}
}

main().catch((e) => console.error(e)).then(() => Deno.exit(0));

0 comments on commit e208ef6

Please sign in to comment.