Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure CPU performance via JS

Tags:

javascript

A webapp has as a central component a relatively heavy algorithm that handles geometric operations.

There are 2 solutions to make the whole thing accessible from both high-end machines and relatively slower mobile devices.

I can use RPC's (Remote Procedure Calls) if i detect that the user machine is ''slow'' or else if i detect that the user machine can handle it OK, then i provide to the web-app the script to handle it client side.

Now what would be a reliable way to detect the speed of the user machine?

I was thinking of providing a sample script as a test when the page loads and detect the time it took to execute that.

Any ideas?

like image 542
nicholaswmin Avatar asked Nov 03 '13 15:11

nicholaswmin


1 Answers

I wrote this quick script to get the cpu speed:

var _speedconstant = 1.15600e-8; //if speed=(c*a)/t, then constant=(s*t)/a and time=(a*c)/s
var d = new Date();
var amount = 150000000;
var estprocessor = 1.7; //average processor speed, in GHZ
console.log("JSBenchmark by Aaron Becker, running loop " + amount + " times.     Estimated time (for " + estprocessor + "ghz processor) is " + (Math.round(((_speedconstant * amount) / estprocessor) * 100) / 100) + "s");
for (var i = amount; i > 0; i--) {}
var newd = new Date();
var accnewd = Number(String(newd.getSeconds()) + "." + String(newd.getMilliseconds()));
var accd = Number(String(d.getSeconds()) + "." + String(d.getMilliseconds()));
var di = accnewd - accd;
//console.log(accnewd,accd,di);
if (d.getMinutes() != newd.getMinutes()) {
  di = (60 * (newd.getMinutes() - d.getMinutes())) + di
}
spd = ((_speedconstant * amount) / di);
console.log("Time: " + Math.round(di * 1000) / 1000 + "s, estimated speed: " + Math.round(spd * 1000) / 1000 + "GHZ");

Note that this depends on browser tabs, memory use, etc. but I found it pretty accurate if you only run it once, say at the loading of a page.

If you would like you can change the _speedconstant to change the speed, just calculate it with the equation (knowncpuspeed*knowntimetocomplete)/knowncycles. Hope you find this useful!

like image 162
Aaron Becker Avatar answered Nov 16 '22 13:11

Aaron Becker