1
1
import 'reflect-metadata' ;
2
- import path from 'node:path' ;
3
- import { assert } from 'n8n-workflow' ;
4
- import glob from 'fast-glob' ;
5
- import callsites from 'callsites' ;
6
- import type { Task } from './types' ;
7
2
import { globalHooks } from './hooks' ;
8
3
9
4
/* eslint-disable import/no-extraneous-dependencies */
10
5
import { withCodSpeed } from '@codspeed/tinybench-plugin' ;
11
6
import Bench from 'tinybench' ;
7
+ import { collectSuites , registerSuites , suiteCount } from './lib/suites' ;
12
8
/* eslint-enable import/no-extraneous-dependencies */
13
9
14
- const map : {
15
- [ suiteFilepath : string ] : {
16
- hooks : Partial < { beforeEach : ( ) => Promise < void > } > ;
17
- tasks : Task [ ] ;
18
- } ;
19
- } = { } ;
20
-
21
- function suiteFilePath ( ) {
22
- const filePath = callsites ( )
23
- . map ( ( site ) => site . getFileName ( ) )
24
- . filter ( ( site ) : site is string => site !== null )
25
- . find ( ( site ) => site . endsWith ( '.tasks.js' ) ) ;
26
-
27
- assert ( filePath !== undefined ) ;
28
-
29
- return filePath ;
30
- }
31
-
32
- export function task ( description : string , operation : Task [ 'operation' ] ) {
33
- map [ suiteFilePath ( ) ] ||= { hooks : { } , tasks : [ ] } ;
34
- map [ suiteFilePath ( ) ] . tasks . push ( { description, operation } ) ;
35
- }
36
-
37
- export function beforeEach ( fn : ( ) => Promise < void > ) {
38
- map [ suiteFilePath ( ) ] ||= { hooks : { } , tasks : [ ] } ;
39
- map [ suiteFilePath ( ) ] . hooks . beforeEach = fn ;
40
- }
41
-
42
- async function loadTasks ( ) {
43
- const files = await glob ( '**/*.tasks.js' , {
44
- cwd : path . join ( 'dist' , 'benchmark' ) ,
45
- absolute : true ,
46
- } ) ;
47
-
48
- for ( const file of files ) {
49
- await import ( file ) ;
50
- }
51
- }
10
+ export { beforeEach , task } from './lib/suites' ;
52
11
53
12
async function main ( ) {
54
- await loadTasks ( ) ;
13
+ await collectSuites ( ) ;
55
14
56
- const suitesCount = Object . keys ( map ) . length ;
15
+ const count = suiteCount ( ) ;
57
16
58
- if ( suitesCount === 0 ) {
17
+ if ( count === 0 ) {
59
18
console . log ( 'No benchmarking suites found' ) ;
60
19
return ;
61
20
}
@@ -69,21 +28,9 @@ async function main() {
69
28
70
29
const bench = process . env . CI === 'true' ? withCodSpeed ( _bench ) : _bench ;
71
30
72
- for ( const filePath of Object . keys ( map ) ) {
73
- const { tasks, hooks } = map [ filePath ] ;
74
-
75
- for ( const t of tasks ) {
76
- /**
77
- * `beforeAll` in tinybench is called once before all iterations of a single operation.
78
- * This is functionally equivalent to `beforeEach` in jest and vitest.
79
- */
80
- const options = hooks . beforeEach ? { beforeAll : hooks . beforeEach } : { } ;
81
-
82
- bench . add ( t . description , t . operation , options ) ;
83
- }
84
- }
31
+ registerSuites ( bench ) ;
85
32
86
- console . log ( `Running ${ suitesCount } benchmarking suites...` ) ;
33
+ console . log ( `Running ${ count } benchmarking suites...` ) ;
87
34
88
35
await bench . run ( ) ;
89
36
0 commit comments