OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 /** |
| 35 * Histogram module. Exports the Histogram class |
| 36 * @module |
| 37 */ |
| 38 |
| 39 'use strict'; |
| 40 |
| 41 /** |
| 42 * Histogram class. Collects data and exposes a histogram and other statistics. |
| 43 * This data structure is taken directly from src/core/support/histogram.c, but |
| 44 * pared down to the statistics needed for client stats in |
| 45 * test/proto/benchmarks/stats.proto. |
| 46 * @constructor |
| 47 * @param {number} resolution The histogram's bucket resolution. Must be positiv
e |
| 48 * @param {number} max_possible The maximum allowed value. Must be greater than
1 |
| 49 */ |
| 50 function Histogram(resolution, max_possible) { |
| 51 this.resolution = resolution; |
| 52 this.max_possible = max_possible; |
| 53 |
| 54 this.sum = 0; |
| 55 this.sum_of_squares = 0; |
| 56 this.multiplier = 1 + resolution; |
| 57 this.count = 0; |
| 58 this.min_seen = max_possible; |
| 59 this.max_seen = 0; |
| 60 this.buckets = []; |
| 61 for (var i = 0; i < this.bucketFor(max_possible) + 1; i++) { |
| 62 this.buckets[i] = 0; |
| 63 } |
| 64 } |
| 65 |
| 66 /** |
| 67 * Get the bucket index for a given value. |
| 68 * @param {number} value The value to check |
| 69 * @return {number} The bucket index |
| 70 */ |
| 71 Histogram.prototype.bucketFor = function(value) { |
| 72 return Math.floor(Math.log(value) / Math.log(this.multiplier)); |
| 73 }; |
| 74 |
| 75 /** |
| 76 * Get the minimum value for a given bucket index |
| 77 * @param {number} The bucket index to check |
| 78 * @return {number} The minimum value for that bucket |
| 79 */ |
| 80 Histogram.prototype.bucketStart = function(index) { |
| 81 return Math.pow(this.multiplier, index); |
| 82 }; |
| 83 |
| 84 /** |
| 85 * Add a value to the histogram. This updates all statistics with the new |
| 86 * value. Those statistics should not be modified except with this function |
| 87 * @param {number} value The value to add |
| 88 */ |
| 89 Histogram.prototype.add = function(value) { |
| 90 // Ensure value is a number |
| 91 value = +value; |
| 92 this.sum += value; |
| 93 this.sum_of_squares += value * value; |
| 94 this.count++; |
| 95 if (value < this.min_seen) { |
| 96 this.min_seen = value; |
| 97 } |
| 98 if (value > this.max_seen) { |
| 99 this.max_seen = value; |
| 100 } |
| 101 this.buckets[this.bucketFor(value)]++; |
| 102 }; |
| 103 |
| 104 /** |
| 105 * Get the mean of all added values |
| 106 * @return {number} The mean |
| 107 */ |
| 108 Histogram.prototype.mean = function() { |
| 109 return this.sum / this.count; |
| 110 }; |
| 111 |
| 112 /** |
| 113 * Get the variance of all added values. Used to calulate the standard deviation |
| 114 * @return {number} The variance |
| 115 */ |
| 116 Histogram.prototype.variance = function() { |
| 117 if (this.count == 0) { |
| 118 return 0; |
| 119 } |
| 120 return (this.sum_of_squares * this.count - this.sum * this.sum) / |
| 121 (this.count * this.count); |
| 122 }; |
| 123 |
| 124 /** |
| 125 * Get the standard deviation of all added values |
| 126 * @return {number} The standard deviation |
| 127 */ |
| 128 Histogram.prototype.stddev = function() { |
| 129 return Math.sqrt(this.variance); |
| 130 }; |
| 131 |
| 132 /** |
| 133 * Get the maximum among all added values |
| 134 * @return {number} The maximum |
| 135 */ |
| 136 Histogram.prototype.maximum = function() { |
| 137 return this.max_seen; |
| 138 }; |
| 139 |
| 140 /** |
| 141 * Get the minimum among all added values |
| 142 * @return {number} The minimum |
| 143 */ |
| 144 Histogram.prototype.minimum = function() { |
| 145 return this.min_seen; |
| 146 }; |
| 147 |
| 148 /** |
| 149 * Get the number of all added values |
| 150 * @return {number} The count |
| 151 */ |
| 152 Histogram.prototype.getCount = function() { |
| 153 return this.count; |
| 154 }; |
| 155 |
| 156 /** |
| 157 * Get the sum of all added values |
| 158 * @return {number} The sum |
| 159 */ |
| 160 Histogram.prototype.getSum = function() { |
| 161 return this.sum; |
| 162 }; |
| 163 |
| 164 /** |
| 165 * Get the sum of squares of all added values |
| 166 * @return {number} The sum of squares |
| 167 */ |
| 168 Histogram.prototype.sumOfSquares = function() { |
| 169 return this.sum_of_squares; |
| 170 }; |
| 171 |
| 172 /** |
| 173 * Get the raw histogram as a list of bucket sizes |
| 174 * @return {Array.<number>} The buckets |
| 175 */ |
| 176 Histogram.prototype.getContents = function() { |
| 177 return this.buckets; |
| 178 }; |
| 179 |
| 180 module.exports = Histogram; |
OLD | NEW |