!import
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is tp.
16 *
17 * The Initial Developer of the Original Code is
18 * Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Darin Fisher <darin@meer.net>
24 * Rob Helmer <rhelmer@mozilla.com>
25 * Vladimir Vukicevic <vladimir@mozilla.com>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 // Constructor
Report
42 function Report(pages) {
43 this.pages = pages;
44 this.timeVals = new Array(pages.length); // matrix of times
45 for (var i = 0; i < this.timeVals.length; ++i) {
46 this.timeVals[i] = new Array();
47 }
48 this.totalCCTime = 0;
49 this.showTotalCCTime = false;
50 }
51
52 // given an array of strings, finds the longest common prefix
findCommonPrefixLength
53 function findCommonPrefixLength(strs) {
54 if (strs.length < 2)
55 return 0;
56
57 var len = 0;
58 do {
59 var newlen = len + 1;
60 var newprefix = null;
61 var failed = false;
62 for (var i = 0; i < strs.length; i++) {
63 if (newlen > strs[i].length) {
64 failed = true;
65 break;
66 }
67
68 var s = strs[i].substr(0, newlen);
69 if (newprefix == null) {
70 newprefix = s;
71 } else if (newprefix != s) {
72 failed = true;
73 break;
74 }
75 }
76
77 if (failed)
78 break;
79
80 len++;
81 } while (true);
82 return len;
83 }
84
85 // returns an object with the following properties:
86 // min : min value of array elements
87 // max : max value of array elements
88 // mean : mean value of array elements
89 // vari : variance computation
90 // stdd : standard deviation, sqrt(vari)
91 // indexOfMax : index of max element (the element that is
92 // removed from the mean computation)
getArrayStats
93 function getArrayStats(ary) {
94 var r = {};
95 r.min = ary[0];
96 r.max = ary[0];
97 r.indexOfMax = 0;
98 var sum = 0;
99 for (var i = 0; i < ary.length; ++i) {
100 if (ary[i] < r.min) {
101 r.min = ary[i];
102 } else if (ary[i] > r.max) {
103 r.max = ary[i];
104 r.indexOfMax = i;
105 }
106 sum = sum + ary[i];
107 }
108
109 // median
110 if (ary.length > 1) {
111 sorted_ary = ary.concat();
112 sorted_ary.sort();
113 // remove longest run
114 sorted_ary.pop();
115 if (sorted_ary.length%2) {
116 r.median = sorted_ary[(sorted_ary.length-1)/2];
117 }else{
118 var n = Math.floor(sorted_ary.length / 2);
119 if (n >= sorted_ary.length)
120 r.median = sorted_ary[n];
121 else
122 r.median = (sorted_ary[n] + sorted_ary[n + 1]) / 2;
123 }
124 }else{
125 r.median = ary[0];
126 }
127
128 // ignore max value when computing mean and stddev
129 if (ary.length > 1)
130 r.mean = (sum - r.max) / (ary.length - 1);
131 else
132 r.mean = ary[0];
133
134 r.vari = 0;
135 for (var i = 0; i < ary.length; ++i) {
136 if (i == r.indexOfMax)
137 continue;
138 var d = r.mean - ary[i];
139 r.vari = r.vari + d * d;
140 }
141
142 if (ary.length > 1) {
143 r.vari = r.vari / (ary.length - 1);
144 r.stdd = Math.sqrt(r.vari);
145 } else {
146 r.vari = 0.0;
147 r.stdd = 0.0;
148 }
149 return r;
150 }
151
strPad
152 function strPad(o, len, left) {
153 var str = o.toString();
154 if (!len)
155 len = 6;
156 if (left == null)
157 left = true;
158
159 if (str.length < len) {
160 len -= str.length;
161 while (--len) {
162 if (left)
163 str = " " + str;
164 else
165 str += " ";
166 }
167 }
168
169 str += " ";
170 return str;
171 }
172
strPadFixed
173 function strPadFixed(n, len, left) {
174 return strPad(n.toFixed(0), len, left);
175 }
176
getReport
177 Report.prototype.getReport = function(format) {
178 // avg and avg median are cumulative for all the pages
179 var avgs = new Array();
180 var medians = new Array();
181 for (var i = 0; i < this.timeVals.length; ++i) {
182 avgs[i] = getArrayStats(this.timeVals[i]).mean;
183 medians[i] = getArrayStats(this.timeVals[i]).median;
184 }
185 var avg = getArrayStats(avgs).mean;
186 var avgmed = getArrayStats(medians).mean;
187
188 var report;
189
190 var prefixLen = findCommonPrefixLength(this.pages);
191
192 if (format == "js") {
193 // output "simple" js format;
194 // array of { page: "str", value: 123.4, stddev: 23.3 } objects
195 report = "([";
196 for (var i = 0; i < this.timeVals.length; i++) {
197 var stats = getArrayStats(this.timeVals[i]);
198 report += uneval({ page: this.pages[i].substr(prefixLen), value: stats.mean, stddev: stats.stdd});
199 report += ",";
200 }
201 report += "])";
202 } else if (format == "jsfull") {
203 // output "full" js format, with raw values
204 } else if (format == "text") {
205 // output text format suitable for dumping
206 report = "============================================================\n";
207 report += " " + strPad("Page", 40, false) + strPad("mean") + strPad("stdd") + strPad("min") + strPad("max") + "raw" + "\n";
208 for (var i = 0; i < this.timeVals.length; i++) {
209 var stats = getArrayStats(this.timeVals[i]);
210 report +=
211 strPad(i, 4, true) +
212 strPad(this.pages[i].substr(prefixLen), 40, false) +
213 strPadFixed(stats.mean) +
214 strPadFixed(stats.stdd) +
215 strPadFixed(stats.min) +
216 strPadFixed(stats.max) +
217 this.timeVals[i] +
218 "\n";
219 }
220 if (this.showTotalCCTime) {
221 report += "Cycle collection: " + this.totalCCTime + "\n"
222 }
223 report += "============================================================\n";
224 } else if (format == "tinderbox") {
225 report = "__start_tp_report\n";
226 report += "_x_x_mozilla_page_load,"+avgmed+",NaN,NaN\n"; // max and min are just 0, ignored
227 report += "_x_x_mozilla_page_load_details,avgmedian|"+avgmed+"|average|"+avg.toFixed(2)+"|minimum|NaN|maximum|NaN|stddev|NaN";
228
229 for (var i = 0; i < this.timeVals.length; i++) {
230 var r = getArrayStats(this.timeVals[i]);
231 report += '|'+
232 i + ';'+
233 this.pages[i].substr(prefixLen) + ';'+
234 r.median + ';'+
235 r.mean + ';'+
236 r.min + ';'+
237 r.max + ';'+
238 this.timeVals[i].join(";") +
239 "\n";
240 }
241 report += "__end_tp_report\n";
242 if (this.showTotalCCTime) {
243 report += "__start_cc_report\n";
244 report += "_x_x_mozilla_cycle_collect," + this.totalCCTime + "\n";
245 report += "__end_cc_report\n";
246 }
247 } else {
248 report = "Unknown report format";
249 }
250
251 return report;
252 }
253
recordTime
254 Report.prototype.recordTime = function(pageIndex, ms) {
255 this.timeVals[pageIndex].push(ms);
256 }
257
recordCCTime
258 Report.prototype.recordCCTime = function(ms) {
259 this.totalCCTime += ms;
260 this.showTotalCCTime = true;
261 }