Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expectbody/assertions #377

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ Available options:
-E/--expectBody EXPECTED
Ensure the body matches this value. If enabled, mismatches count towards bailout.
Enabling this option will slow down the load testing.
-N/--expectDuration EXPECTED
Ensure the response duration less than expectDuration. If bigger, reqOverTimes count towards bailout.
Enabling this option will slow down the load testing.
-P/--expectBodySize EXPECTED
Ensure the response body bytes less than expectBodySize. If bigger, reqOverSizes count towards bailout.
Enabling this option will slow down the load testing.
--renderStatusCodes
Print status codes and their respective statistics.
--debug
Expand Down
2 changes: 2 additions & 0 deletions autocannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const alias = {
socketPath: 'S',
excludeErrorStats: 'x',
expectBody: 'E',
expectDuration: 'N',
expectBodySize: 'P',
workers: 'w',
warmup: 'W',
help: 'h'
Expand Down
4 changes: 4 additions & 0 deletions lib/aggregateResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function aggregateResult (results, opts, histograms) {
acc.errors += r.errors
acc.timeouts += r.timeouts
acc.mismatches += r.mismatches
acc.reqOverTimes += r.reqOverTimes
acc.reqOverSizes += r.reqOverSizes
acc.non2xx += r.non2xx
acc.resets += r.resets
acc['1xx'] += r['1xx']
Expand Down Expand Up @@ -53,6 +55,8 @@ function aggregateResult (results, opts, histograms) {
errors: aggregated.errors,
timeouts: aggregated.timeouts,
mismatches: aggregated.mismatches,
reqOverTimes: aggregated.reqOverTimes,
reqOverSizes: aggregated.reqOverSizes,
non2xx: aggregated.non2xx,
resets: aggregated.resets,
'1xx': aggregated['1xx'],
Expand Down
12 changes: 11 additions & 1 deletion lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function Client (opts) {
this.opts.pipelining = this.opts.pipelining || 1
this.opts.port = this.opts.port || 80
this.opts.expectBody = this.opts.expectBody || null
this.opts.expectDuration = this.opts.expectDuration || Infinity
this.opts.expectBodySize = this.opts.expectBodySize || Infinity
this.opts.tlsOptions = this.opts.tlsOptions || {}
this.timeout = (this.opts.timeout || 10) * 1000
this.ipc = !!this.opts.socketPath
Expand Down Expand Up @@ -90,11 +92,19 @@ function Client (opts) {
if (!this.destroyed && this.reconnectRate && this.reqsMade % this.reconnectRate === 0) {
return this._resetConnection()
}

if (!this.destroyed) {
if (this.opts.expectBody && this.opts.expectBody !== resp.body) {
if (this.opts.expectBody && !resp.body.includes(this.opts.expectBody)) {
return this.emit('mismatch', resp.body)
}

if (this.opts.expectBodyDuration && resp.duration > this.opts.expectBodyDuration) {
return this.emit('reqOverTime', resp.duration)
}
if (this.opts.expectBodySize && resp.bytes > this.opts.expectBodySize) {
return this.emit('reqOverSize', resp.bytes)
}

this.requestIterator.recordBody(resp.req, resp.headers.statusCode, resp.body, resp.headers.headers)

this.emit('response', resp.headers.statusCode, resp.bytes, resp.duration, this.rate)
Expand Down
10 changes: 10 additions & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function run (opts, tracker, cb) {
let errors = 0
let timeouts = 0
let mismatches = 0
let reqOverTimes = 0
let reqOverSizes = 0
let totalBytes = 0
let totalRequests = 0
let totalCompletedRequests = 0
Expand Down Expand Up @@ -75,6 +77,8 @@ function run (opts, tracker, cb) {
url.socketPath = opts.socketPath
url.servername = opts.servername
url.expectBody = opts.expectBody
url.expectDuration = opts.expectDuration
url.expectBodySize = opts.expectBodySize

return url
})
Expand Down Expand Up @@ -117,6 +121,8 @@ function run (opts, tracker, cb) {
errors: errors,
timeouts: timeouts,
mismatches: mismatches,
reqOverSizes,
reqOverTimes,
non2xx: statusCodes[0] + statusCodes[2] + statusCodes[3] + statusCodes[4],
statusCodeStats,
resets: resets,
Expand Down Expand Up @@ -148,6 +154,8 @@ function run (opts, tracker, cb) {
errors = 0
timeouts = 0
mismatches = 0
reqOverTimes = 0
reqOverSizes = 0
totalBytes = 0
totalRequests = 0
totalCompletedRequests = 0
Expand Down Expand Up @@ -201,6 +209,8 @@ function run (opts, tracker, cb) {
client.on('response', onResponse)
client.on('connError', onError)
client.on('mismatch', onExpectMismatch)
client.on('reqOverTime', () => { reqOverTimes++ })
client.on('reqOverSize', () => { reqOverSizes++ })
client.on('reset', () => { resets++ })
client.on('timeout', onTimeout)
client.on('request', () => { totalRequests++ })
Expand Down
Loading