Last active
March 2, 2023 03:53
-
-
Save sakkas-zendesk/38f6892fdfdbec5abbe1b2651ea0ddc5 to your computer and use it in GitHub Desktop.
simple cpu benchmark for nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "cpu-test", | |
"lockfileVersion": 3, | |
"requires": true, | |
"packages": { | |
"": { | |
"dependencies": { | |
"cpu-benchmark": "^1.0.1" | |
} | |
}, | |
"node_modules/cpu-benchmark": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/cpu-benchmark/-/cpu-benchmark-1.0.1.tgz", | |
"integrity": "sha512-b6FmkWdWvwKySXIdMFjXqh9xkWl5MCDq5EZv26/PxzlSoY+/pugsA8wx2rUJ4/w7MhtU/GqmKmifr+uufgMFnw==" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"cpu-benchmark": "^1.0.1" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { fib, dist } = require('cpu-benchmark') | |
const repetition = 10 | |
const fibCount = 41 | |
const distanceTimeLimit = 6 | |
console.log(`Benchmark parameters | |
repetition: ${repetition} | |
fibonacci limit: ${fibCount} | |
distance time limit: ${distanceTimeLimit} | |
`) | |
function runFunctionNTimesAndGetAverage(func, funcParam) { | |
let sum = 0; | |
for (let i = 0; i < repetition; i++) { | |
const result = func(funcParam); | |
sum += result; | |
} | |
return sum / repetition; | |
} | |
const duration = runFunctionNTimesAndGetAverage(fib,fibCount) | |
console.log(`Avg Duration it takes to calculate fib(${fibCount}): ${duration/1000}s`) | |
const ops = runFunctionNTimesAndGetAverage(dist, distanceTimeLimit * 1000) | |
console.log(`Avg Number of ${ops} operations done in ${distanceTimeLimit} s for calculating distances`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment