index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict'
  2. const percentiles = module.exports.percentiles = [
  3. 0.001,
  4. 0.01,
  5. 0.1,
  6. 1,
  7. 2.5,
  8. 10,
  9. 25,
  10. 50,
  11. 75,
  12. 90,
  13. 97.5,
  14. 99,
  15. 99.9,
  16. 99.99,
  17. 99.999
  18. ]
  19. module.exports.histAsObj = function (hist, total) {
  20. const mean = Math.ceil(getMean(hist) * 100) / 100
  21. const result = {
  22. average: mean, // added for backward compat with wrk
  23. mean: mean,
  24. stddev: Math.ceil(getStdDeviation(hist) * 100) / 100,
  25. min: getMin(hist),
  26. max: getMax(hist)
  27. }
  28. if (typeof total === 'number') {
  29. result.total = total
  30. }
  31. return result
  32. }
  33. module.exports.addPercentiles = function (hist, result) {
  34. percentiles.forEach(function (perc) {
  35. const key = ('p' + perc).replace('.', '_')
  36. if (typeof hist.percentile === 'function') {
  37. result[key] = hist.percentile(perc)
  38. } else if (typeof hist.getValueAtPercentile === 'function') {
  39. result[key] = hist.getValueAtPercentile(perc)
  40. }
  41. })
  42. return result
  43. }
  44. function getMean (hist) {
  45. if (typeof hist.mean === 'function') {
  46. return hist.mean()
  47. }
  48. if (typeof hist.getMean === 'function') {
  49. return hist.getMean()
  50. }
  51. return hist.mean
  52. }
  53. function getMin (hist) {
  54. if (typeof hist.min === 'function') {
  55. return hist.min()
  56. }
  57. return hist.minNonZeroValue
  58. }
  59. function getMax (hist) {
  60. if (typeof hist.max === 'function') {
  61. return hist.max()
  62. }
  63. return hist.maxValue
  64. }
  65. function getStdDeviation (hist) {
  66. if (typeof hist.stddev === 'function') {
  67. return hist.stddev()
  68. }
  69. if (typeof hist.getStdDeviation === 'function') {
  70. return hist.getStdDeviation()
  71. }
  72. return hist.stdDeviation
  73. }