Skip to content

Commit

Permalink
Enable WASM for HdrHistogramJS (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvictoor committed Sep 15, 2020
1 parent 10fa41d commit fd09f49
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ function _run (opts, cb, tracker) {
return tracker
}

hdr.initWebAssemblySync()

const latencies = hdr.build({
useWebAssembly: true,
bitBucketSize: 64,
autoResize: true,
lowestDiscernibleValue: 1,
Expand All @@ -45,6 +48,7 @@ function _run (opts, cb, tracker) {
})

const requests = hdr.build({
useWebAssembly: true,
bitBucketSize: 64,
autoResize: true,
lowestDiscernibleValue: 1,
Expand All @@ -53,6 +57,7 @@ function _run (opts, cb, tracker) {
})

const throughput = hdr.build({
useWebAssembly: true,
bitBucketSize: 64,
autoResize: true,
lowestDiscernibleValue: 1,
Expand Down Expand Up @@ -190,12 +195,17 @@ function _run (opts, cb, tracker) {
result.latency.totalCount = latencies.totalCount
result.requests.sent = totalRequests
statusCodes.forEach((code, index) => { result[(index + 1) + 'xx'] = code })
if (result.requests.min === Number.MAX_SAFE_INTEGER) result.requests.min = 0
if (result.throughput.min === Number.MAX_SAFE_INTEGER) result.throughput.min = 0
if (result.latency.min === Number.MAX_SAFE_INTEGER) result.latency.min = 0
if (result.requests.min >= Number.MAX_SAFE_INTEGER) result.requests.min = 0
if (result.throughput.min >= Number.MAX_SAFE_INTEGER) result.throughput.min = 0
if (result.latency.min >= Number.MAX_SAFE_INTEGER) result.latency.min = 0

tracker.emit('done', result)
if (!opts.forever) cb(null, result)
if (!opts.forever) {
latencies.destroy()
requests.destroy()
throughput.destroy()
cb(null, result)
}

// the restart function
setImmediate(() => {
Expand Down
34 changes: 34 additions & 0 deletions test/serial/wasm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const test = require('tap').test
const run = require('../../lib/run')
const helper = require('../helper')

test('should clean up HdrHistogram WASM memory at each run', async (t) => {
const server = helper.startServer()
const runTwentyTimes = (resolve, reject, numberOfRuns = 0) => {
run({
url: 'http://localhost:' + server.address().port,
connections: 1,
amount: 1
}, (result) => {
// should get error " url or socketPath option required"
// we can ignore this error, we just want run() to execute
// and to instantiate new WASM histograms
if (numberOfRuns < 20) {
runTwentyTimes(resolve, reject, ++numberOfRuns)
} else {
resolve()
}
})
}
const lotsOfRuns = []
for (let index = 0; index < 50; index++) {
lotsOfRuns.push(new Promise(runTwentyTimes))
}

await Promise.all(lotsOfRuns)

// if the process has not crashed, we are good \o/
t.end()
})

0 comments on commit fd09f49

Please sign in to comment.