|
| 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 | +}); |
0 commit comments