OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 | |
7 * The webapp reads the plugin's connection statistics frequently (once per | |
8 * second). It logs statistics to the server less frequently, to keep | |
9 * bandwidth and storage costs down. This class bridges that gap, by | |
10 * accumulating high-frequency numeric data, and providing statistics | |
11 * summarising that data. | |
12 */ | |
13 | |
14 'use strict'; | |
15 | |
16 /** @suppress {duplicate} */ | |
17 var remoting = remoting || {}; | |
18 | |
19 (function() { | |
20 | |
21 /** | |
22 * @constructor | |
23 */ | |
24 remoting.StatsAccumulator = function() { | |
25 /** | |
26 * A map from names to lists of values. | |
27 * @private {Object<Array<number>>} | |
28 */ | |
29 this.valueLists_ = {}; | |
30 | |
31 /** | |
32 * The first time, after this object was most recently initialized or emptied, | |
33 * at which a value was added to this object. | |
34 * @private {?number} | |
35 */ | |
36 this.timeOfFirstValue_ = null; | |
37 }; | |
38 | |
39 /** | |
40 * @param {Object<number>} stats | |
41 * @return {boolean} true if there is any non-zero value in stats, false | |
42 * otherwise. | |
43 */ | |
44 function hasValidField(stats) { | |
45 for (var key in stats) { | |
46 if (stats[key] !== 0) { | |
47 return true; | |
48 } | |
49 } | |
50 return false; | |
51 } | |
52 | |
53 /** | |
54 * Adds values to this object. Do nothing if newValues has no valid field. | |
55 * | |
56 * @param {Object<number>} newValues | |
57 */ | |
58 remoting.StatsAccumulator.prototype.add = function(newValues) { | |
59 if (!hasValidField(newValues)) { | |
60 return; | |
61 } | |
62 for (var key in newValues) { | |
63 this.getValueList(key).push(newValues[key]); | |
64 } | |
65 if (this.timeOfFirstValue_ === null) { | |
66 this.timeOfFirstValue_ = new Date().getTime(); | |
67 } | |
68 }; | |
69 | |
70 /** | |
71 * Empties this object. | |
72 */ | |
73 remoting.StatsAccumulator.prototype.empty = function() { | |
74 this.valueLists_ = {}; | |
75 this.timeOfFirstValue_ = null; | |
76 }; | |
77 | |
78 /** | |
79 * Gets the number of milliseconds since the first value was added to this | |
80 * object, after this object was most recently initialized or emptied. | |
81 * | |
82 * @return {number} milliseconds since the first value | |
83 */ | |
84 remoting.StatsAccumulator.prototype.getTimeSinceFirstValue = function() { | |
85 if (this.timeOfFirstValue_ === null) { | |
86 return 0; | |
87 } | |
88 return new Date().getTime() - this.timeOfFirstValue_; | |
89 }; | |
90 | |
91 /** | |
92 * Calculates the mean of the values for a given key. | |
93 * | |
94 * @param {string} key | |
95 * @return {number} the mean of the values for that key | |
96 */ | |
97 remoting.StatsAccumulator.prototype.calcMean = function(key) { | |
98 /** | |
99 * @param {Array<number>} values | |
100 * @return {number} | |
101 */ | |
102 var calcMean = function(values) { | |
103 if (values.length == 0) { | |
104 return 0.0; | |
105 } | |
106 var sum = 0; | |
107 for (var i = 0; i < values.length; i++) { | |
108 sum += values[i]; | |
109 } | |
110 return sum / values.length; | |
111 }; | |
112 return this.map(key, calcMean); | |
113 }; | |
114 | |
115 /** | |
116 * Finds the max of the values for a given key. | |
117 * | |
118 * @param {string} key | |
119 * @return {number} the max of the values for that key | |
120 */ | |
121 remoting.StatsAccumulator.prototype.calcMax = function(key) { | |
122 /** | |
123 * @param {Array<number>} values | |
124 * @return {number} | |
125 */ | |
126 var calcMax = function(values) { | |
127 if (!values || !values.length) { | |
128 return 0; | |
129 } | |
130 return Math.max.apply(null, values); | |
131 }; | |
132 return this.map(key, calcMax); | |
133 }; | |
134 | |
135 /** | |
136 * Applies a given map to the list of values for a given key. | |
137 * | |
138 * @param {string} key | |
139 * @param {function(Array<number>): number} map | |
140 * @return {number} the result of applying that map to the list of values for | |
141 * that key | |
142 */ | |
143 remoting.StatsAccumulator.prototype.map = function(key, map) { | |
144 return map(this.getValueList(key)); | |
145 }; | |
146 | |
147 /** | |
148 * Gets the list of values for a given key. | |
149 * If this object contains no values for that key, then this routine creates | |
150 * an empty list, stores it in this object, and returns it. | |
151 * | |
152 * @private | |
153 * @param {string} key | |
154 * @return {Array<number>} the list of values for that key | |
155 */ | |
156 remoting.StatsAccumulator.prototype.getValueList = function(key) { | |
157 var valueList = this.valueLists_[key]; | |
158 if (!valueList) { | |
159 valueList = []; | |
160 this.valueLists_[key] = valueList; | |
161 } | |
162 return valueList; | |
163 }; | |
164 | |
165 /** | |
166 * @return {?remoting.ClientSession.PerfStats} returns null if all fields are | |
167 * zero. | |
168 */ | |
169 remoting.StatsAccumulator.prototype.getPerfStats = function() { | |
170 var stats = new remoting.ClientSession.PerfStats(); | |
171 stats.videoBandwidth = this.calcMean('videoBandwidth'); | |
172 stats.captureLatency = this.calcMean('captureLatency'); | |
173 stats.maxCaptureLatency = this.calcMax('maxCaptureLatency'); | |
174 stats.encodeLatency = this.calcMean('encodeLatency'); | |
175 stats.maxEncodeLatency = this.calcMax('maxEncodeLatency'); | |
176 stats.decodeLatency = this.calcMean('decodeLatency'); | |
177 stats.maxDecodeLatency = this.calcMax('maxDecodeLatency'); | |
178 stats.renderLatency = this.calcMean('renderLatency'); | |
179 stats.maxRenderLatency = this.calcMax('maxRenderLatency'); | |
180 stats.roundtripLatency = this.calcMean('roundtripLatency'); | |
181 stats.maxRoundtripLatency = this.calcMax('maxRoundtripLatency'); | |
182 | |
183 return hasValidField(stats) ? stats : null; | |
184 }; | |
185 | |
186 })(); | |
OLD | NEW |