UNPKG

nsolid-manager

Version:

A manager that sets up and runs N|Solid for you

62 lines (49 loc) 1.41 kB
/** * Tries to maintain high CPU usage while still releasing to event loop */ "use strict" const usage = require('usage') process.title = 'high-cpu' const MIN_CYCLES = 1000 const TARGET_CPU_USAGE = 90 // min number of consecutive times must hit or miss target before adjusting cycles const ADJUSTMENT_NEEDED_THRESHOLD = 3 let cycles = MIN_CYCLES let reachedTargetUsage = 0 let needsMoreCycles = 0 ;(function check() { usage.lookup(process.pid, (err, result) => { if (err) throw err if (result.cpu < TARGET_CPU_USAGE) { reachedTargetUsage = 0 needsMoreCycles++ } else { needsMoreCycles = 0 reachedTargetUsage++ } if (needsMoreCycles > ADJUSTMENT_NEEDED_THRESHOLD) { cycles *= 3 } if (reachedTargetUsage > ADJUSTMENT_NEEDED_THRESHOLD) { cycles /= 4 } cycles = Math.max(Math.floor(cycles), MIN_CYCLES) setTimeout(check) }) work(cycles) }()) /** * CPU 'work' */ function work(cycles) { let a = 0 for (let i=0; i<cycles; i++) { // bunch of random math a += Math.sqrt(Math.pow(Math.sqrt(Math.sin(i)), Math.sqrt(Math.sin(i))) / Math.pow(Math.sqrt(Math.sin(i)), Math.sqrt(Math.sin(i)))) + Math.sqrt(Math.pow(Math.sqrt(Math.sin(i)), Math.sqrt(Math.sin(i))) / Math.pow(Math.sqrt(Math.sin(i)), Math.sqrt(Math.sin(i)))) } return a }