Skip to content

Commit 455f6f3

Browse files
committed
Added benchmarks
1 parent a9f7c89 commit 455f6f3

File tree

8 files changed

+101
-6
lines changed

8 files changed

+101
-6
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
# diag files
61+
*clinic*
62+
*0x*
63+
64+
# lock files
65+
66+
package-lock.json

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const server = Fastify()
2626

2727
server.register(proxy, {
2828
upstream,
29-
prefix: '/upstream' // optional
29+
prefix: '/upstream', // optional
30+
http2: false // optional
3031
})
3132

3233
server.listen(3000)
@@ -53,11 +54,20 @@ The prefix to mount this plugin on. This is provided by fastify itself.
5354
A `beforeHandler` to be applied on all routes. Useful for performing
5455
authentication.
5556

57+
### http2
58+
59+
A `beforeHandler` to be applied on all routes. Useful for performing
60+
authentication.
61+
62+
## Benchmarks
63+
64+
The following benchmarks where generate on a Macbook 2018 with i5 and
65+
8GB of RAM.
66+
5667
## TODO
5768

5869
* [ ] Generate unique request ids and implement request tracking
5970
* [ ] Perform validations for incoming data
60-
* [ ] Benchmarks
6171

6272
## License
6373

benchmarks/express-http-proxy.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
var proxy = require('express-http-proxy')
4+
var app = require('express')()
5+
6+
app.use('/', proxy('localhost:3001'))
7+
8+
app.listen(3000)

benchmarks/fastify-proxy.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const Fastify = require('fastify')
4+
const proxy = require('..')
5+
6+
async function startProxy (upstream) {
7+
const server = Fastify()
8+
server.register(proxy, {
9+
upstream,
10+
http2: !!process.env.HTTP2
11+
})
12+
13+
await server.listen(3000)
14+
return server
15+
}
16+
17+
startProxy('http://localhost:3001')

benchmarks/http-proxy.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
var httpProxy = require('http-proxy')
4+
var { Agent } = require('http')
5+
6+
var proxy = httpProxy.createProxyServer({
7+
target: 'http://localhost:3001',
8+
agent: new Agent({
9+
keepAlive: true,
10+
keepAliveMsecs: 60 * 1000, // 1 minute
11+
maxSockets: 2048,
12+
maxFreeSockets: 2048
13+
})
14+
})
15+
16+
proxy.on('error', function (e) {
17+
console.log(e)
18+
})
19+
20+
proxy.listen(3000)

benchmarks/origin.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
const Fastify = require('fastify')
4+
5+
async function startOrigin () {
6+
const origin = Fastify({
7+
http2: !!process.env.HTTP2
8+
})
9+
origin.get('/', async (request, reply) => {
10+
return 'this is root'
11+
})
12+
13+
origin.get('/a', async (request, reply) => {
14+
return 'this is a'
15+
})
16+
17+
origin.post('/this-has-data', async (request, reply) => {
18+
if (request.body.hello === 'world') {
19+
return { something: 'posted' }
20+
}
21+
throw new Error('kaboom')
22+
})
23+
24+
await origin.listen(3001)
25+
26+
return origin
27+
}
28+
29+
startOrigin()

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = async function (fastify, opts) {
1010
const beforeHandler = opts.beforeHandler
1111

1212
fastify.register(From, {
13-
base: opts.upstream
13+
base: opts.upstream,
14+
http2: opts.http2
1415
})
1516

1617
fastify.addContentTypeParser('application/json', bodyParser)

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@
2222
},
2323
"homepage": "https://github.com/fastify/fastify-http-proxy#readme",
2424
"devDependencies": {
25-
"fastify": "^1.4.0",
25+
"express": "^4.16.3",
26+
"express-http-proxy": "^1.2.0",
27+
"fastify": "^1.5.0",
2628
"got": "^8.3.1",
2729
"http-errors": "^1.6.3",
30+
"http-proxy": "^1.17.0",
2831
"make-promises-safe": "^1.1.0",
2932
"pre-commit": "^1.2.2",
30-
"simple-get": "^2.8.1",
33+
"simple-get": "^3.0.0",
3134
"snazzy": "^7.1.1",
3235
"standard": "^11.0.0",
3336
"tap": "^12.0.0"
3437
},
3538
"dependencies": {
36-
"fastify-reply-from": "^0.4.1"
39+
"fastify-reply-from": "^0.4.2"
3740
}
3841
}

0 commit comments

Comments
 (0)