diff --git a/src/index.ts b/src/index.ts index eef0ecc..2033b2b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ import { inspect, types } from 'node:util' import assert from 'node:assert' import { performance } from 'node:perf_hooks' import { readFileSync } from 'node:fs' -import { amount as physicalCpuCount } from './physicalCpuCount' +import { availableParallelism } from 'node:os' import { type ReadyMessage, type RequestMessage, @@ -50,7 +50,7 @@ declare global { } } -const cpuCount: number = physicalCpuCount +const cpuCount: number = availableParallelism() interface AbortSignalEventTargetAddOptions { once: boolean diff --git a/src/physicalCpuCount.ts b/src/physicalCpuCount.ts deleted file mode 100644 index 5fe3bc4..0000000 --- a/src/physicalCpuCount.ts +++ /dev/null @@ -1,52 +0,0 @@ -// https://www.npmjs.com/package/physical-cpu-count in ESM -import os from 'node:os' -import childProcess from 'node:child_process' - -function exec(command: string) { - const output = childProcess.execSync(command, { - encoding: 'utf8', - stdio: [null, null, null], - }) - return output -} - -let amount: number - -try { - const platform = os.platform() - - if (platform === 'linux') { - const output1 = exec( - 'cat /proc/cpuinfo | grep "physical id" | sort |uniq | wc -l' - ) // physical cpu number - const output2 = exec( - 'cat /proc/cpuinfo | grep "core id" | sort | uniq | wc -l' - ) // physical cpu core number in each cpu - - const physicalCpuAmount = parseInt(output1.trim(), 10) - const physicalCoreAmount = parseInt(output2.trim(), 10) - - amount = physicalCpuAmount * physicalCoreAmount - } else if (platform === 'darwin') { - const output = exec('sysctl -n hw.physicalcpu_max') - amount = parseInt(output.trim(), 10) - } else if (platform === 'win32') { - // windows takes too long, so let's drop the support - throw new Error() - } else { - const cores = os.cpus().filter(function (cpu, index) { - const hasHyperthreading = cpu.model.includes('Intel') - const isOdd = index % 2 === 1 - return !hasHyperthreading || isOdd - }) - amount = cores.length - } -} catch { - amount = os.cpus().length -} - -if (amount === 0) { - amount = os.cpus().length -} - -export { amount } diff --git a/test/cpu-count.test.ts b/test/cpu-count.test.ts deleted file mode 100644 index 62f53e6..0000000 --- a/test/cpu-count.test.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { expect, type Mock, test, vi } from 'vitest' -import os from 'node:os' -import childProcess from 'node:child_process' - -const platform = os.platform as Mock -const cpus = os.cpus as Mock -const execSync = childProcess.execSync as Mock - -beforeEach(() => { - vi.resetModules() -}) - -describe('Linux', () => { - beforeEach(() => { - platform.mockImplementationOnce(() => 'linux') - }) - - test('returns cpus from cpuinfo', async () => { - execSync - .mockImplementationOnce(() => '20') - .mockImplementationOnce(() => '40') - - const { amount } = await import('../src/physicalCpuCount') - - expect(amount).toBe(800) - }) - - test('returns cpus from node:os when cpuinfo returns 0', async () => { - execSync.mockImplementationOnce(() => '0').mockImplementationOnce(() => '0') - cpus.mockImplementation(() => Array(105)) - - const { amount } = await import('../src/physicalCpuCount') - - expect(amount).toBe(105) - }) -}) - -describe('MacOS', () => { - test('returns cpus from node:os', async () => { - platform.mockImplementationOnce(() => 'darwin') - execSync.mockImplementationOnce(() => '123.32') - - const { amount } = await import('../src/physicalCpuCount') - - expect(amount).toBe(123) - }) -}) - -describe('Windows', () => { - test('returns cpus from node:os', async () => { - platform.mockImplementationOnce(() => 'win32') - cpus.mockImplementation(() => Array(101)) - - const { amount } = await import('../src/physicalCpuCount') - - expect(amount).toBe(101) - }) -}) - -describe('Unknown OS', () => { - test('returns cpus resolved from node:os', async () => { - platform.mockImplementationOnce(() => 'custom') - cpus.mockImplementation(() => [ - { model: 'Intel 1' }, - { model: 'AMD 1' }, - { model: 'Intel 2' }, - { model: 'Intel 3' }, - { model: 'AMD 2' }, - ]) - - const { amount } = await import('../src/physicalCpuCount') - - expect(amount).toBe(3) - }) -}) - -vi.mock(import('node:os'), async (importOriginal) => { - const original = await importOriginal() - - return { - ...original, - default: { - ...original.default, - platform: vi.fn().mockImplementation(original.platform), - cpus: vi.fn().mockImplementation(original.cpus), - }, - } -}) - -vi.mock(import('node:child_process'), async (importOriginal) => { - const original = await importOriginal() - - return { - ...original, - default: { - ...original.default, - execSync: vi.fn().mockImplementation(original.execSync), - }, - } -}) diff --git a/test/options.test.ts b/test/options.test.ts index 7326a11..e4991d2 100644 --- a/test/options.test.ts +++ b/test/options.test.ts @@ -51,7 +51,7 @@ vi.mock(import('node:os'), async (importOriginal) => { const original = await importOriginal() return { ...original, - default: { ...original.default, cpus: () => Array(cpuCount) }, + availableParallelism: () => cpuCount, } })