Home | History | Annotate | Download | only in trace_model
      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.
      4 
      5 'use strict';
      6 
      7 base.require('base.guid');
      8 
      9 /**
     10  * @fileoverview Provides the CounterSeries class.
     11  */
     12 base.exportTo('tracing.trace_model', function() {
     13 
     14   function CounterSample(timestamp, value) {
     15     this.guid_ = base.GUID.allocate();
     16     this.timestamp_ = timestamp;
     17     this.value_ = value;
     18   }
     19 
     20   CounterSample.prototype = {
     21     __proto__: Object.prototype,
     22 
     23     get value() {
     24       return this.value_;
     25     },
     26 
     27     set timestamp(timestamp) {
     28       this.timestamp_ = timestamp;
     29     },
     30 
     31     toJSON: function() {
     32       var obj = new Object();
     33       var keys = Object.keys(this);
     34       for (var i = 0; i < keys.length; i++) {
     35         var key = keys[i];
     36         if (typeof this[key] == 'function')
     37           continue;
     38         if (key == 'parent') {
     39           obj[key] = this[key].guid;
     40           continue;
     41         }
     42         obj[key] = this[key];
     43       }
     44       return obj;
     45     }
     46   };
     47 
     48   function CounterSeries(name, color) {
     49     this.guid_ = base.GUID.allocate();
     50 
     51     this.name_ = name;
     52     this.color_ = color;
     53 
     54     this.timestamps_ = [];
     55     this.samples_ = [];
     56   }
     57 
     58   CounterSeries.prototype = {
     59     __proto__: Object.prototype,
     60 
     61     toJSON: function() {
     62       var obj = new Object();
     63       var keys = Object.keys(this);
     64       for (var i = 0; i < keys.length; i++) {
     65         var key = keys[i];
     66         if (typeof this[key] == 'function')
     67           continue;
     68         if (key == 'parent') {
     69           obj[key] = this[key].guid;
     70           continue;
     71         }
     72         obj[key] = this[key];
     73       }
     74       return obj;
     75     },
     76 
     77     get length() {
     78       return this.timestamps_.length;
     79     },
     80 
     81     get name() {
     82       return this.name_;
     83     },
     84 
     85     get color() {
     86       return this.color_;
     87     },
     88 
     89     get samples() {
     90       return this.samples_;
     91     },
     92 
     93     get timestamps() {
     94       return this.timestamps_;
     95     },
     96 
     97     getSample: function(idx) {
     98       return this.samples_[idx];
     99     },
    100 
    101     getTimestamp: function(idx) {
    102       return this.timestamps_[idx];
    103     },
    104 
    105     addSample: function(ts, val) {
    106       this.timestamps_.push(ts);
    107       this.samples_.push(new CounterSample(ts, val));
    108     },
    109 
    110     getStatistics: function(sampleIndices) {
    111       var sum = 0;
    112       var min = Number.MAX_VALUE;
    113       var max = -Number.MAX_VALUE;
    114 
    115       for (var i = 0; i < sampleIndices.length; ++i) {
    116         var sample = this.getSample(sampleIndices[i]).value;
    117 
    118         sum += sample;
    119         min = Math.min(sample, min);
    120         max = Math.max(sample, max);
    121       }
    122 
    123       return {
    124         min: min,
    125         max: max,
    126         avg: (sum / sampleIndices.length),
    127         start: this.getSample(sampleIndices[0]).value,
    128         end: this.getSample(sampleIndices.length - 1).value
    129       };
    130     },
    131 
    132     shiftTimestampsForward: function(amount) {
    133       for (var i = 0; i < this.timestamps_.length; ++i) {
    134         this.timestamps_[i] += amount;
    135         this.samples_[i].timestamp = this.timestamps_[i];
    136       }
    137     }
    138   };
    139 
    140   return {
    141     CounterSeries: CounterSeries
    142   };
    143 });
    144