|
| 1 | +import os from 'node:os'; |
| 2 | +import { performance as performanceHooks } from 'node:perf_hooks'; |
| 3 | + |
| 4 | +const [{ speed: cpuSpeed = 0 } = {}] = os.cpus(); |
| 5 | + |
| 6 | +export { cpuSpeed }; |
| 7 | + |
| 8 | +export async function importFresh(modulePath: string): Promise<void> { |
| 9 | + const cacheBuster = `${performanceHooks.now()}_${Math.random()}`; |
| 10 | + const cacheBustingModulePath = `${modulePath}?buster=${cacheBuster}`; |
| 11 | + |
| 12 | + await import(cacheBustingModulePath); |
| 13 | +} |
| 14 | + |
| 15 | +function isPositiveNumber(value: number): boolean { |
| 16 | + return value > 0; |
| 17 | +} |
| 18 | + |
| 19 | +type Result = { |
| 20 | + readonly duration: number; |
| 21 | + readonly memory: number; |
| 22 | +}; |
| 23 | + |
| 24 | +type MedianResult = { |
| 25 | + readonly medianDuration: number; |
| 26 | + readonly medianMemory: number; |
| 27 | +}; |
| 28 | + |
| 29 | +function median(list: readonly number[]): number { |
| 30 | + const listParts = 2; |
| 31 | + const medianIndex = Math.floor(list.length / listParts); |
| 32 | + const lowerValue = list[medianIndex - 1] ?? 0; |
| 33 | + const upperValue = list[medianIndex] ?? 0; |
| 34 | + |
| 35 | + if (list.length % listParts === 0) { |
| 36 | + return (lowerValue + upperValue) / listParts; |
| 37 | + } |
| 38 | + |
| 39 | + return upperValue; |
| 40 | +} |
| 41 | + |
| 42 | +export function runSyncBenchmark(fn: () => void, count: number): Readonly<MedianResult> { |
| 43 | + const results: Result[] = []; |
| 44 | + |
| 45 | + Array.from({ length: count }).forEach(() => { |
| 46 | + const startTime = performanceHooks.now(); |
| 47 | + const startMemory = process.memoryUsage.rss(); |
| 48 | + fn(); |
| 49 | + const endTime = performanceHooks.now(); |
| 50 | + const endMemory = process.memoryUsage.rss(); |
| 51 | + const duration = endTime - startTime; |
| 52 | + const memory = endMemory - startMemory; |
| 53 | + |
| 54 | + results.push({ duration, memory }); |
| 55 | + }); |
| 56 | + |
| 57 | + const medianDuration = median(results.map((result) => { |
| 58 | + return result.duration; |
| 59 | + })); |
| 60 | + const medianMemory = median( |
| 61 | + results |
| 62 | + .map((result) => { |
| 63 | + return result.memory; |
| 64 | + }) |
| 65 | + .filter(isPositiveNumber) |
| 66 | + ); |
| 67 | + |
| 68 | + return { medianDuration, medianMemory }; |
| 69 | +} |
| 70 | + |
| 71 | +async function measureSingleAsyncTask(fn: () => Promise<void>): Promise<Result> { |
| 72 | + const startTime = performanceHooks.now(); |
| 73 | + const startMemory = process.memoryUsage().rss; |
| 74 | + await fn(); |
| 75 | + const endTime = performanceHooks.now(); |
| 76 | + const endMemory = process.memoryUsage().rss; |
| 77 | + const duration = endTime - startTime; |
| 78 | + const memory = endMemory - startMemory; |
| 79 | + |
| 80 | + return { duration, memory }; |
| 81 | +} |
| 82 | + |
| 83 | +export async function runAsyncBenchmark(fn: () => Promise<void>, count: number): Promise<MedianResult> { |
| 84 | + const results = []; |
| 85 | + |
| 86 | + for (let iteration = 0; iteration < count; iteration += 1) { |
| 87 | + const result = await measureSingleAsyncTask(fn); |
| 88 | + results.push(result); |
| 89 | + } |
| 90 | + |
| 91 | + const medianDuration = median(results.map((result) => { |
| 92 | + return result.duration; |
| 93 | + })); |
| 94 | + const medianMemory = median( |
| 95 | + results |
| 96 | + .map((result) => { |
| 97 | + return result.memory; |
| 98 | + }) |
| 99 | + .filter(isPositiveNumber) |
| 100 | + ); |
| 101 | + |
| 102 | + return { medianDuration, medianMemory }; |
| 103 | +} |
0 commit comments