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

chore: sample for multi nodes #270

Merged
merged 2 commits into from Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -221,7 +221,7 @@ async function foo () {
Start autocannon against the given target.

* `opts`: Configuration options for the autocannon instance. This can have the following attributes. _REQUIRED_.
* `url`: The given target. Can be http or https. _REQUIRED_.
* `url`: The given target. Can be http or https. More than one url is allowed, but it is recommended that the number of connections be an integer multiple of the url. _REQUIRED_.
* `socketPath`: A path to a Unix Domain Socket or a Windows Named Pipe. A `url` is still required in order to send the correct Host header and path. _OPTIONAL_.
* `connections`: The number of concurrent connections. _OPTIONAL_ default: `10`.
* `duration`: The number of seconds to run the autocannon. Can be a [timestring](https://www.npmjs.com/package/timestring). _OPTIONAL_ default: `10`.
Expand Down
44 changes: 44 additions & 0 deletions samples/bench-multi-url.js
@@ -0,0 +1,44 @@
'use strict'

const http = require('http')
const autocannon = require('autocannon')

function createHandler (serverName) {
return function (req, res) {
console.log(serverName + ' received request')
res.end('hello world')
}
}

const server1 = http.createServer(createHandler('server1'))
const server2 = http.createServer(createHandler('server2'))

server1.listen(0, startBench)
server2.listen(0, startBench)

function startBench () {
const url = [
'http://localhost:' + server1.address().port,
'http://localhost:' + server2.address().port
]

// same with run the follow command in cli
// autocannon -d 10 -c 2 http://localhost:xxxx http://localhost:yyyy
autocannon({
url: url,
// connection number should n times of the number of server
connections: 2,
duration: 10,
requests: [
{
method: 'GET',
path: '/'
}
]
}, finishedBench)

function finishedBench (err, res) {
console.log('finished bench', err, res)
process.exit(1)
}
}