/*
 * Copyright (C) 2007 Apple Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

function analyzer(results, elapsed) 
{
    this.elapsed = elapsed;
    this.output = results;
    this.count = output.length;

    this.itemTotals = {};
    this.itemTotals.length = this.count;

    this.total = 0;
    this.categoryTotals = {};
    this.testTotalsByCategory = {};

    this.mean = 0;
    this.categoryMeans = {};
    this.testMeansByCategory = {};

    this.stdDev = 0;
    this.categoryStdDevs = {};
    this.testStdDevsByCategory = {};

    this.stdErr = 0;
    this.categoryStdErrs = {};
    this.testStdErrsByCategory = {};

    this.initialize = function() {
        this.itemTotals = { total: [] };

        for (var i = 0; i < categories.length; i++) {
            var category = categories[i];
            this.itemTotals[category] = [];
            this.categoryTotals[category] = 0;
            this.testTotalsByCategory[category] = {};
            this.categoryMeans[category] = 0;
            this.testMeansByCategory[category] = {};
            this.categoryStdDevs[category] = 0;
            this.testStdDevsByCategory[category] = {};
            this.categoryStdErrs[category] = 0;
            this.testStdErrsByCategory[category] = {};
        }

        for (var i = 0; i < tests.length; i++) {
            var test = tests[i];
            this.itemTotals[test] = [];
            var category = test.replace(/-.*/, "");
            this.testTotalsByCategory[category][test] = 0;
            this.testMeansByCategory[category][test] = 0;
            this.testStdDevsByCategory[category][test] = 0;
            this.testStdErrsByCategory[category][test] = 0;
        }

        for (var i = 0; i < this.count; i++) {
            this.itemTotals["total"][i] = 0;
            for (var category in this.categoryTotals) {
                if (category != "toJSONString")
                {
                    this.itemTotals[category][i] = 0;
                    for (var test in this.testTotalsByCategory[category]) {
                        if (test != "toJSONString")
                        {
                            this.itemTotals[test][i] = 0;
                        }
                    }
                }
            }
        }
    }

    this.computeItemTotals = function() {
        for (var i = 0; i < this.output.length; i++) {
            var result = this.output[i];
            for (var test in result) {
                if (test != "toJSONString")
                {
                    var time = result[test];
                    var category = test.replace(/-.*/, "");
                    this.itemTotals["total"][i] += time;
                    this.itemTotals[category][i] += time;
                    this.itemTotals[test][i] += time;
                }
            }
        }
    }

    this.computeTotals = function() {
        for (var i = 0; i < this.output.length; i++) {
            var result = this.output[i];
            for (var test in result) {
                if (test != "toJSONString")
                {
                    var time = result[test];
                    var category = test.replace(/-.*/, "");
                    this.total += time;
                    this.categoryTotals[category] += time;
                    this.testTotalsByCategory[category][test] += time;
                }
            }
        }
    }

    this.computeMeans = function() {
        this.mean = this.total / this.count;
        for (var category in this.categoryTotals) {
            if (category != "toJSONString")
            {
                this.categoryMeans[category] = this.categoryTotals[category] / this.count;
                for (var test in this.testTotalsByCategory[category]) {
                    if (test != "toJSONString")
                    {
                        this.testMeansByCategory[category][test] = this.testTotalsByCategory[category][test] / this.count;
                    }
                }
            }
        }
    }

    this.standardDeviation = function(mean, items) {
        var deltaSquaredSum = 0;
        for (var i = 0; i < items.length; i++) {
            var delta = items[i] - mean;
            deltaSquaredSum += delta * delta;
        }
        variance = deltaSquaredSum / (items.length - 1);
        return Math.sqrt(variance);
    }

    this.computeStdDevs = function() {
        this.stdDev = this.standardDeviation(this.mean, this.itemTotals["total"]);
        for (var category in this.categoryStdDevs) {
            if (category!= "toJSONString") {
                this.categoryStdDevs[category] = this.standardDeviation(this.categoryMeans[category], this.itemTotals[category]);
            }
        }
        for (var category in this.categoryStdDevs) {
            if (category!= "toJSONString") {
                for (var test in this.testStdDevsByCategory[category]) {
                    if (test!= "toJSONString") {
                        this.testStdDevsByCategory[category][test] = this.standardDeviation(this.testMeansByCategory[category][test], this.itemTotals[test]);
                    }
                }
            }
        }
    }

    this.computeStdErrors = function() {
        var sqrtCount = Math.sqrt(this.count);

        this.stdErr = this.stdDev / sqrtCount;
        for (var category in this.categoryStdErrs) {
            if (category != "toJSONString") {
                this.categoryStdErrs[category] = this.categoryStdDevs[category] / sqrtCount;
            }
        }
        for (var category in this.categoryStdDevs) {
            if (category != "toJSONString") {
                for (var test in this.testStdErrsByCategory[category]) {
                    if (test != "toJSONString") {
                        this.testStdErrsByCategory[category][test] = this.testStdDevsByCategory[category][test] / sqrtCount;
                    }
                }
            }
        }

    }

    this.tDistribution = [NaN, NaN, 12.71, 4.30, 3.18, 2.78, 2.57, 2.45, 2.36, 2.31, 2.26, 2.23, 2.20, 2.18, 2.16, 2.14, 2.13, 2.12, 2.11, 2.10, 2.09, 2.09, 2.08, 2.07, 2.07, 2.06, 2.06, 2.06, 2.05, 2.05, 2.05, 2.04, 2.04, 2.04, 2.03, 2.03, 2.03, 2.03, 2.03, 2.02, 2.02, 2.02, 2.02, 2.02, 2.02, 2.02, 2.01, 2.01, 2.01, 2.01, 2.01, 2.01, 2.01, 2.01, 2.01, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.99, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.98, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.97, 1.96];
    this.tMax = this.tDistribution.length;
    this.tLimit = 1.96;

    this.tDist = function(n) {
        if (n > this.tMax)
            return thils.tLimit;
        return this.tDistribution[n];
    }


    this.formatResult = function(meanWidth, mean, stdErr, n) {
        var meanString = mean.toFixed(1).toString();
        while (meanString.length < meanWidth) {
            meanString = " " + meanString;
        }

        if (n == 1)
            return meanString + "ms";

        return meanString + "ms +/- " + ((this.tDist(n) * stdErr / mean) * 100).toFixed(1) + "%";
    }

    this.computeLabelWidth = function() {
        var width = "Total".length;
        for (var category in this.categoryMeans) {
            if (category != "toJSONString") {
                if (category.length + 2 > width)
                    width = category.length + 2;
            }
        }
        for (var i = 0; i < tests.length; i++) {
            var shortName = tests[i].replace(/^[^-]*-/, "");
            if (shortName.length + 4 > width)
                width = shortName.length + 4;
        }

        return width;
    }

    this.computeMeanWidth = function() {
        var width = this.mean.toFixed(1).toString().length;
        for (var category in this.categoryMeans) {
            if (category != "toJSONString") {
                var candidate = this.categoryMeans[category].toFixed(2).toString().length;
                if (candidate > width)
                    width = candidate;
                for (var test in this.testMeansByCategory[category]) {
                    if (test != "toJSONString") {
                        var candidate = this.testMeansByCategory[category][test].toFixed(2).toString().length;
                        if (candidate > width)
                            width = candidate;
                    }
                }
            }
        }

        return width;
    }

    this.resultLine = function(labelWidth, indent, label, meanWidth, mean, stdErr) {
        var result = "";
        for (i = 0; i < indent; i++) {
            result += " ";
        }

        result += label + ": ";

        for (i = 0; i < (labelWidth - (label.length + indent)); i++) {
            result += " ";
        }

        return result + this.formatResult(meanWidth, mean, stdErr, this.count);
    }

    this.printOutput = function() {
        var labelWidth = this.computeLabelWidth();
        var meanWidth = this.computeMeanWidth();

        var resultsText = "";
        resultsText += "============================================<br/>";
        if (this.count == 1)
            resultsText += "RESULTS<br/>";
        else
            resultsText += "RESULTS (means and 95% confidence intervals)<br/>";
        resultsText += "--------------------------------------------<br/>";
        var temp = this.count;
        this.count = 1;
        resultsText += this.resultLine(labelWidth, 0, "Elapsed", meanWidth, this.elapsed, null) + "<br/>";
        this.count = temp;
        resultsText += this.resultLine(labelWidth, 0, "Total", meanWidth, this.mean, this.stdErr) + "<br/>";
        resultsText += "--------------------------------------------<br/>";
        for (var category in this.categoryMeans) {
            if (category != "toJSONString") {
                resultsText += "<br/>";
                resultsText += this.resultLine(labelWidth, 2, category, meanWidth, this.categoryMeans[category], this.categoryStdErrs[category]) + "<br/>";
                for (var test in this.testMeansByCategory[category]) {
                    if (test != "toJSONString") {
                        var shortName = test.replace(/^[^-]*-/, "");
                        resultsText += this.resultLine(labelWidth, 4, shortName, meanWidth, this.testMeansByCategory[category][test], this.testStdErrsByCategory[category][test]) + "<br/>";
                    }
                }
            }
        }
        return resultsText;
    }

    this.generateResults = function() {
        this.initialize();
        this.computeItemTotals();
        this.computeTotals();
        this.computeMeans();
        this.computeStdDevs();
        this.computeStdErrors();
        return this.printOutput();
    }
}
