Skip to content

Commit

Permalink
fix: prevent 100% CPU usage in large-scale test suites (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel authored Jul 26, 2024
1 parent 2c002c0 commit 382631b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/polyfills/cpus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
availableParallelism as nodeAvailableParallelism,
cpus,
} from 'node:os';

export const availableParallelism = (): number =>
typeof nodeAvailableParallelism === 'function'
? Math.floor(nodeAvailableParallelism() / 2)
: (cpus()?.length ?? 1);
3 changes: 2 additions & 1 deletion src/services/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { format } from './format.js';
import { runTestFile } from './run-test-file.js';
import { isQuiet } from '../parsers/output.js';
import { results } from '../configs/poku.js';
import { availableParallelism } from '../polyfills/cpus.js';

const cwd = processCWD();

Expand Down Expand Up @@ -101,7 +102,7 @@ export const runTestsParallel = async (
? [sanitizePath(dir)]
: await listFiles(testDir, configs);
const filesByConcurrency: string[][] = [];
const concurrencyLimit = configs?.concurrency || 0;
const concurrencyLimit = configs?.concurrency ?? availableParallelism();
const concurrencyResults: (boolean | undefined)[][] = [];
const showLogs = !isQuiet(configs);

Expand Down

0 comments on commit 382631b

Please sign in to comment.