summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/pageloader/chrome/report.js
blob: 7f5d61922a22882488a23df11e0dd712a5cc8141 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// given an array of strings, finds the longest common prefix
function findCommonPrefixLength(strs) {
  if (strs.length < 2) {
    // only one page in the manifest
    // i.e. http://localhost/tests/perf-reftest/bloom-basic.html
    var place = strs[0].lastIndexOf("/");
    if (place < 0) {
      place = 0;
    }
    return place;
  }

  var len = 0;
  do {
    var newlen = len + 1;
    var newprefix = null;
    var failed = false;
    for (var i = 0; i < strs.length; i++) {
      if (newlen > strs[i].length) {
        failed = true;
        break;
      }

      var s = strs[i].substr(0, newlen);
      if (newprefix == null) {
        newprefix = s;
      } else if (newprefix != s) {
        failed = true;
        break;
      }
    }

    if (failed) {
      break;
    }

    len++;
  } while (true);
  return len;
}

// Constructor
function Report() {
  this.timeVals = {};
  this.totalCCTime = 0;
  this.showTotalCCTime = false;
}

Report.prototype.pageNames = function () {
  var retval = [];
  for (var page in this.timeVals) {
    retval.push(page);
  }
  return retval;
};

Report.prototype.getReport = function () {
  var report;
  var pages = this.pageNames();
  var prefixLen = findCommonPrefixLength(pages);

  report = "__start_tp_report\n";
  report += "_x_x_mozilla_page_load\n";
  report += "_x_x_mozilla_page_load_details\n";
  report += "|i|pagename|runs|\n";

  for (var i = 0; i < pages.length; i++) {
    // don't report any measurements that were reported for about:blank
    // some tests (like about-preferences) use it as a dummy test page
    if (pages[i] == "about:blank") {
      continue;
    }
    report +=
      "|" +
      i +
      ";" +
      pages[i].substr(prefixLen) +
      ";" +
      this.timeVals[pages[i]].join(";") +
      "\n";
  }
  report += "__end_tp_report\n";

  if (this.showTotalCCTime) {
    report += "__start_cc_report\n";
    report += "_x_x_mozilla_cycle_collect," + this.totalCCTime + "\n";
    report += "__end_cc_report\n";
  }
  var now = window.performance.now();
  report += "__startTimestamp" + now + "__endTimestamp\n"; // timestamp for determning shutdown time, used by talos

  return report;
};

Report.prototype.getReportSummary = function () {
  function average(arr) {
    var sum = 0;
    for (var i in arr) {
      sum += arr[i];
    }
    return sum / (arr.length || 1);
  }

  function median(arr) {
    if (!arr.length) {
      return 0;
    } // As good indication for "not available" as any other value.

    var sorted = arr.slice(0).sort();
    var mid = Math.floor(arr.length / 2);

    if (sorted.length % 2) {
      return sorted[mid];
    }

    return average(sorted.slice(mid, mid + 2));
  }

  // We use sample stddev and not population stddev because
  // well.. it's a sample and we can't collect all/infinite number of values.
  function stddev(arr) {
    if (arr.length <= 1) {
      return 0;
    }
    var avg = average(arr);

    var squareDiffArr = arr.map(function (v) {
      return Math.pow(v - avg, 2);
    });
    var sum = squareDiffArr.reduce(function (a, b) {
      return a + b;
    });
    var rv = Math.sqrt(sum / (arr.length - 1));
    return rv;
  }

  var report = "";
  var pages = this.pageNames();
  var prefixLen = findCommonPrefixLength(pages);

  report += "------- Summary: start -------\n";
  report += "Number of tests: " + pages.length + "\n";

  for (var i = 0; i < pages.length; i++) {
    var results = this.timeVals[pages[i]].map(function (v) {
      return Number(v);
    });

    report +=
      "\n[#" +
      i +
      "] " +
      pages[i].substr(prefixLen) +
      "  Cycles:" +
      results.length +
      "  Average:" +
      average(results).toFixed(2) +
      "  Median:" +
      median(results).toFixed(2) +
      "  stddev:" +
      stddev(results).toFixed(2) +
      " (" +
      ((100 * stddev(results)) / median(results)).toFixed(1) +
      "%)" +
      (results.length < 5
        ? ""
        : "  stddev-sans-first:" + stddev(results.slice(1)).toFixed(2)) +
      "\nValues: " +
      results
        .map(function (v) {
          return v.toFixed(1);
        })
        .join("  ") +
      "\n";
  }
  report += "-------- Summary: end --------\n";

  return report;
};

Report.prototype.recordTime = function (pageName, ms) {
  if (this.timeVals[pageName] == undefined) {
    this.timeVals[pageName] = [];
  }
  this.timeVals[pageName].push(ms);
};

Report.prototype.recordCCTime = function (ms) {
  this.totalCCTime += ms;
  this.showTotalCCTime = true;
};