mcollina / autocannon

fast HTTP/1.1 benchmarking tool written in Node.js
MIT License
7.86k stars 326 forks source link

Cannot specify body for `PUT` request via CLI #493

Closed jsumners closed 1 year ago

jsumners commented 1 year ago

Given the index.js:

'use strict'

const fastify = require('fastify')()

fastify.addContentTypeParser(
  'application/x-www-form-urlencoded',
  { parseAs: 'string' },
  fastify.defaultTextParser
)

fastify.route({
  method: 'put',
  path: '/put',
  handler (req, res) {
    res.send(req.body)
  }
})

fastify.listen({ port: 8000 })

Start the server and then run:

npx autocannon -d 10 -c 30 -w 3 -m PUT -H content-type="application/x-www-form-urlencoded" -b "1" 127.0.0.1:8000/put

The result will be:

Running 10s test @ http://127.0.0.1:8000/put
30 connections
3 workers

Worker error: Error: body must be either a string or a buffer
    at RequestIterator.buildRequest [as requestBuilder] (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/httpRequestBuilder.js:83:13)
    at RequestIterator.rebuildRequest (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:108:17)
    at /Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:74:12
    at Array.forEach (<anonymous>)
    at RequestIterator.setRequests (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:71:17)
    at new RequestIterator (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:35:8)
    at new Client (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/httpClient.js:36:26)
    at initialiseClients (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/run.js:210:22)
    at run (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/run.js:86:3)
    at runTracker (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/worker.js:29:19)
body must be either a string or a buffer
Worker error: Error: body must be either a string or a buffer
    at RequestIterator.buildRequest [as requestBuilder] (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/httpRequestBuilder.js:83:13)
    at RequestIterator.rebuildRequest (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:108:17)
    at /Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:74:12
    at Array.forEach (<anonymous>)
    at RequestIterator.setRequests (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:71:17)
    at new RequestIterator (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:35:8)
    at new Client (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/httpClient.js:36:26)
    at initialiseClients (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/run.js:210:22)
    at run (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/run.js:86:3)
    at runTracker (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/worker.js:29:19)
Worker error: Error: body must be either a string or a buffer
    at RequestIterator.buildRequest [as requestBuilder] (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/httpRequestBuilder.js:83:13)
    at RequestIterator.rebuildRequest (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:108:17)
    at /Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:74:12
    at Array.forEach (<anonymous>)
    at RequestIterator.setRequests (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:71:17)
    at new RequestIterator (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/requestIterator.js:35:8)
    at new Client (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/httpClient.js:36:26)
    at initialiseClients (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/run.js:210:22)
    at run (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/run.js:86:3)
    at runTracker (/Users/jsumners/.npm/_npx/0192e65d342ecc83/node_modules/autocannon/lib/worker.js:29:19)

If we alter the autocanon command to:

npx autocannon -d 10 -c 30 -m PUT -H content-type="application/x-www-form-urlencoded" -b "1" 127.0.0.1:8000/put
# Note the removal of "-w 3"

Then we get simply:

body must be either a string or a buffer
mcollina commented 1 year ago

A fix for this would be awesome ;).

jsumners commented 1 year ago

https://github.com/mcollina/autocannon/pull/494