Skip to content

Commit

Permalink
Introduce describe()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Apr 23, 2024
1 parent 4157807 commit 82b3f78
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/benchmark/benchmark.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ All workflows with default settings unless otherwise specified.

1.1. using "Respond immediately" mode
1.2. using "When last node finishes" mode
1.3. using "Using 'Respond to Webhook' node" mode
1.3. using 'Respond to Webhook' node mode
29 changes: 17 additions & 12 deletions packages/cli/src/benchmark/lib/suites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,27 @@ function suiteFilePath() {
return filePath;
}

/**
* Run a benchmarking task, i.e. a single operation whose performance to measure.
*/
export function task(description: string, operation: Task['operation']) {
export function describe(suiteName: string, suiteFn: () => void) {
const filePath = suiteFilePath();

suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].tasks.push({ description, operation });
suites[filePath] = { name: suiteName, hooks: {}, tasks: [] };

suiteFn();
}

export function task(taskName: string, operation: Task['operation']) {
const filePath = suiteFilePath();

suites[filePath].tasks.push({
description: suites[filePath].name + ' ' + taskName,
operation,
});
}

// @TODO: Rename next two utils to dismbiguate?

/**
* Setup step to run once before each benchmarking task in a suite.
* Only one `beforeEach` is allowed per suite.
* Setup step to run once before all iterations of each benchmarking task in a suite.
*/
export function beforeEach(fn: Callback) {
const filePath = suiteFilePath();
Expand All @@ -78,13 +86,11 @@ export function beforeEach(fn: Callback) {
throw new DuplicateHookError('beforeEach', filePath);
}

suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].hooks.beforeEach = fn;
}

/**
* Teardown step to run once after each benchmarking task in a suite.
* Only one `afterEach` is allowed per suite.
* Teardown step to run once after all iterations of each benchmarking task in a suite.
*/
export function afterEach(fn: Callback) {
const filePath = suiteFilePath();
Expand All @@ -93,6 +99,5 @@ export function afterEach(fn: Callback) {
throw new DuplicateHookError('afterEach', filePath);
}

suites[filePath] ||= { hooks: {}, tasks: [] };
suites[filePath].hooks.afterEach = fn;
}
1 change: 1 addition & 0 deletions packages/cli/src/benchmark/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
export type Suites = {
[suiteFilepath: string]: {
name: string;
hooks: {
beforeEach?: Callback;
afterEach?: Callback;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/benchmark/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Bench from 'tinybench';
import { withCodSpeed } from '@codspeed/tinybench-plugin';
/* eslint-enable import/no-extraneous-dependencies */

export { beforeEach, afterEach, task } from './lib/suites';
export { describe, task, beforeEach, afterEach } from './lib/suites';

async function main() {
const dbType = config.getEnv('database.type');
Expand Down
29 changes: 12 additions & 17 deletions packages/cli/src/benchmark/tasks/webhook.tasks.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import axios from 'axios';
import { task } from '../main.js';
import { task, describe } from '../main';

const client = axios.create({ baseURL: 'http://localhost:5678/' });
// @TODO: Rename file?

task(
'1.1. Production workflow with authless webhook node using "Respond immediately" mode',
async () => {
describe('1. Production workflow with authless webhook node', () => {
const client = axios.create({ baseURL: 'http://localhost:5678/' });

task('(1.1) using "Respond immediately" mode', async () => {
await client.get('/webhook/1.1');
},
);
});

task(
'1.2. Production workflow with authless webhook node using "When last node finishes" mode',
async () => {
task('(1.2) using "When last node finishes" mode', async () => {
await client.get('/webhook/1.2');
},
);
});

task(
'1.3. Production workflow with authless webhook node using "Using \'Respond to Webhook\' node" mode',
async () => {
task('(1.3) using "Respond to Webhook" node mode', async () => {
await client.get('/webhook/1.3');
},
);
});
});

0 comments on commit 82b3f78

Please sign in to comment.