Skip to content

Commit

Permalink
Merge pull request #116 from mcollina/js-hdr
Browse files Browse the repository at this point in the history
Removes the binary dependency
  • Loading branch information
GlenTiki authored Jan 29, 2018
2 parents 7b9a7c0 + 8b314a4 commit dc569bc
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 82 deletions.
11 changes: 2 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,5 @@ node_js:
- "4"
- "6"
- "7"
env:
- CC=gcc-4.8 CXX=g++-4.8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-4.8
- g++-4.8
- "8"
- "9"
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ or if you want to use the [API](#api) or as a dependency:
npm i autocannon --save
```

### Supported systems

**autocannon** is supported on Linux, Mac OS X and Windows.
If you see any errors during installation, you might need to configure
your system to compile native addons:
follow the instructions at [node-gyp][node-gyp].

## Usage

### Command Line
Expand Down
18 changes: 12 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
environment:
matrix:
- nodejs_version: 4.0.0
- nodejs_version: 4.8.7
platform: x64
msvs_toolset: 12
- nodejs_version: 4.0.0
- nodejs_version: 4.8.7
platform: x86
msvs_toolset: 12
- nodejs_version: 5.0.0
- nodejs_version: 6.12.3
platform: x64
msvs_toolset: 12
- nodejs_version: 5.0.0
- nodejs_version: 6.12.3
platform: x86
msvs_toolset: 12
- nodejs_version: 6.0.0
- nodejs_version: 8.9.4
platform: x64
msvs_toolset: 12
- nodejs_version: 6.0.0
- nodejs_version: 8.9.4
platform: x86
msvs_toolset: 12
- nodejs_version: 9.4.0
platform: x64
msvs_toolset: 12
- nodejs_version: 9.4.0
platform: x86
msvs_toolset: 12

Expand Down
9 changes: 5 additions & 4 deletions lib/httpRequestBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const xtend = require('xtend')
const methods = require('./httpMethods')
const ending = Buffer.from('\r\n')

// this is a build request factory, that curries the build request function
// and sets the default for it
Expand All @@ -12,7 +13,7 @@ function requestBuilder (defaults) {
method: 'GET',
path: '/',
headers: {},
body: new Buffer(0),
body: Buffer.alloc(0),
hostname: 'localhost',
port: 80
}
Expand Down Expand Up @@ -52,7 +53,7 @@ function requestBuilder (defaults) {
let bodyBuf

if (typeof body === 'string') {
bodyBuf = new Buffer(body)
bodyBuf = Buffer.from(body)
} else if (Buffer.isBuffer(body)) {
bodyBuf = body
} else if (body) {
Expand All @@ -70,10 +71,10 @@ function requestBuilder (defaults) {
.map((key) => `${key}: ${headers[key]}\r\n`)
.reduce((acc, str) => acc + str, baseReq)

req = new Buffer(req + '\r\n', 'utf8')
req = Buffer.from(req + '\r\n', 'utf8')

if (bodyBuf && bodyBuf.length > 0) {
req = Buffer.concat([req, bodyBuf, new Buffer('\r\n')])
req = Buffer.concat([req, bodyBuf, ending])
}

return req
Expand Down
2 changes: 1 addition & 1 deletion lib/progressTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function asRow (name, stat) {
name,
stat.average,
stat.stddev,
stat.max
Math.floor(stat.max * 100) / 100
]
}

Expand Down
6 changes: 3 additions & 3 deletions lib/requestIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RequestIterator.prototype.move = function () {
let ret = this.currentRequest.requestBuffer
this.nextRequest()
return this.reqDefaults.idReplacement
? new Buffer(ret.toString().replace(/\[<id>\]/g, hyperid()))
? Buffer.from(ret.toString().replace(/\[<id>\]/g, hyperid()))
: ret
}

Expand All @@ -57,13 +57,13 @@ RequestIterator.prototype.setHeaders = function (newHeaders) {
}

RequestIterator.prototype.setBody = function (newBody) {
this.currentRequest.body = newBody || new Buffer(0)
this.currentRequest.body = newBody || Buffer.alloc(0)
this.rebuildRequest()
}

RequestIterator.prototype.setHeadersAndBody = function (newHeaders, newBody) {
this.currentRequest.headers = newHeaders || {}
this.currentRequest.body = newBody || new Buffer(0)
this.currentRequest.body = newBody || Buffer.alloc(0)
this.rebuildRequest()
}

Expand Down
39 changes: 30 additions & 9 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const EE = require('events').EventEmitter
const URL = require('url')
const Histogram = require('native-hdr-histogram')
const hdr = require('hdr-histogram-js')
const timestring = require('timestring')
const Client = require('./httpClient')
const xtend = require('xtend')
Expand All @@ -13,7 +13,7 @@ const addPercentiles = histUtil.addPercentiles

const defaultOptions = {
headers: {},
body: new Buffer(0),
body: Buffer.alloc(0),
method: 'GET',
duration: 10,
connections: 10,
Expand All @@ -37,9 +37,30 @@ function run (opts, cb) {

const tracker = new EE()

const latencies = new Histogram(1, 10000, 5)
const requests = new Histogram(1, 1000000, 3)
const throughput = new Histogram(1, 100000000000, 1)
const latencies = hdr.build({
bitBucketSize: 64,
autoResize: true,
lowestDiscernibleValue: 1,
highestTrackableValue: 10000,
numberOfSignificantValueDigits: 5
})

const requests = hdr.build({
bitBucketSize: 64,
autoResize: true,
lowestDiscernibleValue: 1,
highestTrackableValue: 1000000,
numberOfSignificantValueDigits: 3
})

const throughput = hdr.build({
bitBucketSize: 64,
autoResize: true,
lowestDiscernibleValue: 1,
highestTrackableValue: 100000000000,
numberOfSignificantValueDigits: 1
})

const statusCodes = [
0, // 1xx
0, // 2xx
Expand Down Expand Up @@ -109,8 +130,8 @@ function run (opts, cb) {
function tickInterval () {
totalBytes += bytes
totalCompletedRequests += counter
requests.record(counter)
throughput.record(bytes)
requests.recordValue(counter)
throughput.recordValue(bytes)
counter = 0
bytes = 0
tracker.emit('tick')
Expand Down Expand Up @@ -201,8 +222,8 @@ function run (opts, cb) {
tracker.emit('response', this, statusCode, resBytes, responseTime)
const codeIndex = Math.floor(parseInt(statusCode) / 100) - 1
statusCodes[codeIndex] += 1
// only record 2xx latencies
if (codeIndex === 1) latencies.record(responseTime)
// only recordValue 2xx latencies
if (codeIndex === 1) latencies.recordValue(responseTime)
bytes += resBytes
counter++
}
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@
"bl": "^1.1.2",
"pre-commit": "^1.1.2",
"split2": "^2.1.0",
"standard": "^9.0.0",
"tap": "^10.3.0"
"standard": "^10.0.0",
"tap": "^11.0.0"
},
"dependencies": {
"chalk": "^1.1.3",
"chalk": "^2.0.0",
"color-support": "^1.1.1",
"deep-extend": "^0.4.1",
"hdr-histogram-percentiles-obj": "^1.1.0",
"deep-extend": "^0.5.0",
"hdr-histogram-js": "^1.0.0",
"hdr-histogram-percentiles-obj": "^1.2.0",
"http-parser-js": "^0.4.2",
"hyperid": "^1.1.0",
"minimist": "^1.2.0",
"native-hdr-histogram": "^0.4.0",
"pretty-bytes": "^4.0.2",
"progress": "^1.1.8",
"progress": "^2.0.0",
"reinterval": "^1.1.0",
"retimer": "^1.0.1",
"table": "^4.0.1",
"timestring": "^3.0.1",
"timestring": "^5.0.0",
"xtend": "^4.0.1"
}
}
30 changes: 15 additions & 15 deletions test/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ test('client supports sending a body', (t) => {

const opts = server.address()
opts.method = 'POST'
opts.body = new Buffer('hello world')
opts.body = Buffer.from('hello world')

const client = new Client(opts)

Expand Down Expand Up @@ -181,13 +181,13 @@ test('client supports changing the body', (t) => {
const client = new Client(opts)

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'request is okay before modifying')

client.setBody('modified')

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'body changes updated request')
client.destroy()
})
Expand All @@ -200,13 +200,13 @@ test('client supports changing the headers', (t) => {

const client = new Client(opts)
t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\n\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\n\r\n`),
'request is okay before modifying')

