Skip to content

Commit 4b8ec72

Browse files
committed
Fork all bench servers into child processes
1 parent 98a7a4c commit 4b8ec72

2 files changed

Lines changed: 58 additions & 38 deletions

File tree

bench/index.js

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
'use strict'
22

3-
const http = require('http')
3+
const { fork } = require('child_process')
4+
const { join } = require('path')
45
const getScrud = require('get-scrud')
5-
const polka = require('polka')
6-
const fastify = require('fastify')()
7-
const scrud = require('./../index')
8-
const express = require('express')
96
const autocannon = require('autocannon')
107
const table = require('tty-table')
118
const ports = {http: 3010, fastify: 3011, polka: 3012, scrud: 3013, express: 3014}
12-
const ready = {}
13-
Object.keys(ports).forEach((k) => { ready[k] = false })
14-
const logStart = (n) => {
15-
ready[n] = true
16-
if (Object.keys(ready).every((k) => ready[k])) bench()
17-
}
189
const results = []
1910
const benchId = 301
11+
Promise.all(Object.keys(ports).map((k) => new Promise((resolve, reject) => {
12+
let child = fork(join(__dirname, 'server'), [k])
13+
child.once('error', (err) => {
14+
console.log(err)
15+
process.exit()
16+
})
17+
child.once('message', (m) => {
18+
(m === k) ? resolve() : reject(m)
19+
})
20+
}))).then(() => bench())
2021

2122
const urlTemplate = (port, string) => {
2223
let url = {host: 'localhost', port, path: `/bench/${benchId}`}
@@ -42,32 +43,6 @@ const formatBytes = (bytes) => {
4243
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
4344
}
4445

45-
// HTTP
46-
http.createServer((req, res) => {
47-
res.setHeader('Content-Type', 'application/json')
48-
res.end(JSON.stringify({data: `${benchId}`, error: null}))
49-
}).listen(ports.http, () => logStart('http'))
50-
51-
// FASTIFY
52-
fastify.get('/bench/:id', function (req, reply) {
53-
reply.send({data: `${req.params.id}`, error: null})
54-
})
55-
fastify.listen(ports.fastify, () => logStart('fastify'))
56-
57-
// POLKA
58-
polka().get('/bench/:id', (req, res) => {
59-
res.end(JSON.stringify({data: `${req.params.id}`, error: null}))
60-
}).listen(ports.polka).then(() => logStart('polka'))
61-
62-
// SCRUD
63-
scrud.register('bench', {read: (req, res) => scrud.sendData(res, `${req.id}`)})
64-
scrud.start({port: ports.scrud}).then(() => logStart('scrud'))
65-
66-
// EXPRESS
67-
express().get('/bench/:id', (req, res) => {
68-
res.end(JSON.stringify({data: `${req.params.id}`, error: null}))
69-
}).listen(ports.express, () => logStart('express'))
70-
7146
// benchamrks
7247
const bencher = (title) => new Promise((resolve, reject) => {
7348
console.log(`benchmarking ${title}`)
@@ -77,7 +52,12 @@ const bencher = (title) => new Promise((resolve, reject) => {
7752
results.push(res)
7853
return resolve(title)
7954
}
80-
let acOpts = {url: urlTemplate(port, true), title, connections: 50}
55+
let acOpts = {
56+
url: urlTemplate(port, true),
57+
title,
58+
connections: 50,
59+
pipelining: 10
60+
}
8161
autocannon(Object.assign({duration: 3}, acOpts), () => {
8262
autocannon(Object.assign({duration: 7}, acOpts), done)
8363
})

bench/server.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const ports = {http: 3010, fastify: 3011, polka: 3012, scrud: 3013, express: 3014}
4+
const logStart = (n) => process.send(n)
5+
const benchId = 301
6+
const start = {
7+
http: () => {
8+
const http = require('http')
9+
http.createServer((req, res) => {
10+
res.setHeader('Content-Type', 'application/json')
11+
res.end(JSON.stringify({data: `${benchId}`, error: null}))
12+
}).listen(ports.http, () => logStart('http'))
13+
},
14+
fastify: () => {
15+
const fastify = require('fastify')()
16+
fastify.get('/bench/:id', function (req, reply) {
17+
reply.send({data: `${req.params.id}`, error: null})
18+
})
19+
fastify.listen(ports.fastify, () => logStart('fastify'))
20+
},
21+
polka: () => {
22+
const polka = require('polka')
23+
polka().get('/bench/:id', (req, res) => {
24+
res.end(JSON.stringify({data: `${req.params.id}`, error: null}))
25+
}).listen(ports.polka).then(() => logStart('polka'))
26+
},
27+
scrud: () => {
28+
const scrud = require('./../index')
29+
scrud.register('bench', {read: (req, res) => scrud.sendData(res, `${req.id}`)})
30+
scrud.start({port: ports.scrud}).then(() => logStart('scrud'))
31+
},
32+
express: () => {
33+
const express = require('express')
34+
express().get('/bench/:id', (req, res) => {
35+
res.end(JSON.stringify({data: `${req.params.id}`, error: null}))
36+
}).listen(ports.express, () => logStart('express'))
37+
}
38+
}
39+
40+
start[process.argv[2]]()

0 commit comments

Comments
 (0)