-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add benchmarks for various scenarios
- Loading branch information
Showing
12 changed files
with
820 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable no-var */ | ||
export {}; | ||
|
||
declare global { | ||
declare var benchConfig: { | ||
taskCount?: number; | ||
timeout?: number; | ||
}; | ||
|
||
declare type BenchmarkResult = { | ||
Duration: string; | ||
ELU: string; | ||
}; | ||
|
||
declare type BenchmarkResults = Record<string, BenchmarkResult>; | ||
|
||
declare var results = {} as { | ||
add: (name: string, result: BenchmarkResult) => void; | ||
delete(name: string): void; | ||
print(): void; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const actualResults: BenchmarkResults = {}; | ||
|
||
global.benchConfig = { | ||
taskCount: 100, | ||
timeout: 5, | ||
}; | ||
|
||
global.results = { | ||
add(name, result) { | ||
actualResults[name] = result; | ||
}, | ||
delete(name) { | ||
delete actualResults[name]; | ||
}, | ||
print() { | ||
console.table(actualResults); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use strict"; | ||
|
||
import "./benchmark-setup"; | ||
|
||
import yoctoSpinner from "yocto-spinner"; | ||
|
||
import { | ||
with_ct_10, | ||
with_ct_100, | ||
with_ct_1000, | ||
with_ct_default, | ||
} from "./units/ct"; | ||
import { for_each } from "./units/for-each"; | ||
import { for_loop } from "./units/for-loop"; | ||
import { while_loop } from "./units/while-loop"; | ||
|
||
benchConfig.taskCount = 1000; | ||
|
||
async function main() { | ||
const fns = [ | ||
while_loop, | ||
for_loop, | ||
for_each, | ||
with_ct_default, | ||
with_ct_10, | ||
with_ct_100, | ||
with_ct_1000, | ||
]; | ||
|
||
const spinner = yoctoSpinner({ | ||
text: "Benchmarking", | ||
}).start(); | ||
|
||
for (const index in fns) { | ||
const idx = Number(index); | ||
const fn = fns[idx]; | ||
spinner.text = `Benchmarking (${idx + 1}/${fns.length}) - ${ | ||
fn.displayName | ||
} `; | ||
await fn(); | ||
} | ||
|
||
spinner.success("Success!"); | ||
results.print(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { CT } from "../.."; | ||
import { generateTasks } from "../../../testing-utils/utils/generate-tasks"; | ||
|
||
function whatIsLast(last: number) { | ||
return function isLast(value: number, callback: () => void) { | ||
if (value === last) { | ||
callback(); | ||
} | ||
}; | ||
} | ||
|
||
type IsLast = ReturnType<typeof whatIsLast>; | ||
|
||
export type BenchmarkArgs = { | ||
tasks: CT.TasksWithDone; | ||
total: number; | ||
isLast: IsLast; | ||
done: () => void; | ||
}; | ||
|
||
function benchInner( | ||
name: string, | ||
benchmark: (args: BenchmarkArgs) => Promise<void> | void | ||
) { | ||
const startMark = `${name}-start`; | ||
const endMark = `${name}-end`; | ||
|
||
const task = performance.timerify(() => { | ||
return new Promise((resolve) => { | ||
const tasks = generateTasks(benchConfig.taskCount, benchConfig.timeout); | ||
const total = tasks.length; | ||
const isLast = whatIsLast(total - 1); | ||
|
||
performance.mark(startMark); | ||
|
||
benchmark({ | ||
tasks, | ||
total, | ||
done: () => { | ||
resolve(true); | ||
performance.mark(endMark); | ||
const entry = performance.measure(name, startMark, endMark); | ||
|
||
results.add(name, { | ||
Duration: `${entry.duration.toFixed(2)}ms`, | ||
ELU: `${( | ||
performance.eventLoopUtilization().utilization * 100 | ||
).toFixed(2)}%`, | ||
}); | ||
}, | ||
isLast, | ||
}); | ||
}); | ||
}); | ||
|
||
const value = task as typeof task & { | ||
displayName: string; | ||
}; | ||
|
||
value.displayName = name; | ||
|
||
return value; | ||
} | ||
|
||
export const bench = benchInner as typeof benchInner & { | ||
displayName: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function whatIsLast(last: number) { | ||
return function isLast(value: number, callback: () => void) { | ||
if (value === last) { | ||
callback(); | ||
} | ||
}; | ||
} | ||
|
||
export type IsLast = ReturnType<typeof whatIsLast>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TaskRunner } from "../../TaskRunner"; | ||
import { bench } from "../helpers/benchmarker"; | ||
|
||
const withCT = (name: string, concurrency = 3) => | ||
bench(`with-ct-${name}`, ({ done, tasks }) => { | ||
const runner = new TaskRunner({ | ||
concurrency, | ||
onEnd: done, | ||
}); | ||
|
||
runner.addMultiple(tasks); | ||
|
||
runner.start(); | ||
}); | ||
|
||
export const with_ct_default = withCT("default"); | ||
export const with_ct_10 = withCT("10", 10); | ||
export const with_ct_100 = withCT("100", 100); | ||
export const with_ct_1000 = withCT("1000", 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { bench } from "../helpers/benchmarker"; | ||
|
||
export const for_each = bench("for-each", ({ tasks, isLast, done }) => { | ||
tasks.forEach((task, index) => { | ||
task(() => { | ||
isLast(index, done); | ||
}, index); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { bench } from "../helpers/benchmarker"; | ||
|
||
export const for_loop = bench("for-loop", ({ tasks, isLast, done }) => { | ||
for (const idx in tasks) { | ||
const task = tasks[idx]; | ||
const count = Number(idx); | ||
|
||
task(() => { | ||
isLast(count, done); | ||
}, count); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { bench } from "../helpers/benchmarker"; | ||
|
||
export const while_loop = bench("while-loop", ({ tasks, isLast, done }) => { | ||
const slicedTasks = tasks.slice(); | ||
|
||
let resolved = false; | ||
let count = 0; | ||
|
||
while (!resolved && slicedTasks.length) { | ||
const task = slicedTasks.shift()!; | ||
|
||
task(() => { | ||
count++; | ||
isLast(count, () => { | ||
resolved = true; | ||
done(); | ||
}); | ||
}, slicedTasks.length); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.