( name: string, noun: string, unit: string, actions: (N: number, stepSize: number) => [number, number], maxResult: number, before?: () => void, maxN?: number, steps?: number, )
| 4 | import {expect, test} from 'vitest'; |
| 5 | |
| 6 | export const repeat = ( |
| 7 | name: string, |
| 8 | noun: string, |
| 9 | unit: string, |
| 10 | actions: (N: number, stepSize: number) => [number, number], |
| 11 | maxResult: number, |
| 12 | before?: () => void, |
| 13 | maxN?: number, |
| 14 | steps?: number, |
| 15 | ): void => { |
| 16 | test(`${name}`, () => { |
| 17 | if (before != null) { |
| 18 | before(); |
| 19 | } |
| 20 | |
| 21 | if (maxN == null) { |
| 22 | maxN = 10000; |
| 23 | } |
| 24 | |
| 25 | if (steps == null) { |
| 26 | steps = 40; |
| 27 | } |
| 28 | |
| 29 | const stepSize = maxN / steps; |
| 30 | const runs = []; |
| 31 | for (let N = stepSize; N <= maxN; N += stepSize) { |
| 32 | runs.push(N); |
| 33 | } |
| 34 | |
| 35 | let totalDuration = 0; |
| 36 | let totalCount = 0; |
| 37 | |
| 38 | const results = runs.map((N) => { |
| 39 | const [duration, count] = actions(N, stepSize); |
| 40 | totalDuration += duration; |
| 41 | totalCount += count; |
| 42 | return parseFloat((duration / count).toFixed(2)); |
| 43 | }); |
| 44 | |
| 45 | const average = parseFloat((totalDuration / totalCount).toFixed(2)); |
| 46 | const averages = new Array(steps).fill(average); |
| 47 | const maxes = new Array(steps).fill(maxResult); |
| 48 | |
| 49 | // eslint-disable-next-line no-console |
| 50 | console.log( |
| 51 | `${name} with multiple ${noun}, ${unit}\n` + |
| 52 | ` From: ${stepSize} ${noun}\n` + |
| 53 | ` To: ${maxN} ${noun}\n` + |
| 54 | `First: ${results[0]} ${unit}\n` + |
| 55 | ` Last: ${results[results.length - 1]} ${unit}\n` + |
| 56 | ` Min: ${Math.min(...results)} ${unit}\n` + |
| 57 | ` Max: ${Math.max(...results)} ${unit}\n` + |
| 58 | ` Avg: ${average} ${unit}\n\n` + |
| 59 | `${plot([maxes, averages, results], { |
| 60 | height: 15, |
| 61 | min: 0, |
| 62 | max: maxResult, |
| 63 | colors: [red, blue, yellow], |
no test coverage detected
searching dependent graphs…