Showing posts with label javascript benchmarker. Show all posts
Showing posts with label javascript benchmarker. Show all posts

Monday, 26 February 2018

javascript benchmarker

how do you know if your javascript is being impacted by something on the client? CPU, memory, disk io. I don't know. Maybe set a timeout for a second and then when the timeout is triggered see how close to a second it was? The more variance from a second the more impact something is having on the browser/js. Then just keep looping that timeout.

benchmarker is probably the wrong term, but life is hard.

Seems mostly the thing that effects JS is CPU. To test:
https://blogs.msdn.microsoft.com/vijaysk/2012/10/26/tools-to-simulate-cpu-memory-disk-load/
http://download.sysinternals.com/files/CPUSTRES.zip


function Benchmarker() {

    var times = []; // records the time at each interval check
    var interval = 1000; // the amount of time (in msec) to wait between time checking
    var warningVarianceThreshold = 100; // the amount of time (in msec) as a variance from the expected time that counts as a warning
    var alertVarianceThreshold = 150; // the amount of time (in msec) as a variance from the expected time that counts as an alert
    var warningCount = 0;
    var alertCount = 0;
    var stopFlag = false;

    var intervalCallback = null;    // a callback function called each time the interval occurs

    // this method will run at a given interval and record the time
    // will compare the current time to the last run time and establish a variance to the expected difference
    function checkInterval() {
        if (stopFlag) {
            return;
        }
        var d = new Date();
        var currentSeconds = d.getTime();
        if (times.length > 0) {
            var lastSeconds = times[times.length - 1];
            var timeChanged = currentSeconds - lastSeconds;
            //console.log(currentSeconds + "; " + timeChanged);
            var variance = Math.abs(timeChanged - interval);
            if (variance >= alertVarianceThreshold) {
                alertCount++;
            } else if (variance >= warningVarianceThreshold) {
                warningCount++;
            }
            if (typeof intervalCallback === "function") {
                var data = getData();
                data = $.extend(data, { lastVariance: variance });
                intervalCallback(data);
            }
        } else {
            //console.log(currentSeconds);
        }
        times.push(currentSeconds);
        setTimeout(function () { checkInterval(); }, interval);
    }

    this.getData = function() {
        var data = { interval: interval, intervalCount:times.length, warningVarianceThreshold: warningVarianceThreshold, alertVarianceThreshold: alertVarianceThreshold, warningCount: warningCount, alertCount: alertCount };
        return data;
    }

    this.start = function () {
        checkInterval();
    }
    this.stop = function () {
        stopFlag = true;
    }

    this.getAlertCount = function () {
        return alertCount;
    }
    this.getWarningCount = function () {
        return warningCount;
    }
    this.setIntervalCallback = function(callback) {
        intervalCallback = callback;
    }
}