client.setHeaders({header: 'modified'})

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modified\r\n\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modified\r\n\r\n`),
'header changes updated request')
client.destroy()
})
Expand All @@ -221,14 +221,14 @@ test('client supports changing the headers and body', (t) => {
const client = new Client(opts)

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'request is okay before modifying')

client.setBody('modified')
client.setHeaders({header: 'modifiedHeader'})

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'changes updated request')
client.destroy()
})
Expand All @@ -243,13 +243,13 @@ test('client supports changing the headers and body together', (t) => {
const client = new Client(opts)

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'request is okay before modifying')

client.setHeadersAndBody({header: 'modifiedHeader'}, 'modified')

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'changes updated request')
client.destroy()
})
Expand All @@ -264,13 +264,13 @@ test('client supports updating the current request object', (t) => {
const client = new Client(opts)

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'request is okay before modifying')

client.setRequest({ headers: { header: 'modifiedHeader' }, body: 'modified', method: 'GET' })

t.same(client.getRequestBuffer(),
new Buffer(`GET / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
Buffer.from(`GET / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'changes updated request')
client.destroy()
})
Expand All @@ -292,11 +292,11 @@ test('client customiseRequest function overwrites the headers and body', (t) =>
const client = new Client(opts)

t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nheader: modifiedHeader\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'changes updated request')

t.notSame(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'changes updated request')

client.destroy()
Expand Down Expand Up @@ -362,7 +362,7 @@ test('client should have 2 different requests it iterates over', (t) => {
number++
if (number === 1 || number === 3) {
t.same(client.getRequestBuffer(),
new Buffer(`GET / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
Buffer.from(`GET / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 8\r\n\r\nmodified\r\n`),
'body changes updated request')

if (number === 3) {
Expand All @@ -371,7 +371,7 @@ test('client should have 2 different requests it iterates over', (t) => {
}
} else {
t.same(client.getRequestBuffer(),
new Buffer(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
Buffer.from(`POST / HTTP/1.1\r\nHost: localhost:${server.address().port}\r\nConnection: keep-alive\r\nContent-Length: 11\r\n\r\nhello world\r\n`),
'request was okay')
}
})
Expand Down
Loading

0 comments on commit dc569bc

Please sign in to comment.