|
1 | 1 | import 'reflect-metadata';
|
2 |
| -import path from 'node:path'; |
3 |
| -import type Bench from 'tinybench'; |
4 |
| -import { assert } from 'n8n-workflow'; |
5 |
| -import glob from 'fast-glob'; |
6 |
| -import callsites from 'callsites'; |
7 |
| -import type { Suites, Task, Callback } from './types'; |
8 |
| -import { DuplicateHookError } from './errors/duplicate-hook.error'; |
9 |
| -import { DuplicateSuiteError } from './errors/duplicate-suite.error'; |
10 |
| - |
11 |
| -const suites: Suites = {}; |
12 |
| - |
13 |
| -export async function collectSuites() { |
14 |
| - const files = await glob('**/*.suite.js', { |
15 |
| - cwd: path.join('dist', 'benchmark'), |
16 |
| - absolute: true, |
17 |
| - }); |
18 |
| - |
19 |
| - for (const f of files) { |
20 |
| - await import(f); |
21 |
| - } |
22 |
| - |
23 |
| - return suites; |
24 |
| -} |
25 |
| - |
26 |
| -export function registerSuites(bench: Bench) { |
27 |
| - for (const { hooks, tasks } of Object.values(suites)) { |
28 |
| - /** |
29 |
| - * In tinybench, `beforeAll` and `afterAll` refer to all iterations of |
30 |
| - * a single task, while `beforeEach` and `afterEach` refer to each iteration. |
31 |
| - * |
32 |
| - * In jest and vitest, `beforeAll` and `afterAll` refer to all tests in a suite, |
33 |
| - * while `beforeEach` and `afterEach` refer to each individual test. |
34 |
| - * |
35 |
| - * We rename tinybench's hooks to prevent confusion from this difference. |
36 |
| - */ |
37 |
| - const options: Record<string, Callback> = {}; |
38 |
| - |
39 |
| - if (hooks.beforeEach) options.beforeAll = hooks.beforeEach; |
40 |
| - if (hooks.afterEach) options.afterAll = hooks.afterEach; |
41 |
| - |
42 |
| - for (const t of tasks) { |
43 |
| - bench.add(t.name, t.operation, options); |
44 |
| - } |
45 |
| - } |
46 |
| -} |
47 |
| - |
48 |
| -function suiteFilePath() { |
49 |
| - const filePath = callsites() |
50 |
| - .map((site) => site.getFileName()) |
51 |
| - .filter((site): site is string => site !== null) |
52 |
| - .find((site) => site.endsWith('.suite.js')); |
53 |
| - |
54 |
| - assert(filePath !== undefined); |
55 |
| - |
56 |
| - return filePath; |
57 |
| -} |
58 |
| - |
59 |
| -export function suite(suiteName: string, suiteFn: () => void) { |
60 |
| - const filePath = suiteFilePath(); |
61 |
| - |
62 |
| - if (suites[filePath]) throw new DuplicateSuiteError(filePath); |
63 |
| - |
64 |
| - suites[filePath] = { name: suiteName, hooks: {}, tasks: [] }; |
65 |
| - |
66 |
| - suiteFn(); |
67 |
| -} |
68 |
| - |
69 |
| -export function task(taskName: string, operation: Task['operation']) { |
70 |
| - const filePath = suiteFilePath(); |
71 |
| - |
72 |
| - suites[filePath].tasks.push({ |
73 |
| - name: suites[filePath].name + ' ' + taskName, |
74 |
| - operation, |
75 |
| - }); |
76 |
| -} |
77 |
| - |
78 |
| -// @TODO: Rename next two utils to dismbiguate? |
79 |
| - |
80 |
| -/** |
81 |
| - * Setup step to run once before all iterations of each benchmarking task in a suite. |
82 |
| - */ |
83 |
| -export function beforeEach(fn: Callback) { |
84 |
| - const filePath = suiteFilePath(); |
85 |
| - |
86 |
| - if (suites[filePath]?.hooks.beforeEach) { |
87 |
| - throw new DuplicateHookError('beforeEach', filePath); |
88 |
| - } |
89 |
| - |
90 |
| - suites[filePath].hooks.beforeEach = fn; |
91 |
| -} |
92 |
| - |
93 |
| -/** |
94 |
| - * Teardown step to run once after all iterations of each benchmarking task in a suite. |
95 |
| - */ |
96 |
| -export function afterEach(fn: Callback) { |
97 |
| - const filePath = suiteFilePath(); |
98 |
| - |
99 |
| - if (suites[filePath]?.hooks.afterEach) { |
100 |
| - throw new DuplicateHookError('afterEach', filePath); |
101 |
| - } |
102 |
| - |
103 |
| - suites[filePath].hooks.afterEach = fn; |
104 |
| -} |
105 |
| - |
106 |
| -export const BACKEND_BASE_URL = 'http://localhost:5678'; |
107 | 2 |
|
| 3 | +export { BACKEND_BASE_URL } from './constants'; |
| 4 | +export { suite, task, beforeEach, afterEach, collectSuites, registerSuites } from './registration'; |
| 5 | +export * as hooks from './hooks'; |
108 | 6 | export type { Suites } from './types';
|
| 7 | +export { UnsupportedDatabaseError } from './errors/unsupported-db.error'; |
0 commit comments