@@ -13,7 +13,11 @@ import { jsonParse } from 'n8n-workflow';
13
13
import { readFile } from 'fs/promises' ;
14
14
import type { WorkflowRequest } from '@/workflows/workflow.request' ;
15
15
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner' ;
16
+ import { Logger } from '@/Logger' ;
16
17
18
+ /**
19
+ * Create a temp `.n8n` dir for encryption key, sqlite DB, etc.
20
+ */
17
21
function n8nDir ( ) {
18
22
const baseDirPath = path . join ( tmpdir ( ) , 'n8n-benchmarks/' ) ;
19
23
@@ -31,66 +35,69 @@ function n8nDir() {
31
35
'utf-8' ,
32
36
) ;
33
37
34
- /**
35
- * @TODO Better approach than overriding? Setting N8N_USER_FOLDER has no effect
36
- */
38
+ // @TODO : Find better approach than overriding like this
39
+ // Setting N8N_USER_FOLDER has no effect
37
40
const instanceSettings = Container . get ( InstanceSettings ) ;
38
41
instanceSettings . n8nFolder = _n8nDir ;
39
42
Container . set ( InstanceSettings , instanceSettings ) ;
40
43
41
- console . info ( ' .n8n dir' , _n8nDir ) ;
44
+ Container . get ( Logger ) . info ( `Temp .n8n dir location: ${ instanceSettings . n8nFolder } ` ) ;
42
45
}
43
46
44
- let main : Start ;
45
-
46
- async function mainProcess ( ) {
47
- const args : string [ ] = [ ] ;
48
- const _config = new Config ( { root : __dirname } ) ;
49
-
50
- main = new Start ( args , _config ) ;
51
-
52
- await main . init ( ) ;
53
- await main . run ( ) ;
54
- }
55
-
56
- async function loadFixtures ( owner : User ) {
57
- const files = await glob ( 'fixtures/*.json' , {
47
+ /**
48
+ * Load into DB and activate in memory all workflows to use in benchmarks.
49
+ */
50
+ async function prepareWorkflows ( owner : User ) {
51
+ const files = await glob ( 'workflows/*.json' , {
58
52
cwd : path . join ( 'dist' , 'benchmark' ) ,
59
53
absolute : true ,
60
54
} ) ;
61
55
62
- const fixtures : WorkflowRequest . CreatePayload [ ] = [ ] ;
56
+ const workflows : WorkflowRequest . CreatePayload [ ] = [ ] ;
63
57
64
58
for ( const file of files ) {
65
59
const content = await readFile ( file , 'utf8' ) ;
66
- fixtures . push ( jsonParse < WorkflowRequest . CreatePayload > ( content ) ) ;
60
+ workflows . push ( jsonParse < WorkflowRequest . CreatePayload > ( content ) ) ;
67
61
}
68
62
69
- for ( const fixture of fixtures ) {
70
- try {
71
- // @ts -ignore @TODO Fix typing
72
- await Container . get ( WorkflowsController ) . create ( { body : fixture , user : owner } ) ;
73
- await Container . get ( ActiveWorkflowRunner ) . add ( fixture . id as string , 'activate' ) ;
74
- } catch ( e ) {
75
- console . log ( e ) ;
76
- }
63
+ for ( const workflow of workflows ) {
64
+ // @ts -ignore @TODO Fix typing
65
+ await Container . get ( WorkflowsController ) . create ( { body : workflow , user : owner } ) ;
66
+ await Container . get ( ActiveWorkflowRunner ) . add ( workflow . id as string , 'activate' ) ;
77
67
}
68
+ }
69
+
70
+ let main : Start ;
78
71
79
- // const allActive = await Container.get(WorkflowRepository).getAllActive();
80
- // console.log('allActive', allActive);
72
+ /**
73
+ * Start the main n8n process to use in benchmarks.
74
+ */
75
+ async function mainProcess ( ) {
76
+ const args : string [ ] = [ ] ;
77
+ const _config = new Config ( { root : __dirname } ) ;
78
+
79
+ main = new Start ( args , _config ) ;
80
+
81
+ await main . init ( ) ;
82
+ await main . run ( ) ;
81
83
}
82
84
83
- export async function setup ( ) {
85
+ /**
86
+ * Setup to run before once all benchmarks.
87
+ */
88
+ export async function globalSetup ( ) {
84
89
n8nDir ( ) ;
85
90
86
91
await mainProcess ( ) ;
87
- // @TODO : Postgres?
88
92
89
93
const owner = await Container . get ( UserRepository ) . createTestOwner ( ) ;
90
94
91
- await loadFixtures ( owner ) ;
95
+ await prepareWorkflows ( owner ) ;
92
96
}
93
97
94
- export async function teardown ( ) {
98
+ /**
99
+ * Teardown to run before after all benchmarks.
100
+ */
101
+ export async function globalTeardown ( ) {
95
102
await main . stopProcess ( ) ;
96
103
}
0 commit comments