| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Add a periodically updated performance graph. |
| 7 */ |
| 8 |
| 9 /** |
| 10 * Namespace for Performance Graphing related functions. |
| 11 */ |
| 12 var graph = {}; |
| 13 |
| 14 /** |
| 15 * Sequence of characters used for GraphAPI encoding. |
| 16 * @private |
| 17 */ |
| 18 graph.GRAPH_SCALE_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + |
| 19 'abcdefghijklmnopqrstuvwxyz0123456789'; |
| 20 |
| 21 /** |
| 22 * Encode a single graph value as a 'simple point' character. |
| 23 * @param {integer} val Value we are graphing (0-61). |
| 24 * @return {string} The encoded point. |
| 25 */ |
| 26 graph.simplePoint = function(val) { |
| 27 return graph.GRAPH_SCALE_[val]; |
| 28 }; |
| 29 |
| 30 /** |
| 31 * Given data to graph generate a GraphAPI URL. |
| 32 * @param {dict} data The data to graph. Containing: 'builders' listing the name |
| 33 * of each builder, 'times' containing the times sampled, and data for |
| 34 * [time][builder] for each time, builder pair. |
| 35 * @return {string} A GraphAPI URL to graph the data. |
| 36 */ |
| 37 graph.graphURL = function(data) { |
| 38 // Get the list of builders and reorder it. |
| 39 var builders = []; |
| 40 for (var b in data['builders']) { |
| 41 builders.push(b); |
| 42 } |
| 43 builders.sort(); |
| 44 |
| 45 // Get the list of times and reorder it. |
| 46 var times = []; |
| 47 for (var t in data['times']) { |
| 48 times.push(t); |
| 49 } |
| 50 times.sort(); |
| 51 |
| 52 // Convert points to numeric samples. |
| 53 var pointsNumeric = []; |
| 54 var buildersLen = builders.length; |
| 55 for (var b = 0; b < buildersLen; b++) { |
| 56 var builder = builders[b]; |
| 57 var bp = []; |
| 58 var timesLen = times.length; |
| 59 for (var t = 0; t < timesLen; t++) { |
| 60 var tm = times[t]; |
| 61 if (tm in data && builder in data[tm]) { |
| 62 var v = data[tm][builder]; |
| 63 } else { |
| 64 var v = 0; |
| 65 } |
| 66 bp.push(v); |
| 67 } |
| 68 pointsNumeric.push(bp); |
| 69 } |
| 70 |
| 71 // Calculate the total. |
| 72 total = []; |
| 73 var pointsNumeric0Len = 0; |
| 74 if (pointsNumeric.length > 0) pointsNumeric0Len = pointsNumeric[0].length; |
| 75 for (var vp = 0; vp < pointsNumeric0Len; vp++) { |
| 76 total.push(0); |
| 77 } |
| 78 var pointsNumericLen = pointsNumeric.length; |
| 79 for (var ptr = 0; ptr < pointsNumericLen; ptr++) { |
| 80 for (var vp = 0; vp < pointsNumeric0Len; vp++) { |
| 81 total[vp] += pointsNumeric[ptr][vp]; |
| 82 } |
| 83 } |
| 84 builders.unshift('TOTAL|TOTAL'); |
| 85 pointsNumeric.unshift(total); |
| 86 |
| 87 // Find the maximum values in all graphs. |
| 88 minValue = 9e9; |
| 89 maxValue = -9e9; |
| 90 for (var b = 0; b < pointsNumericLen; b++) { |
| 91 for (var t = 0; t < pointsNumeric0Len; t++) { |
| 92 var v = pointsNumeric[b][t]; |
| 93 if (v < minValue) minValue = v; |
| 94 if (v > maxValue) maxValue = v; |
| 95 } |
| 96 } |
| 97 // Force minimum to at least zero. |
| 98 if (minValue > 0) minValue = 0; |
| 99 // Precomute the gap between smallest and biggest. |
| 100 var gap = Math.max(maxValue - minValue, 1); |
| 101 |
| 102 // Convert numeric samples to simple samples. |
| 103 var points = []; |
| 104 for (var b = 0; b < pointsNumericLen; b++) { |
| 105 var bp = ''; |
| 106 for (var t = 0; t < pointsNumeric0Len; t++) { |
| 107 var v = pointsNumeric[b][t]; |
| 108 v = Math.floor((v - minValue) * 61 / gap); |
| 109 bp += graph.simplePoint(v); |
| 110 } |
| 111 points.push(bp); |
| 112 } |
| 113 var pointsPacked = points.join(','); |
| 114 |
| 115 // Get list of builder names for labels. |
| 116 var builderNames = []; |
| 117 for (var b in builders) { |
| 118 var name = builders[b].split('|')[1]; |
| 119 builderNames.push(name); |
| 120 } |
| 121 |
| 122 // Get number of points if any. |
| 123 var numPoints = 0; |
| 124 if (points.length) { |
| 125 numPoints = points[0].length; |
| 126 } |
| 127 |
| 128 // Generate URL. |
| 129 var url = 'http://chart.apis.google.com/chart?' + |
| 130 'chco=000000,ff0000,ffcc00,ffff00,00ff00,00ffff,0000ff,ff00ff&' + |
| 131 //'chxr=0,0,' + numPoints + '|1,' + |
| 132 //minValue + ',' + maxValue + '&' + |
| 133 'chxr=0,' + minValue + ',' + maxValue + '&' + |
| 134 //'chxt=x,y&' + |
| 135 'chxt=y&' + |
| 136 'chf=c,lg,45,eeeeee,1,888888,0&' + |
| 137 'chdl=' + builderNames.join('|') + '&' + |
| 138 'cht=lc&chs=400x200&' + |
| 139 'chd=s:' + pointsPacked; |
| 140 return url; |
| 141 }; |
| 142 |
| 143 /** |
| 144 * Decode the result of querying the server for performance data. |
| 145 * @param {string} text The result of a performance_raw query. |
| 146 * @param {dict} data A variable to use for data storage for this graph. |
| 147 */ |
| 148 graph.decodeData = function(text, data) { |
| 149 if (!text) return; |
| 150 if (!('times' in data)) data['times'] = {}; |
| 151 if (!('builders' in data)) data['builders'] = {}; |
| 152 var rows = text.split('\n'); |
| 153 for (var r in rows) { |
| 154 var fields = rows[r].split('|'); |
| 155 if (fields.length != 5) continue; |
| 156 var builder = fields[0] + '|' + fields[1]; |
| 157 var tm = parseInt(fields[2]); |
| 158 var count = parseInt(fields[3]) / parseInt(fields[4]); |
| 159 if (!(tm in data)) data[tm] = {}; |
| 160 data[tm][builder] = count; |
| 161 data['times'][tm] = 0; |
| 162 data['builders'][builder] = 0; |
| 163 } |
| 164 |
| 165 // Get the list of times and reorder it. |
| 166 var times = []; |
| 167 for (var t in data['times']) { |
| 168 times.push(t); |
| 169 } |
| 170 times.sort(); |
| 171 |
| 172 // Dump anything older than the first 60 samples. |
| 173 if (times.length > 70) { |
| 174 var junk = times.slice(times.length - 70, times.length); |
| 175 for (var j in junk) { |
| 176 var tm = junk[j]; |
| 177 delete data[tm]; |
| 178 delete data['times'][tm]; |
| 179 } |
| 180 } |
| 181 }; |
| 182 |
| 183 /** |
| 184 * Setup everything needed to keep a performance graph up to date. |
| 185 * @param {string} id The id of an HTML <div> to receive the graph. |
| 186 * @param {dict} data A variable to use for data storage related to this graph. |
| 187 * @param {float} interval The update interval in seconds. |
| 188 */ |
| 189 graph.setup = function(id, data, interval) { |
| 190 // Init goal if not set. |
| 191 if (!('goal' in data)) data['goal'] = interval * 70; |
| 192 // Get next item. |
| 193 var step = interval * 20; |
| 194 var goal = data['goal']; |
| 195 var next = goal - step; |
| 196 if (next < 0) next = 0; |
| 197 data['goal'] = next; |
| 198 // Fetch it. |
| 199 http.getText( |
| 200 '/performance_raw?interval=' + interval + |
| 201 '&start=' + goal + '&stop=' + next, |
| 202 function(text) { |
| 203 graph.decodeData(text, data); |
| 204 var e = document.getElementById(id); |
| 205 e.src = graph.graphURL(data); |
| 206 var timeout; |
| 207 if (next == 0) { |
| 208 timeout = 20000; |
| 209 } else { |
| 210 timeout = 0; |
| 211 } |
| 212 window.setTimeout(function() { |
| 213 graph.setup(id, data, interval); |
| 214 }, timeout); |
| 215 }); |
| 216 }; |
| 217 |
| OLD | NEW |