forked from microsoft/napajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute-overhead.ts
79 lines (67 loc) · 2.52 KB
/
execute-overhead.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as napa from '../lib/index';
import * as mdTable from 'markdown-table';
import { formatTimeDiff } from './bench-utils';
function batchExecuteOnNamedFunction(
zone: napa.zone.Zone,
repeat: number,
args: any[]): Promise<void> {
return new Promise<void>((resolve, reject) => {
let finished = 0;
let start = process.hrtime();
for (let i = 0; i < repeat; ++i) {
zone.execute("", "test", args).then(() => {
++finished;
if (finished === repeat) {
resolve();
}
});
}
});
}
function batchExecuteOnAnonymousFunction(
zone: napa.zone.Zone,
repeat: number,
args: any[]): Promise<void> {
return new Promise<void>((resolve, reject) => {
let finished = 0;
let start = process.hrtime();
for (let i = 0; i < repeat; ++i) {
zone.execute(() => {}, args).then(() => {
++finished;
if (finished === repeat) {
resolve();
}
});
}
});
}
export async function bench(zone: napa.zone.Zone): Promise<void> {
console.log("Benchmarking execute overhead...");
// Prepare a empty function.
await zone.broadcast("function test() {}");
const ARGS = [1, "hello", {a: 1, b: true}];
// Warm-up.
const WARMUP_REPEAT: number = 20;
console.log("## Execute overhead during warmup\n")
let warmupTable = [];
warmupTable.push(["call #", "time (ms)"]);
for (let i = 0; i < WARMUP_REPEAT; ++i) {
let start = process.hrtime();
await zone.execute("", "test", ARGS);
warmupTable.push([i.toString(), formatTimeDiff(process.hrtime(start))]);
}
console.log(mdTable(warmupTable));
// execute after warm-up
const REPEAT: number = 10000;
console.log("## `zone.execute` overhead (use function name)\n");
let start = process.hrtime();
await batchExecuteOnNamedFunction(zone, REPEAT, ARGS);
console.log(`Elapse of running empty function by name for ${REPEAT} times: ${formatTimeDiff(process.hrtime(start), true)}\n`);
console.log("## `zone.execute` overhead (use anonymous function)\n");
start = process.hrtime();
await batchExecuteOnAnonymousFunction(zone, REPEAT, ARGS);
console.log(`Elapse of running empty anonymous function for ${REPEAT} times: ${formatTimeDiff(process.hrtime(start), true)}\n`);
return;
}