hdr-js.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict'
  2. const test = require('tap').test
  3. const hdr = require('hdr-histogram-js')
  4. const histPercentileObj = require('../')
  5. test('should return a valid hist as object', (t) => {
  6. t.plan(24)
  7. const histogram = hdr.build({
  8. lowestDiscernibleValue: 1,
  9. highestTrackableValue: 100
  10. })
  11. let total = 0
  12. for (let i = 0; i < 10000; i++) {
  13. const num = Math.floor(Math.random() * 100)
  14. histogram.recordValue(num)
  15. total += num
  16. }
  17. // any of the numbers below _could_ be 0, so we do a type test instead of t.ok
  18. const result = histPercentileObj.histAsObj(histogram, total)
  19. t.ok(result)
  20. t.type(result.average, 'number')
  21. t.type(result.mean, 'number')
  22. t.type(result.stddev, 'number')
  23. t.type(result.min, 'number')
  24. t.type(result.max, 'number')
  25. t.type(result.total, 'number')
  26. const withPercentiles = histPercentileObj.addPercentiles(histogram, result)
  27. t.ok(withPercentiles)
  28. t.type(withPercentiles.average, 'number')
  29. t.type(withPercentiles.p0_001, 'number')
  30. t.type(withPercentiles.p0_01, 'number')
  31. t.type(withPercentiles.p0_1, 'number')
  32. t.type(withPercentiles.p1, 'number')
  33. t.type(withPercentiles.p2_5, 'number')
  34. t.type(withPercentiles.p10, 'number')
  35. t.type(withPercentiles.p25, 'number')
  36. t.type(withPercentiles.p50, 'number')
  37. t.type(withPercentiles.p75, 'number')
  38. t.type(withPercentiles.p90, 'number')
  39. t.type(withPercentiles.p97_5, 'number')
  40. t.type(withPercentiles.p99, 'number')
  41. t.type(withPercentiles.p99_9, 'number')
  42. t.type(withPercentiles.p99_99, 'number')
  43. t.type(withPercentiles.p99_999, 'number')
  44. })
  45. test('should return expected numbers', (t) => {
  46. t.plan(18)
  47. const histogram = hdr.build({
  48. lowestDiscernibleValue: 1,
  49. highestTrackableValue: 10
  50. })
  51. histogram.recordValue(4)
  52. histogram.recordValue(5)
  53. histogram.recordValue(6)
  54. // any of the numbers below _could_ be 0, so we do a type test instead of t.ok
  55. const result = histPercentileObj.histAsObj(histogram, 15)
  56. t.ok(result)
  57. t.equal(result.average, 5)
  58. t.equal(result.mean, 5)
  59. t.equal(result.stddev, 0.82)
  60. t.equal(result.min, 4)
  61. t.equal(result.max, 6)
  62. t.equal(result.total, 15)
  63. const withPercentiles = histPercentileObj.addPercentiles(histogram, result)
  64. t.ok(withPercentiles)
  65. t.equal(withPercentiles.average, 5)
  66. t.equal(withPercentiles.p2_5, 4)
  67. t.equal(withPercentiles.p50, 5)
  68. t.equal(withPercentiles.p75, 6)
  69. t.equal(withPercentiles.p90, 6)
  70. t.equal(withPercentiles.p97_5, 6)
  71. t.equal(withPercentiles.p99, 6)
  72. t.equal(withPercentiles.p99_9, 6)
  73. t.equal(withPercentiles.p99_99, 6)
  74. t.equal(withPercentiles.p99_999, 6)
  75. })
  76. test('should return a valid hist as object from a WASM histogram', (t) => {
  77. t.plan(1)
  78. hdr.initWebAssemblySync()
  79. const histogram = hdr.build({
  80. useWebAssembly: true
  81. })
  82. t.ok(histPercentileObj.histAsObj(histogram))
  83. })