From dc1c106671fe4a2a83af806c44e6dc3dbe1fea87 Mon Sep 17 00:00:00 2001 From: Swizec Teller Date: Thu, 11 Oct 2018 00:02:08 -0700 Subject: [PATCH] Burn CPU in background, keep measuring temp --- eat-cpu.js | 12 ++++++++++++ index.js | 16 ++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 eat-cpu.js diff --git a/eat-cpu.js b/eat-cpu.js new file mode 100644 index 0000000..0dadecd --- /dev/null +++ b/eat-cpu.js @@ -0,0 +1,12 @@ +// Borrowed from https://gist.github.com/sqren/5083d73f184acae0c5b7 + +function mySlowFunction(baseNumber) { + var result = 0; + for (var i = Math.pow(baseNumber, 10); i >= 0; i--) { + result += Math.atan(i) * Math.tan(i); + }; + return result; +} + +// module.exports = mySlowFunction; +mySlowFunction(8); \ No newline at end of file diff --git a/index.js b/index.js index 8c9e845..fdbb6ad 100644 --- a/index.js +++ b/index.js @@ -2,8 +2,7 @@ const si = require("systeminformation"); const mri = require("mri"); const sll = require("single-line-log").stdout; const kleur = require("kleur"); -const isPrime = require("prime-number"); -const eatCPU = require("./eat-cpu"); +const fork = require("child_process").fork; async function readTemp() { const temp = await si.cpuTemperature(); @@ -12,12 +11,17 @@ async function readTemp() { async function thawMyCarrots(targetTemp) { let temp = await readTemp(), - n = 2; + children = []; while (1) { if (temp.main < targetTemp) { - eatCPU(n); - n = n + 1; + if (children.length < 10) { + children.push(fork("./eat-cpu")); + } + } else if (temp.main > targetTemp) { + if (children.length > 0) { + children.pop().kill(); + } } temp = await readTemp(); @@ -26,7 +30,7 @@ async function thawMyCarrots(targetTemp) { kleur.bold.red(temp.main.toFixed(2)), "\nCores at", kleur.bold.gray(temp.cores), - n + `\nChildren: ${children.length}` ); } }