From 4db82a91b4aaa68199c86e2947b980ab74925fb3 Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:04:53 +0300 Subject: [PATCH] refactor(benchmark): Expand js code node benchmark (#11052) --- .../js-code-node-once-for-each.manifest.json | 7 -- .../js-code-node.json} | 103 +++++++++--------- .../js-code-node/js-code-node.manifest.json | 7 ++ .../js-code-node.script.js} | 2 +- 4 files changed, 59 insertions(+), 60 deletions(-) delete mode 100644 packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.manifest.json rename packages/@n8n/benchmark/scenarios/{js-code-node-once-for-each/js-code-node-once-for-each.json => js-code-node/js-code-node.json} (58%) create mode 100644 packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.manifest.json rename packages/@n8n/benchmark/scenarios/{js-code-node-once-for-each/js-code-node-once-for-each.script.js => js-code-node/js-code-node.script.js} (88%) diff --git a/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.manifest.json b/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.manifest.json deleted file mode 100644 index 1d768f706e..0000000000 --- a/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.manifest.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "$schema": "../scenario.schema.json", - "name": "CodeNodeJsOnceForEach", - "description": "A JS Code Node that runs once for each item and adds, modifies and removes properties. The data of 5 items is generated using DebugHelper Node, and returned with RespondToWebhook Node.", - "scenarioData": { "workflowFiles": ["js-code-node-once-for-each.json"] }, - "scriptPath": "js-code-node-once-for-each.script.js" -} diff --git a/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.json b/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.json similarity index 58% rename from packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.json rename to packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.json index 7b89ffde96..d6f30ac5ea 100644 --- a/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.json +++ b/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.json @@ -1,9 +1,31 @@ { "createdAt": "2024-08-06T12:19:51.268Z", "updatedAt": "2024-08-06T12:20:45.000Z", - "name": "JS Code Node Once For Each", + "name": "JS Code Node", "active": true, "nodes": [ + { + "parameters": { + "respondWith": "allIncomingItems", + "options": {} + }, + "type": "n8n-nodes-base.respondToWebhook", + "typeVersion": 1.1, + "position": [1280, 460], + "id": "0067e317-09b8-478a-8c50-e19b4c9e294c", + "name": "Respond to Webhook" + }, + { + "parameters": { + "mode": "runOnceForEachItem", + "jsCode": "// Add new field\n$input.item.json.age = 10 + Math.floor(Math.random() * 30);\n// Mutate existing field\n$input.item.json.password = $input.item.json.password.split('').map(() => '*').join(\"\")\n// Remove field\ndelete $input.item.json.lastname\n// New object field\nconst emailParts = $input.item.json.email.split(\"@\")\n$input.item.json.emailData = {\n user: emailParts[0],\n domain: emailParts[1]\n}\n\nreturn $input.item;" + }, + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [1040, 460], + "id": "56d751c0-0d30-43c3-89fa-bebf3a9d436f", + "name": "OnceForEachItemJSCode" + }, { "parameters": { "httpMethod": "POST", @@ -13,68 +35,23 @@ }, "type": "n8n-nodes-base.webhook", "typeVersion": 2, - "position": [0, 0], - "id": "849350b3-4212-4416-a462-1cf331157d37", + "position": [580, 460], + "id": "417d749d-156c-4ffe-86ea-336f702dc5da", "name": "Webhook", "webhookId": "34ca1895-ccf4-4a4a-8bb8-a042f5edb567" }, { "parameters": { - "respondWith": "allIncomingItems", - "options": {} - }, - "type": "n8n-nodes-base.respondToWebhook", - "typeVersion": 1.1, - "position": [660, 0], - "id": "f0660aa1-8a65-490f-b5cd-f8d134070c13", - "name": "Respond to Webhook" - }, - { - "parameters": { - "category": "randomData", - "randomDataCount": 5 - }, - "type": "n8n-nodes-base.debugHelper", - "typeVersion": 1, - "position": [220, 0], - "id": "50f1efe8-bd2d-4061-9f51-b38c0e3daeb2", - "name": "DebugHelper" - }, - { - "parameters": { - "mode": "runOnceForEachItem", - "jsCode": "// Add new field\n$input.item.json.age = 10 + Math.floor(Math.random() * 30);\n// Mutate existing field\n$input.item.json.password = $input.item.json.password.split('').map(() => '*').join(\"\")\n// Remove field\ndelete $input.item.json.lastname\n// New object field\nconst emailParts = $input.item.json.email.split(\"@\")\n$input.item.json.emailData = {\n user: emailParts[0],\n domain: emailParts[1]\n}\n\nreturn $input.item;" + "jsCode": "const digits = '0123456789';\nconst uppercaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst lowercaseLetters = uppercaseLetters.toLowerCase();\nconst alphabet = [digits, uppercaseLetters, lowercaseLetters].join('').split('')\n\nconst randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;\nconst randomItem = (arr) => arr.at(randomInt(0, arr.length - 1))\nconst randomString = (len) => Array.from({ length: len }).map(() => randomItem(alphabet)).join('')\n\nconst randomUid = () => [8,4,4,4,8].map(len => randomString(len)).join(\"-\")\nconst randomEmail = () => `${randomString(8)}@${randomString(10)}.com`\n\nconst randomPerson = () => ({\n uid: randomUid(),\n email: randomEmail(),\n firstname: randomString(5),\n lastname: randomString(12),\n password: randomString(10)\n})\n\nreturn Array.from({ length: 100 }).map(() => ({\n json: randomPerson()\n}))" }, + "id": "c30db155-73ca-48b9-8860-c3fe7a0926fb", + "name": "Code", "type": "n8n-nodes-base.code", "typeVersion": 2, - "position": [440, 0], - "id": "f9f2f865-e228-403d-8e47-72308359e207", - "name": "OnceForEachItemJSCode" + "position": [820, 460] } ], "connections": { - "Webhook": { - "main": [ - [ - { - "node": "DebugHelper", - "type": "main", - "index": 0 - } - ] - ] - }, - "DebugHelper": { - "main": [ - [ - { - "node": "OnceForEachItemJSCode", - "type": "main", - "index": 0 - } - ] - ] - }, "OnceForEachItemJSCode": { "main": [ [ @@ -85,6 +62,28 @@ } ] ] + }, + "Webhook": { + "main": [ + [ + { + "node": "Code", + "type": "main", + "index": 0 + } + ] + ] + }, + "Code": { + "main": [ + [ + { + "node": "OnceForEachItemJSCode", + "type": "main", + "index": 0 + } + ] + ] } }, "settings": { "executionOrder": "v1" }, diff --git a/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.manifest.json b/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.manifest.json new file mode 100644 index 0000000000..8b0165baf7 --- /dev/null +++ b/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.manifest.json @@ -0,0 +1,7 @@ +{ + "$schema": "../scenario.schema.json", + "name": "CodeNodeJs", + "description": "A JS Code Node that first generates 100 items and then runs once for each item and adds, modifies and removes properties. The data returned with RespondToWebhook Node.", + "scenarioData": { "workflowFiles": ["js-code-node.json"] }, + "scriptPath": "js-code-node.script.js" +} diff --git a/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.script.js b/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.script.js similarity index 88% rename from packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.script.js rename to packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.script.js index 11e8e87ac3..74cef4f441 100644 --- a/packages/@n8n/benchmark/scenarios/js-code-node-once-for-each/js-code-node-once-for-each.script.js +++ b/packages/@n8n/benchmark/scenarios/js-code-node/js-code-node.script.js @@ -12,7 +12,7 @@ export default function () { try { const body = JSON.parse(r.body); - return Array.isArray(body) ? body.length === 5 : false; + return Array.isArray(body) ? body.length === 100 : false; } catch (error) { console.error('Error parsing response body: ', error); return false;