-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
init-context.js
45 lines (38 loc) · 1002 Bytes
/
init-context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict'
const http = require('http')
const autocannon = require('../autocannon')
const server = http.createServer(handle)
server.listen(0, startBench)
function handle (req, res) {
const body = []
// this route handler simply returns whatever it gets from response
req
.on('data', chunk => body.push(chunk))
.on('end', () => res.end(Buffer.concat(body)))
}
function startBench () {
const url = 'http://localhost:' + server.address().port
autocannon({
url,
connections: 1,
amount: 1,
initialContext: { user: { firstName: 'Salman' } },
requests: [
{
// use data from context
method: 'PUT',
setupRequest: (req, context) => ({
...req,
path: `/user/${context.user.id}`,
body: JSON.stringify({
...context.user,
lastName: 'Mitha'
})
})
}
]
}, finishedBench)
function finishedBench (err, res) {
console.log('finished bench', err, res)
}
}