n8n/packages/core/bin/generate-known
कारतोफ्फेलस्क्रिप्ट™ 76c04815f7
fix(core): Reduce logging overhead for levels that do not output (#7479)
all current logging calls execute `callsites()` to figure out what code
tried to log. This happens even for logging methods that aren't supposed
to create any output. Under moderate load, this can take up quite a lot
of resources. This PR changes the logger to make all ignorable logging
methods a No-Op.

In a small benchmark with a simple webhook, with log-level set to
`warn`, and using `ab -c 50 -n 500
http://localhost:5678/webhook/testing`, these were the response times:

### Before

![Before](https://github.com/n8n-io/n8n/assets/196144/01680fd9-3d2a-4f7f-bb1c-5b03bd7d5bc3)

### After

![After](https://github.com/n8n-io/n8n/assets/196144/ccacb20a-48ca-455a-a8cb-098c9c0e352e)
2023-10-20 18:26:33 +02:00

67 lines
1.9 KiB
JavaScript
Executable file

#!/usr/bin/env node
const path = require('path');
const glob = require('fast-glob');
const { LoggerProxy } = require('n8n-workflow');
const { packageDir, writeJSON } = require('./common');
const { loadClassInIsolation } = require('../dist/ClassLoader');
LoggerProxy.init(console);
const loadClass = (sourcePath) => {
try {
const [className] = path.parse(sourcePath).name.split('.');
const filePath = path.resolve(packageDir, sourcePath);
const instance = loadClassInIsolation(filePath, className);
return { instance, sourcePath, className };
} catch (e) {
LoggerProxy.warn(`Failed to load ${sourcePath}: ${e.message}`);
}
};
const nodesToTestWith = {};
const generate = async (kind) => {
const data = glob
.sync(`dist/${kind}/**/*.${kind === 'nodes' ? 'node' : kind}.js`, {
cwd: packageDir,
})
.map(loadClass)
.filter((data) => !!data)
.reduce((obj, { className, sourcePath, instance }) => {
const name = kind === 'nodes' ? instance.description.name : instance.name;
if (!/[vV]\d.node\.js$/.test(sourcePath)) {
if (name in obj) console.error('already loaded', kind, name, sourcePath);
else obj[name] = { className, sourcePath };
}
if (kind === 'credentials' && Array.isArray(instance.extends)) {
obj[name].extends = instance.extends;
}
if (kind === 'nodes') {
const { credentials } = instance.description;
if (credentials && credentials.length) {
for (const credential of credentials) {
nodesToTestWith[credential.name] = nodesToTestWith[credential.name] || [];
nodesToTestWith[credential.name].push(name);
}
}
} else {
if (name in nodesToTestWith) {
obj[name].nodesToTestWith = nodesToTestWith[name];
}
}
return obj;
}, {});
LoggerProxy.info(`Detected ${Object.keys(data).length} ${kind}`);
await writeJSON(`known/${kind}.json`, data);
return data;
};
(async () => {
await generate('nodes');
await generate('credentials');
})();