Skip to content

Commit d0be040

Browse files
committed
Cleanup
1 parent d6d7efb commit d0be040

File tree

6 files changed

+116
-110
lines changed

6 files changed

+116
-110
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const BACKEND_BASE_URL = 'http://localhost:5678';

packages/cli/src/benchmark/lib/errors/duplicate-hook.error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ApplicationError } from 'n8n-workflow';
33
export class DuplicateHookError extends ApplicationError {
44
constructor(hookName: 'beforeEach' | 'afterEach', filePath: string) {
55
super(
6-
`Duplicate \`${hookName}\` hook found in benchmarking suite \`${filePath}\`. Please define a single \`${hookName}\` hook for this suite.`,
6+
`Duplicate \`${hookName}\` hook found at \`${filePath}\`. Please define a single \`${hookName}\` hook for this file.`,
77
{ level: 'warning' },
88
);
99
}

packages/cli/src/benchmark/lib/errors/unsupported-db.error.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { ApplicationError } from 'n8n-workflow';
22

33
export class UnsupportedDatabaseError extends ApplicationError {
44
constructor() {
5-
super('Currently only sqlite is supported for benchmarking', { level: 'warning' });
5+
super(
6+
'Currently only sqlite is supported for benchmarking. Please ensure DB_TYPE is set to `sqlite`',
7+
{ level: 'warning' },
8+
);
69
}
710
}
+4-105
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,7 @@
11
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';
1072

3+
export { BACKEND_BASE_URL } from './constants';
4+
export { suite, task, beforeEach, afterEach, collectSuites, registerSuites } from './registration';
5+
export * as hooks from './hooks';
1086
export type { Suites } from './types';
7+
export { UnsupportedDatabaseError } from './errors/unsupported-db.error';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
// @TODO: Support async suiteFn
60+
export function suite(suiteName: string, suiteFn: () => void) {
61+
const filePath = suiteFilePath();
62+
63+
if (suites[filePath]) throw new DuplicateSuiteError(filePath);
64+
65+
suites[filePath] = { name: suiteName, hooks: {}, tasks: [] };
66+
67+
suiteFn();
68+
}
69+
70+
export function task(taskName: string, operation: Task['operation']) {
71+
const filePath = suiteFilePath();
72+
73+
suites[filePath].tasks.push({
74+
name: suites[filePath].name + ' ' + taskName,
75+
operation,
76+
});
77+
}
78+
79+
// @TODO: Rename `beforeEach` and `afteEach` to dismbiguate? e.g. `beforeEachTask`, `afterEachTask`
80+
81+
/**
82+
* Setup step to run once before all iterations of each benchmarking task in a suite.
83+
*/
84+
export function beforeEach(fn: Callback) {
85+
const filePath = suiteFilePath();
86+
87+
if (suites[filePath]?.hooks.beforeEach) {
88+
throw new DuplicateHookError('beforeEach', filePath);
89+
}
90+
91+
suites[filePath].hooks.beforeEach = fn;
92+
}
93+
94+
/**
95+
* Teardown step to run once after all iterations of each benchmarking task in a suite.
96+
*/
97+
export function afterEach(fn: Callback) {
98+
const filePath = suiteFilePath();
99+
100+
if (suites[filePath]?.hooks.afterEach) {
101+
throw new DuplicateHookError('afterEach', filePath);
102+
}
103+
104+
suites[filePath].hooks.afterEach = fn;
105+
}

packages/cli/src/benchmark/main.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import 'reflect-metadata';
22
import Container from 'typedi';
33
import config from '@/config';
44
import { Logger } from '@/Logger';
5-
import * as hooks from './lib/hooks';
6-
import { collectSuites, registerSuites } from './lib';
7-
import { UnsupportedDatabaseError } from './lib/errors/unsupported-db.error';
5+
import { collectSuites, registerSuites, UnsupportedDatabaseError, hooks } from './lib';
86

97
/* eslint-disable import/no-extraneous-dependencies */
108
import Bench from 'tinybench';

0 commit comments

Comments
 (0)