summaryrefslogtreecommitdiffstats
path: root/PerformanceTests/Parser/resources/runner.js
blob: 3fc0b4f3ed1284f13375bc1f8c8bfd9a207f9a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function log(text) {
    document.getElementById("log").innerHTML += text + "\n";
    window.scrollTo(0, document.body.height);
}

function loadFile(path) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", path, false);
    xhr.send(null);
    return xhr.responseText;
}

var runCount = -1;
var runFunction = function() {};
var completedRuns = -1; // Discard the any runs < 0.
var times = [];

function computeAverage(values) {
    var sum = 0;
    for (var i = 0; i < values.length; i++)
        sum += values[i];
    return sum / values.length;
}

function computeMax(values) {
    var max = values.length ? values[0] : 0;
    for (var i = 1; i < values.length; i++) {
        if (max < values[i])
            max = values[i];
    }
    return max;
}

function computeMedian(values) {
    values.sort(function(a, b) { return a - b; });
    var len = values.length;
    if (len % 2)
        return values[(len-1)/2];
    return (values[len/2-1] + values[len/2]) / 2;
}

function computeMin(values) {
    var min = values.length ? values[0] : 0;
    for (var i = 1; i < values.length; i++) {
        if (min > values[i])
            min = values[i];
    }
    return min;
}

function computeStdev(values) {
    var average = computeAverage(values);
    var sumOfSquaredDeviations = 0;
    for (var i = 0; i < values.length; ++i) {
        var deviation = values[i] - average;
        sumOfSquaredDeviations += deviation * deviation;
    }
    return Math.sqrt(sumOfSquaredDeviations / values.length);
}

function logStatistics(times) {
    log("");
    log("avg " + computeAverage(times));
    log("median " + computeMedian(times));
    log("stdev " + computeStdev(times));
    log("min " + computeMin(times));
    log("max " + computeMax(times));
}

function run() {
    var start = new Date();
    for (var i = 0; i < 10; ++i)
        window.runFunction();
    var time = new Date() - start;
    completedRuns++;
    if (completedRuns <= 0) {
        log("Ignoring warm-up run (" + time + ")");
    } else {
        times.push(time);
        log(time);
    }
    if (completedRuns < window.runCount) {
        window.setTimeout(run, 0);
    } else {
        logStatistics(times);
    }
}

function start(runCount, runFunction) {
    window.runCount = runCount;
    window.runFunction = runFunction;

    log("Running " + runCount + " times");
    run();
}