histogram.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Histogram module. Exports the Histogram class
  35. * @module
  36. */
  37. 'use strict';
  38. /**
  39. * Histogram class. Collects data and exposes a histogram and other statistics.
  40. * This data structure is taken directly from src/core/support/histogram.c, but
  41. * pared down to the statistics needed for client stats in
  42. * test/proto/benchmarks/stats.proto.
  43. * @constructor
  44. * @param {number} resolution The histogram's bucket resolution. Must be positive
  45. * @param {number} max_possible The maximum allowed value. Must be greater than 1
  46. */
  47. function Histogram(resolution, max_possible) {
  48. this.resolution = resolution;
  49. this.max_possible = max_possible;
  50. this.sum = 0;
  51. this.sum_of_squares = 0;
  52. this.multiplier = 1 + resolution;
  53. this.count = 0;
  54. this.min_seen = max_possible;
  55. this.max_seen = 0;
  56. this.buckets = [];
  57. for (var i = 0; i < this.bucketFor(max_possible) + 1; i++) {
  58. this.buckets[i] = 0;
  59. }
  60. }
  61. /**
  62. * Get the bucket index for a given value.
  63. * @param {number} value The value to check
  64. * @return {number} The bucket index
  65. */
  66. Histogram.prototype.bucketFor = function(value) {
  67. return Math.floor(Math.log(value) / Math.log(this.multiplier));
  68. };
  69. /**
  70. * Get the minimum value for a given bucket index
  71. * @param {number} The bucket index to check
  72. * @return {number} The minimum value for that bucket
  73. */
  74. Histogram.prototype.bucketStart = function(index) {
  75. return Math.pow(this.multiplier, index);
  76. };
  77. /**
  78. * Add a value to the histogram. This updates all statistics with the new
  79. * value. Those statistics should not be modified except with this function
  80. * @param {number} value The value to add
  81. */
  82. Histogram.prototype.add = function(value) {
  83. // Ensure value is a number
  84. value = +value;
  85. this.sum += value;
  86. this.sum_of_squares += value * value;
  87. this.count++;
  88. if (value < this.min_seen) {
  89. this.min_seen = value;
  90. }
  91. if (value > this.max_seen) {
  92. this.max_seen = value;
  93. }
  94. this.buckets[this.bucketFor(value)]++;
  95. };
  96. /**
  97. * Get the mean of all added values
  98. * @return {number} The mean
  99. */
  100. Histogram.prototype.mean = function() {
  101. return this.sum / this.count;
  102. };
  103. /**
  104. * Get the variance of all added values. Used to calulate the standard deviation
  105. * @return {number} The variance
  106. */
  107. Histogram.prototype.variance = function() {
  108. if (this.count == 0) {
  109. return 0;
  110. }
  111. return (this.sum_of_squares * this.count - this.sum * this.sum) /
  112. (this.count * this.count);
  113. };
  114. /**
  115. * Get the standard deviation of all added values
  116. * @return {number} The standard deviation
  117. */
  118. Histogram.prototype.stddev = function() {
  119. return Math.sqrt(this.variance);
  120. };
  121. /**
  122. * Get the maximum among all added values
  123. * @return {number} The maximum
  124. */
  125. Histogram.prototype.maximum = function() {
  126. return this.max_seen;
  127. };
  128. /**
  129. * Get the minimum among all added values
  130. * @return {number} The minimum
  131. */
  132. Histogram.prototype.minimum = function() {
  133. return this.min_seen;
  134. };
  135. /**
  136. * Get the number of all added values
  137. * @return {number} The count
  138. */
  139. Histogram.prototype.getCount = function() {
  140. return this.count;
  141. };
  142. /**
  143. * Get the sum of all added values
  144. * @return {number} The sum
  145. */
  146. Histogram.prototype.getSum = function() {
  147. return this.sum;
  148. };
  149. /**
  150. * Get the sum of squares of all added values
  151. * @return {number} The sum of squares
  152. */
  153. Histogram.prototype.sumOfSquares = function() {
  154. return this.sum_of_squares;
  155. };
  156. /**
  157. * Get the raw histogram as a list of bucket sizes
  158. * @return {Array.<number>} The buckets
  159. */
  160. Histogram.prototype.getContents = function() {
  161. return this.buckets;
  162. };
  163. module.exports = Histogram;