11'use strict'
22
3- const http = require ( 'http' )
3+ const { fork } = require ( 'child_process' )
4+ const { join } = require ( 'path' )
45const getScrud = require ( 'get-scrud' )
5- const polka = require ( 'polka' )
6- const fastify = require ( 'fastify' ) ( )
7- const scrud = require ( './../index' )
8- const express = require ( 'express' )
96const autocannon = require ( 'autocannon' )
107const table = require ( 'tty-table' )
118const 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- }
189const results = [ ]
1910const 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
2122const 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
7247const 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 } )
0 commit comments