Skip to content

Commit bf27cd0

Browse files
author
Scott Hardy
authored
Merge pull request #114 from GameBridgeAI/benchmarks
Benchmarks
2 parents 72ed1bd + 21a7e43 commit bf27cd0

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to
1111
### Added
1212

1313
- missing copyright statement
14+
- basic benchmark tests
1415

1516
## [v1.2.0] - 2021-01-22
1617

benchmarks/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Benchmarks
2+
3+
To run use `$ deno test -- --benchmark`

benchmarks/_one_property_test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2018-2021 Gamebridge.ai authors. All rights reserved. MIT license.
2+
3+
import { test } from "../test_deps.ts";
4+
import { ignore, input, only, Root, TestAmount } from "./_utils.ts";
5+
6+
test({
7+
only,
8+
ignore,
9+
name:
10+
`Benchmark fromJSON() ${TestAmount.OneMillion} times and average out of ${TestAmount.Ten} runs`,
11+
fn() {
12+
for (let i = 0; i < TestAmount.Ten; i++) {
13+
performance.mark(`start_fromJSON_benchmark_1m_${i}`);
14+
for (let j = 0; j < TestAmount.OneMillion; j++) {
15+
new Root().fromJSON(input);
16+
}
17+
performance.mark(`end_fromJSON_benchmark_1m_${i}`);
18+
performance.measure(
19+
"measure_fromJSON_benchmark_1m",
20+
`start_fromJSON_benchmark_1m_${i}`,
21+
`end_fromJSON_benchmark_1m_${i}`,
22+
);
23+
}
24+
const fromJSONMeasured = performance.getEntriesByName(
25+
"measure_fromJSON_benchmark_1m",
26+
);
27+
28+
const avg = fromJSONMeasured.reduce((acc, { duration }) =>
29+
acc + duration, 0) /
30+
fromJSONMeasured.length;
31+
console.log(avg);
32+
},
33+
});
34+
35+
test({
36+
only,
37+
ignore,
38+
name:
39+
`Benchmark toJSON() ${TestAmount.OneMillion} times and average out of ${TestAmount.Ten} runs`,
40+
fn() {
41+
const root = new Root().fromJSON(input);
42+
for (let i = 0; i < TestAmount.Ten; i++) {
43+
performance.mark(`start_toJSON_benchmark_1m_${i}`);
44+
for (let j = 0; j < TestAmount.OneMillion; j++) {
45+
root.toJSON();
46+
}
47+
performance.mark(`end_toJSON_benchmark_1m_${i}`);
48+
performance.measure(
49+
"measure_toJSON_benchmark_1m",
50+
`start_toJSON_benchmark_1m_${i}`,
51+
`end_toJSON_benchmark_1m_${i}`,
52+
);
53+
}
54+
const toJSONMeasured = performance.getEntriesByName(
55+
"measure_toJSON_benchmark_1m",
56+
);
57+
const avg = toJSONMeasured.reduce((acc, { duration }) =>
58+
acc + duration, 0) /
59+
toJSONMeasured.length;
60+
console.log(avg);
61+
},
62+
});
63+
64+
test({
65+
only,
66+
ignore,
67+
name:
68+
`Benchmark tsSerialize() ${TestAmount.OneMillion} times and average out of ${TestAmount.Ten} runs`,
69+
fn() {
70+
const root = new Root().fromJSON(input);
71+
for (let i = 0; i < TestAmount.Ten; i++) {
72+
performance.mark(`start_tsSerialize_benchmark_1m_${i}`);
73+
for (let j = 0; j < TestAmount.OneMillion; j++) {
74+
root.tsSerialize();
75+
}
76+
performance.mark(`end_tsSerialize_benchmark_1m_${i}`);
77+
performance.measure(
78+
"measure_tsSerialize_benchmark_1m",
79+
`start_tsSerialize_benchmark_1m_${i}`,
80+
`end_tsSerialize_benchmark_1m_${i}`,
81+
);
82+
}
83+
84+
const tsSerializeMeasured = performance.getEntriesByName(
85+
"measure_tsSerialize_benchmark_1m",
86+
);
87+
const avg = tsSerializeMeasured.reduce((acc, { duration }) =>
88+
acc + duration, 0) /
89+
tsSerializeMeasured.length;
90+
console.log(avg);
91+
},
92+
});

benchmarks/_utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018-2021 Gamebridge.ai authors. All rights reserved. MIT license.
2+
3+
import { Serializable } from "../serializable.ts";
4+
import { SerializeProperty } from "../serialize_property.ts";
5+
6+
export const only = Deno.args.includes("--benchmark");
7+
export const ignore = !only;
8+
9+
export enum TestAmount {
10+
Ten = 10,
11+
OneMillion = 1e6,
12+
}
13+
14+
export class Root extends Serializable {
15+
@SerializeProperty()
16+
property: string = "default";
17+
}
18+
19+
export const input = {
20+
property: "from_input",
21+
};

0 commit comments

Comments
 (0)