From 57bb3a0df36f012632b188e186a89177b405d1ac Mon Sep 17 00:00:00 2001 From: VandeurenGlenn <8685280+VandeurenGlenn@users.noreply.github.com> Date: Sun, 11 Aug 2024 19:50:06 +0200 Subject: [PATCH] Add advanced flag & scanSpeed option --- README.md | 10 +++++++ src/main.ts | 60 +++++++++++++++++++++++++++++++++++++++- src/stages/scan-files.ts | 3 +- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de1e0c5..d259df6 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,16 @@ The interactive CLI can guide you through the following steps: npx esperf ``` +## Advanced Usage + +The interactive CLI will show more steps: + +- Set scan speed + +```sh +npx esperf --advanced +``` + ## License MIT diff --git a/src/main.ts b/src/main.ts index 2e9c819..273c353 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,6 +8,28 @@ import {scanFiles} from './stages/scan-files.js'; import {fixFiles} from './stages/fix-files.js'; import {traverseFiles} from './stages/traverse-files.js'; import {scanDependencies} from './stages/scan-dependencies.js'; +import {availableParallelism} from 'node:os'; + +let advanced = false; + +for (let i = 0; i < process.argv.length; ++i) { + if (process.argv[i] === '--advanced') advanced = true; +} + +function getWantedThreads(scanSpeed: string): number { + const threads = availableParallelism(); + switch (scanSpeed) { + case 'slow': + return threads * 0.25; + case 'medium': + return threads * 0.5; + case 'fast': + return threads * 0.75; + case 'fastest': + return threads; + } + return 1; +} const availableManifests: Record = { native: modReplacements.nativeReplacements, @@ -118,7 +140,41 @@ async function runModuleReplacements(): Promise { cl.confirm({ message: 'Automatically uninstall packages?', initialValue: false - }) + }), + scanSpeed: () => + advanced + ? cl.select({ + message: 'Preferred scan speed', + options: [ + { + value: 'fastest', + label: 'Fastest', + hint: 'uses all the threads, pushes cpu to 100%' + }, + { + value: 'fast', + label: 'Fast', + hint: '75% of available threads' + }, + { + value: 'medium', + label: 'Medium', + hint: '50% of available threads' + }, + { + value: 'slow', + label: 'Slow', + hint: '25% of available threads' + }, + { + value: 'slowest', + label: 'Slowest', + hint: 'disables parallelism, 1 thread' + } + ], + initialValue: 'medium' + }) + : Promise.resolve('medium') }, { onCancel: () => { @@ -198,10 +254,12 @@ async function runModuleReplacements(): Promise { try { const files = await traverseFiles(options.filesDir); + const threads = getWantedThreads(options.scanSpeed); const scanFilesResult = await scanFiles( files, manifestReplacements, + threads, scanSpinner ); diff --git a/src/stages/scan-files.ts b/src/stages/scan-files.ts index f3d2a94..e4ab066 100644 --- a/src/stages/scan-files.ts +++ b/src/stages/scan-files.ts @@ -13,6 +13,7 @@ const available = availableParallelism(); export function scanFiles( files: string[], replacements: modReplacements.ModuleReplacement[], + threads: number, spinner: ReturnType ): Promise { return new Promise((resolve, reject) => { @@ -21,7 +22,7 @@ export function scanFiles( const filesLength = files.length; const results: FileReplacement[] = []; - for (const file of files.splice(0, available)) { + for (const file of files.splice(0, threads)) { const worker = new Worker(`${__dirname}/workers/scan-file.js`); // todo, what todo with the errors? worker.on('error', (error) => reject(error.message));