1 // Copyright (c) 2013 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('tracing.trace_model.counter_sample');
10 * @fileoverview Provides the CounterSeries class.
12 tvcm.exportTo('tracing.trace_model', function() {
13 var CounterSample = tracing.trace_model.CounterSample;
15 function CounterSeries(name, color) {
16 this.guid_ = tvcm.GUID.allocate();
21 this.timestamps_ = [];
24 // Set by counter.addSeries
25 this.counter = undefined;
26 this.seriesIndex = undefined;
29 CounterSeries.prototype = {
30 __proto__: Object.prototype,
33 var obj = new Object();
34 var keys = Object.keys(this);
35 for (var i = 0; i < keys.length; i++) {
37 if (typeof this[key] == 'function')
39 if (key == 'counter') {
40 obj[key] = this[key].guid;
49 return this.timestamps_.length;
65 return this.timestamps_;
68 getSample: function(idx) {
69 return this.samples_[idx];
72 getTimestamp: function(idx) {
73 return this.timestamps_[idx];
76 addSample: function(ts, val) {
77 this.timestamps_.push(ts);
78 var sample = new CounterSample(this, ts, val);
79 this.samples_.push(sample);
83 getStatistics: function(sampleIndices) {
85 var min = Number.MAX_VALUE;
86 var max = -Number.MAX_VALUE;
88 for (var i = 0; i < sampleIndices.length; ++i) {
89 var sample = this.getSample(sampleIndices[i]).value;
92 min = Math.min(sample, min);
93 max = Math.max(sample, max);
99 avg: (sum / sampleIndices.length),
100 start: this.getSample(sampleIndices[0]).value,
101 end: this.getSample(sampleIndices.length - 1).value
105 shiftTimestampsForward: function(amount) {
106 for (var i = 0; i < this.timestamps_.length; ++i) {
107 this.timestamps_[i] += amount;
108 this.samples_[i].timestamp = this.timestamps_[i];
112 iterateAllEvents: function(callback) {
113 this.samples_.forEach(callback);
118 CounterSeries: CounterSeries