-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_bench.ts
53 lines (47 loc) · 1.17 KB
/
test_bench.ts
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
import { assertEquals } from "@std/assert";
import { readableStreamFromWorker, writableStreamFromWorker } from "./mod.ts";
const count = 1000;
const sizes = [
64,
128,
256,
512,
1024,
];
const worker = new Worker(
new URL("./test_echo_server.ts", import.meta.url).href,
{ type: "module" },
);
for (const size of sizes) {
Deno.bench(
`Streams API (${size.toString().padStart(2)} Bytes x ${count})`,
{
group: size.toString(),
baseline: true,
},
async () => {
const rstream = readableStreamFromWorker(worker);
const wstream = writableStreamFromWorker(worker);
const producer = async () => {
const writer = wstream.getWriter();
for (let i = 0; i < count; i++) {
const data = new Uint8Array(size);
await writer.write(data);
}
writer.close();
};
const consumer = async () => {
let total = 0;
for await (const chunk of rstream) {
total += chunk.length;
}
return total;
};
const [_, total] = await Promise.all([
producer(),
consumer(),
]);
assertEquals(total, size * count);
},
);
}