mirror of
https://github.com/n8n-io/n8n.git
synced 2026-12-04 00:30:34 +00:00
test(benchmark): Add sub-workflow scenario (#43187)
This commit is contained in:
1 parent
abdd0dbd6c
commit
562129829f
11 files changed
+288
-9
No files matched your search
@@ -129,6 +129,8 @@ A benchmark scenario defines one or multiple steps to execute and measure. It co
|
||||
|
||||
Available scenarios are located in [`./scenarios`](./scenarios/).
|
||||
|
||||
A workflow keeps the `id` from its file when the CLI imports it. Before the import, the CLI deletes the benchmark workflows (names that start with `[BENCHMARK]`) that have the same name or the same `id`. If a different workflow uses the same `id`, the import fails and the CLI does not change that workflow. To call a workflow from another workflow (for example as a sub-workflow), set a fixed `id` in the workflow file that is called and use that `id` in the workflow that calls it.
|
||||
|
||||
## n8n setups
|
||||
|
||||
An n8n setup defines one n8n runtime configuration with Docker Compose. The setups are in [`scripts/n8n-setups`](scripts/n8n-setups/).
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"properties": {
|
||||
"workflowFiles": {
|
||||
"type": "array",
|
||||
"description": "Workflow files to import, in this order. A workflow keeps the id from its file",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
{
|
||||
"id": "SubWorkflowBenchmarkChild",
|
||||
"name": "Sub-workflow child",
|
||||
"active": false,
|
||||
"nodes": [
|
||||
{
|
||||
"parameters": {
|
||||
"inputSource": "passthrough"
|
||||
},
|
||||
"type": "n8n-nodes-base.executeWorkflowTrigger",
|
||||
"typeVersion": 1.1,
|
||||
"position": [0, 0],
|
||||
"id": "3f1c6a52-9a4e-4b1f-8f43-0d7e5c2b6a11",
|
||||
"name": "When Executed by Another Workflow"
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"assignments": {
|
||||
"assignments": [
|
||||
{
|
||||
"id": "b6f2a1d4-5c3e-4a8b-9d7f-1e2c3b4a5d61",
|
||||
"name": "fullName",
|
||||
"value": "={{ $json.firstName }} {{ $json.lastName }}",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"id": "c7e3b2f5-6d4f-4b9c-8e1a-2f3d4c5b6e72",
|
||||
"name": "ageNextYear",
|
||||
"value": "={{ $json.age + 1 }}",
|
||||
"type": "number"
|
||||
},
|
||||
{
|
||||
"id": "d8f4c3a6-7e5a-4c1d-9f2b-3a4e5d6c7f83",
|
||||
"name": "processedBySubWorkflow",
|
||||
"value": true,
|
||||
"type": "boolean"
|
||||
}
|
||||
]
|
||||
},
|
||||
"includeOtherFields": true,
|
||||
"options": {}
|
||||
},
|
||||
"type": "n8n-nodes-base.set",
|
||||
"typeVersion": 3.4,
|
||||
"position": [224, 0],
|
||||
"id": "e9a5d4b7-8f6b-4d2e-8a3c-4b5f6e7d8a94",
|
||||
"name": "Edit Fields"
|
||||
}
|
||||
],
|
||||
"connections": {
|
||||
"When Executed by Another Workflow": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Edit Fields",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
"pinData": {},
|
||||
"settings": { "executionOrder": "v1" },
|
||||
"staticData": null,
|
||||
"tags": []
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
{
|
||||
"name": "Sub-workflow",
|
||||
"active": true,
|
||||
"nodes": [
|
||||
{
|
||||
"parameters": {
|
||||
"httpMethod": "POST",
|
||||
"path": "sub-workflow-benchmark",
|
||||
"responseMode": "responseNode",
|
||||
"options": {}
|
||||
},
|
||||
"type": "n8n-nodes-base.webhook",
|
||||
"typeVersion": 2.1,
|
||||
"position": [0, 0],
|
||||
"id": "1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c51",
|
||||
"name": "Webhook",
|
||||
"webhookId": "7c1e9f3a-2b4d-4e6f-8a0c-1d3e5f7a9b21"
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"fieldToSplitOut": "body.items",
|
||||
"options": {}
|
||||
},
|
||||
"type": "n8n-nodes-base.splitOut",
|
||||
"typeVersion": 1,
|
||||
"position": [224, 0],
|
||||
"id": "2b3c4d5e-6f7a-4b8c-9d0e-1f2a3b4c5d62",
|
||||
"name": "Split Out"
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"workflowId": {
|
||||
"__rl": true,
|
||||
"value": "SubWorkflowBenchmarkChild",
|
||||
"mode": "id"
|
||||
},
|
||||
"mode": "once",
|
||||
"options": {
|
||||
"waitForSubWorkflow": true
|
||||
}
|
||||
},
|
||||
"type": "n8n-nodes-base.executeWorkflow",
|
||||
"typeVersion": 1.2,
|
||||
"position": [448, 0],
|
||||
"id": "3c4d5e6f-7a8b-4c9d-8e1f-2a3b4c5d6e73",
|
||||
"name": "Execute Workflow"
|
||||
},
|
||||
{
|
||||
"parameters": {
|
||||
"respondWith": "allIncomingItems",
|
||||
"options": {}
|
||||
},
|
||||
"type": "n8n-nodes-base.respondToWebhook",
|
||||
"typeVersion": 1.4,
|
||||
"position": [672, 0],
|
||||
"id": "4d5e6f7a-8b9c-4d0e-9f2a-3b4c5d6e7f84",
|
||||
"name": "Respond to Webhook"
|
||||
}
|
||||
],
|
||||
"connections": {
|
||||
"Webhook": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Split Out",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Split Out": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Execute Workflow",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
},
|
||||
"Execute Workflow": {
|
||||
"main": [
|
||||
[
|
||||
{
|
||||
"node": "Respond to Webhook",
|
||||
"type": "main",
|
||||
"index": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
"pinData": {},
|
||||
"settings": { "executionOrder": "v1" },
|
||||
"staticData": null,
|
||||
"tags": []
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "../scenario.schema.json",
|
||||
"name": "SubWorkflow",
|
||||
"description": "A webhook receives 100 items and sends them to a sub-workflow with an Execute Workflow node. The sub-workflow adds fields with a Set node. The data is returned with RespondToWebhook Node.",
|
||||
"scenarioData": {
|
||||
"workflowFiles": ["sub-workflow-child.json", "sub-workflow.json"]
|
||||
},
|
||||
"scriptPath": "sub-workflow.script.js",
|
||||
"supportedEngines": ["v1", "v2"]
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
import http from 'k6/http';
|
||||
import { check } from 'k6';
|
||||
|
||||
const apiBaseUrl = __ENV.API_BASE_URL;
|
||||
|
||||
const ITEM_COUNT = 100;
|
||||
|
||||
const payload = JSON.stringify({
|
||||
items: Array.from({ length: ITEM_COUNT }, (_, i) => ({
|
||||
id: i,
|
||||
firstName: `First${i}`,
|
||||
lastName: `Last${i}`,
|
||||
age: 20 + (i % 50),
|
||||
})),
|
||||
});
|
||||
|
||||
export default function () {
|
||||
const res = http.post(`${apiBaseUrl}/webhook/sub-workflow-benchmark`, payload, {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
if (res.status !== 200) {
|
||||
console.error(
|
||||
`Invalid response. Received status ${res.status}. Body: ${JSON.stringify(res.body)}`,
|
||||
);
|
||||
}
|
||||
|
||||
check(res, {
|
||||
'is status 200': (r) => r.status === 200,
|
||||
'has items processed by the sub-workflow': (r) => {
|
||||
if (r.status !== 200) return false;
|
||||
|
||||
try {
|
||||
const body = JSON.parse(r.body);
|
||||
return (
|
||||
Array.isArray(body) &&
|
||||
body.length === ITEM_COUNT &&
|
||||
body.every((item) => item.processedBySubWorkflow === true)
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error parsing response body: ', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
+2
-1
@@ -70,8 +70,9 @@ export class AuthenticatedN8nApiClient extends N8nApiClient {
|
||||
});
|
||||
}
|
||||
|
||||
async delete<T>(endpoint: string) {
|
||||
async delete<T>(endpoint: string, init?: Omit<AxiosRequestConfig, 'url' | 'method'>) {
|
||||
return await this.authenticatedRequest<T>(endpoint, {
|
||||
...init,
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ export type Workflow = {
|
||||
id: string;
|
||||
name: string;
|
||||
versionId: string;
|
||||
isArchived?: boolean;
|
||||
tags?: string[];
|
||||
settings?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
+28
-1
@@ -32,7 +32,34 @@ export class WorkflowApiClient {
|
||||
await this.apiClient.post(`/workflows/${workflowId}/archive`, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an archived workflow. n8n removes the triggers of an unpublished
|
||||
* workflow in the background and rejects the delete with 409 until it is done,
|
||||
* so we retry until the timeout.
|
||||
*/
|
||||
async deleteWorkflow(workflowId: Workflow['id']): Promise<void> {
|
||||
await this.apiClient.delete(`/workflows/${workflowId}`);
|
||||
const RETRY_INTERVAL_MS = 500;
|
||||
const TIMEOUT_MS = 30_000;
|
||||
const deadline = Date.now() + TIMEOUT_MS;
|
||||
|
||||
while (true) {
|
||||
const response = await this.apiClient.delete<{ message?: string }>(
|
||||
`/workflows/${workflowId}`,
|
||||
{
|
||||
timeout: Math.max(deadline - Date.now(), 1),
|
||||
validateStatus: (status) => (status >= 200 && status < 300) || status === 409,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.status !== 409) return;
|
||||
|
||||
if (Date.now() >= deadline) {
|
||||
throw new Error(
|
||||
`Could not delete workflow ${workflowId} within ${TIMEOUT_MS / 1000} seconds: ${response.data.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
await this.apiClient.delay(RETRY_INTERVAL_MS);
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
-6
@@ -11,6 +11,8 @@ import { ProjectApiClient } from '@/n8n-api-client/project-api-client';
|
||||
import { WorkflowApiClient } from '@/n8n-api-client/workflows-api-client';
|
||||
import type { LoadableScenarioData } from '@/scenario/scenario-data-loader';
|
||||
|
||||
const BENCHMARK_NAME_PREFIX = '[BENCHMARK]';
|
||||
|
||||
/**
|
||||
* Imports scenario data into an n8n instance
|
||||
*/
|
||||
@@ -119,13 +121,18 @@ export class ScenarioDataImporter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports a single workflow into n8n removing any existing workflows with the same name
|
||||
* Imports a single workflow into n8n removing any existing benchmark workflows with the same name or id.
|
||||
* The workflow keeps the id from its file, so other workflows can reference it (e.g. as a sub-workflow).
|
||||
* Fails if a workflow that the benchmark did not create uses the same id
|
||||
*/
|
||||
private async importWorkflow(opts: { existingWorkflows: Workflow[]; workflow: Workflow }) {
|
||||
const existingWorkflows = this.findExistingWorkflows(opts.existingWorkflows, opts.workflow);
|
||||
if (existingWorkflows.length > 0) {
|
||||
for (const toDelete of existingWorkflows) {
|
||||
await this.workflowApiClient.archiveWorkflow(toDelete.id);
|
||||
// The workflow is already archived when an earlier run failed to delete it
|
||||
if (!toDelete.isArchived) {
|
||||
await this.workflowApiClient.archiveWorkflow(toDelete.id);
|
||||
}
|
||||
await this.workflowApiClient.deleteWorkflow(toDelete.id);
|
||||
}
|
||||
}
|
||||
@@ -158,20 +165,36 @@ export class ScenarioDataImporter {
|
||||
): Workflow[] {
|
||||
const benchmarkWorkflowName = this.getBenchmarkWorkflowName(workflowToImport);
|
||||
|
||||
const workflowWithSameId = existingWorkflows.find(
|
||||
(existingWorkflow) =>
|
||||
workflowToImport.id !== undefined && existingWorkflow.id === workflowToImport.id,
|
||||
);
|
||||
// Only delete workflows that the benchmark created
|
||||
if (workflowWithSameId && !this.isBenchmarkWorkflow(workflowWithSameId)) {
|
||||
throw new Error(
|
||||
`Cannot import workflow "${workflowToImport.name}": workflow "${workflowWithSameId.name}" already uses the id ${workflowToImport.id}`,
|
||||
);
|
||||
}
|
||||
|
||||
return existingWorkflows.filter(
|
||||
(existingWorkflow) => existingWorkflow.name === benchmarkWorkflowName,
|
||||
(existingWorkflow) =>
|
||||
existingWorkflow.name === benchmarkWorkflowName || existingWorkflow === workflowWithSameId,
|
||||
);
|
||||
}
|
||||
|
||||
private getBenchmarkCredentialName(credential: Credential) {
|
||||
return `[BENCHMARK] ${credential.name}`;
|
||||
return `${BENCHMARK_NAME_PREFIX} ${credential.name}`;
|
||||
}
|
||||
|
||||
private getBenchmarkWorkflowName(workflow: Workflow) {
|
||||
return `[BENCHMARK] ${workflow.name}`;
|
||||
return `${BENCHMARK_NAME_PREFIX} ${workflow.name}`;
|
||||
}
|
||||
|
||||
private isBenchmarkWorkflow(workflow: Workflow) {
|
||||
return workflow.name.startsWith(`${BENCHMARK_NAME_PREFIX} `);
|
||||
}
|
||||
|
||||
private getBenchmarkDataTableName(dataTable: DataTable) {
|
||||
return `[BENCHMARK] ${dataTable.name}`;
|
||||
return `${BENCHMARK_NAME_PREFIX} ${dataTable.name}`;
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -5,7 +5,9 @@
|
||||
"boundaries": {
|
||||
// `~icons/*` is a vite virtual module injected by unplugin-icons (design-system
|
||||
// icon barrel), not a real package — declare it so boundaries doesn't flag it.
|
||||
"implicitDependencies": ["~icons"]
|
||||
// `k6` modules are provided by the k6 runtime that runs the benchmark scenario
|
||||
// scripts. npm only has their types (`@types/k6`).
|
||||
"implicitDependencies": ["~icons", "k6"]
|
||||
},
|
||||
"futureFlags": {
|
||||
// `--affected` uses task `inputs`, so it selects a task that reads a file from another package.
|
||||
|
||||
Reference in new issue
Block a user