Skip to content

Commit

Permalink
perf: Reduce variance in nodes-base benchmarks (#9363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored May 10, 2024
1 parent f581e40 commit 4ccef05
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
14 changes: 12 additions & 2 deletions packages/nodes-base/test/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import glob from 'fast-glob';
import Bench from 'tinybench';
import { withCodSpeed } from '@codspeed/tinybench-plugin';
import { setup, workflowToTests as toTests } from './nodes/Helpers';
import { executeWorkflow } from './nodes/ExecuteWorkflow';
import { performExecution, setupExecution } from './nodes/ExecuteWorkflow';

async function main() {
const filePaths = await glob('nodes/**/*.workflow.json');
Expand All @@ -14,8 +14,18 @@ async function main() {
const bench = withCodSpeed(new Bench({ time: 0, iterations: 1 })); // @TODO temp config

for (const test of tests) {
const { waitPromise, additionalData, executionMode, workflowInstance, nodeExecutionOrder } =
await setupExecution(test, nodeTypes);

bench.add(test.description, async () => {
await executeWorkflow(test, nodeTypes);
await performExecution(
waitPromise,
additionalData,
executionMode,
test,
workflowInstance,
nodeExecutionOrder,
);
});
}

Expand Down
51 changes: 45 additions & 6 deletions packages/nodes-base/test/nodes/ExecuteWorkflow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import nock from 'nock';
import { WorkflowExecute } from 'n8n-core';
import type { INodeTypes, IRun, IRunExecutionData } from 'n8n-workflow';
import type {
IDeferredPromise,
INodeTypes,
IRun,
IRunExecutionData,
IWorkflowExecuteAdditionalData,
WorkflowExecuteMode,
} from 'n8n-workflow';
import { createDeferredPromise, Workflow } from 'n8n-workflow';
import * as Helpers from './Helpers';
import type { WorkflowTestData } from './types';

export async function executeWorkflow(testData: WorkflowTestData, nodeTypes: INodeTypes) {
export async function setupExecution(testData: WorkflowTestData, nodeTypes: INodeTypes) {
if (testData.nock) {
const { baseUrl, mocks } = testData.nock;
const agent = nock(baseUrl);
Expand All @@ -30,7 +37,24 @@ export async function executeWorkflow(testData: WorkflowTestData, nodeTypes: INo
testData,
);

let executionData: IRun;
return {
waitPromise,
additionalData,
executionMode,
testData,
workflowInstance,
nodeExecutionOrder,
};
}

export async function performExecution(
waitPromise: IDeferredPromise<IRun>,
additionalData: IWorkflowExecuteAdditionalData,
executionMode: WorkflowExecuteMode,
testData: WorkflowTestData,
workflowInstance: Workflow,
nodeExecutionOrder: string[],
) {
const runExecutionData: IRunExecutionData = {
resultData: {
runData: {},
Expand All @@ -50,9 +74,24 @@ export async function executeWorkflow(testData: WorkflowTestData, nodeTypes: INo
],
},
};
const workflowExecute = new WorkflowExecute(additionalData, executionMode, runExecutionData);
executionData = await workflowExecute.processRunExecutionData(workflowInstance);

const workflowExecute = new WorkflowExecute(additionalData, executionMode, runExecutionData);
const finalExecutionData = await workflowExecute.processRunExecutionData(workflowInstance);
const result = await waitPromise.promise();
return { executionData, result, nodeExecutionOrder };

return { executionData: finalExecutionData, result, nodeExecutionOrder };
}

export async function executeWorkflow(testData: WorkflowTestData, nodeTypes: INodeTypes) {
const { waitPromise, additionalData, executionMode, workflowInstance, nodeExecutionOrder } =
await setupExecution(testData, nodeTypes);

return await performExecution(
waitPromise,
additionalData,
executionMode,
testData,
workflowInstance,
nodeExecutionOrder,
);
}

0 comments on commit 4ccef05

Please sign in to comment.