Skip to content

Commit

Permalink
fix(config): physical cpu core counting (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Nov 9, 2022
1 parent 1178139 commit 4b97e80
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"clearMocks": true,
"collectCoverageFrom": [
"src/**/*.js",
"!src/app.js"
"!src/app.js",
"!src/utils/core-count/index.js"
],
"coverageReporters": [
"text",
Expand Down Expand Up @@ -117,7 +118,6 @@
"mammoth": "^1.5.1",
"node-poppler": "^6.0.3",
"node-unrtf": "^3.0.3",
"physical-cpu-count": "^2.0.0",
"pino": "^8.7.0",
"redoc": "^2.0.0",
"secure-json-parse": "^2.5.0",
Expand Down
4 changes: 2 additions & 2 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const S = require("fluent-json-schema");
const fs = require("fs/promises");
const path = require("upath");
const pino = require("pino");
const physicalCpuCount = require("physical-cpu-count");
const rotatingLogStream = require("file-stream-rotator");
const secJSON = require("secure-json-parse");

const coreCount = require("../utils/core-count");
const { description, license, version } = require("../../package.json");

/**
Expand Down Expand Up @@ -290,7 +290,7 @@ async function getConfig() {
enabled: env.OCR_ENABLED === true,
languages: env.OCR_LANGUAGES || "eng",
// Use number of physical CPU cores available if ENV variable not specified
workers: env.OCR_WORKERS || physicalCpuCount,
workers: env.OCR_WORKERS || coreCount(),
},
unrtf: {
binPath: env.UNRTF_BINARY_PATH
Expand Down
44 changes: 44 additions & 0 deletions src/utils/core-count/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable security/detect-child-process */
const { cpus, EOL, platform } = require("os");
const { execSync } = require("child_process");

/**
* @author Frazer Smith
* @description Count number of physical CPU cores a system has,
* taking into account Intel Hyper-Threading.
* @see https://github.com/nodejs/node/issues/7730
* @returns {number} Number of physical cores the system has.
*/
function coreCount() {
const config = { encoding: "utf8" };

switch (platform()) {
case "darwin":
return parseInt(
execSync("sysctl -n hw.physicalcpu_max", config).trim(),
10
);
case "linux":
return parseInt(
execSync(
'lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l',
config
).trim(),
10
);
case "win32":
return execSync("WMIC CPU Get NumberOfCores", config)
.split(EOL)
.map((line) => parseInt(line, 10))
.filter((value) => !Number.isNaN(Number(value)))
.reduce((sum, number) => sum + number, 0);
default:
return cpus().filter((cpu, index) => {
const hasHyperthreading = cpu.model.includes("Intel");
const isOdd = index % 2 === 1;
return !hasHyperthreading || isOdd;
}).length;
}
}

module.exports = coreCount;
8 changes: 8 additions & 0 deletions src/utils/core-count/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const util = require(".");

describe("Core-Count Util", () => {
test("Should return count of physical cores", () => {
const response = util();
expect(response).toEqual(expect.any(Number));
});
});

0 comments on commit 4b97e80

Please sign in to comment.