1 // Copyright (c) 2012 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.
7 tvcm.require('tvcm.guid');
8 tvcm.require('tvcm.range');
9 tvcm.require('tracing.trace_model.counter_series');
12 * @fileoverview Provides the Counter class.
14 tvcm.exportTo('tracing.trace_model', function() {
17 * Stores all the samples for a given counter.
20 function Counter(parent, id, category, name) {
21 this.guid_ = tvcm.GUID.allocate();
25 this.category = category || '';
30 this.bounds = new tvcm.Range();
34 __proto__: Object.prototype,
37 * @return {Number} A globally unique identifier for this counter.
44 var obj = new Object();
45 var keys = Object.keys(this);
46 for (var i = 0; i < keys.length; i++) {
48 if (typeof this[key] == 'function')
50 if (key == 'parent') {
51 obj[key] = this[key].guid;
60 throw new Error('Bad counter API. No cookie.');
63 set seriesNames(arg) {
64 throw new Error('Bad counter API. No cookie.');
67 set seriesColors(arg) {
68 throw new Error('Bad counter API. No cookie.');
72 throw new Error('Bad counter API. No cookie.');
75 addSeries: function(series) {
76 series.counter = this;
77 series.seriesIndex = this.series_.length;
78 this.series_.push(series);
82 getSeries: function(idx) {
83 return this.series_[idx];
91 return this.series_.length;
95 if (this.series_.length === 0)
97 return this.series_[0].length;
101 if (this.series_.length === 0)
103 return this.series_[0].timestamps;
107 * Obtains min, max, avg, values, start, and end for different series for
109 * getSampleStatistics([0,1])
110 * The statistics objects that this returns are an array of objects, one
111 * object for each series for the counter in the form:
112 * {min: minVal, max: maxVal, avg: avgVal, start: startVal, end: endVal}
114 * @param {Array.<Number>} Indices to summarize.
115 * @return {Object} An array of statistics. Each element in the array
116 * has data for one of the series in the selected counter.
118 getSampleStatistics: function(sampleIndices) {
119 sampleIndices.sort();
122 this.series_.forEach(function(series) {
123 ret.push(series.getStatistics(sampleIndices));
129 * Shifts all the timestamps inside this counter forward by the amount
132 shiftTimestampsForward: function(amount) {
133 for (var i = 0; i < this.series_.length; ++i)
134 this.series_[i].shiftTimestampsForward(amount);
138 * Updates the bounds for this counter based on the samples it contains.
140 updateBounds: function() {
145 if (this.series_.length === 0)
148 var firstSeries = this.series_[0];
149 var lastSeries = this.series_[this.series_.length - 1];
151 this.bounds.addValue(firstSeries.getTimestamp(0));
152 this.bounds.addValue(lastSeries.getTimestamp(lastSeries.length - 1));
154 var numSeries = this.numSeries;
155 this.maxTotal = -Infinity;
157 // Sum the samples at each timestamp.
158 // Note, this assumes that all series have all timestamps.
159 for (var i = 0; i < firstSeries.length; ++i) {
161 this.series_.forEach(function(series) {
162 total += series.getSample(i).value;
163 this.totals.push(total);
166 this.maxTotal = Math.max(total, this.maxTotal);
170 iterateAllEvents: function(callback) {
171 for (var i = 0; i < this.series_.length; i++)
172 this.series_[i].iterateAllEvents(callback);
177 * Comparison between counters that orders by parent.compareTo, then name.
179 Counter.compare = function(x, y) {
180 var tmp = x.parent.compareTo(y);
183 var tmp = x.name.localeCompare(y.name);
185 return x.tid - y.tid;