fix: readd cpu speed computation but only if necessary
[poolifier.git] / src / pools / utils.ts
index 7c1b7866e6d958687ba6dc2e2c29827706caab4f..724c4ab960c691e8f4a5d8661f9c442ff14d5b3d 100644 (file)
@@ -110,24 +110,29 @@ const estimatedCpuSpeed = (): number => {
   return Math.trunc(runs / duration / 1000) // in MHz
 }
 
-const estCpuSpeed = estimatedCpuSpeed()
-
-const getDefaultWorkerWeight = (estimatedCpuSpeed = estCpuSpeed): number => {
+const getDefaultWorkerWeight = (): number => {
+  const currentCpus = cpus()
+  let estCpuSpeed: number | undefined
+  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+  if (currentCpus.every(cpu => cpu.speed == null || cpu.speed === 0)) {
+    estCpuSpeed = estimatedCpuSpeed()
+  }
   let cpusCycleTimeWeight = 0
-  for (const cpu of cpus()) {
+  for (const cpu of currentCpus) {
     // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
     if (cpu.speed == null || cpu.speed === 0) {
       cpu.speed =
         // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
-        cpus().find(cpu => cpu.speed != null && cpu.speed !== 0)?.speed ??
-        estimatedCpuSpeed
+        currentCpus.find(cpu => cpu.speed != null && cpu.speed !== 0)?.speed ??
+        estCpuSpeed ??
+        2000
     }
     // CPU estimated cycle time
     const numberOfDigits = cpu.speed.toString().length - 1
     const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits))
     cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits)
   }
-  return Math.round(cpusCycleTimeWeight / cpus().length)
+  return Math.round(cpusCycleTimeWeight / currentCpus.length)
 }
 
 export const checkFilePath = (filePath: string | undefined): void => {