Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
First Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan Amini committed Feb 22, 2018
1 parent e018466 commit 035373e
Show file tree
Hide file tree
Showing 25 changed files with 3,082 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ typings/
# dotenv environment variables file
.env

results/
140 changes: 140 additions & 0 deletions benchmark-bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env node

'use strict'

const inquirer = require('inquirer')
const bench = require('./lib/bench')

function select (callback) {
inquirer.prompt([
{
type: 'checkbox',
message: 'Select packages',
name: 'list',
choices: [
new inquirer.Separator(' = The usual ='),
{
name: 'foxify',
checked: true
},
{
name: 'fastify'
},
{
name: 'express'
},
{
name: 'koa'
},
{
name: 'hapi'
},
{
name: 'connect'
},
{
name: 'restify'
},
{
name: 'take-five'
},
{
name: 'total.js'
},
new inquirer.Separator(' = The extras = '),
{
name: 'connect-router'
},
{
name: 'express-route-prefix'
},
{
name: 'koa-router'
},
{
name: 'micro'
},
{
name: 'micro-router'
},
{
name: 'trek-engine'
},
{
name: 'trek-engine-router'
}
],
validate: function (answer) {
if (answer.length < 1) return 'You must choose at least one package.'

return true
}
}
])
.then(function (answers) {
callback(answers.list)
})
}

inquirer.prompt([
{
type: 'confirm',
name: 'all',
message: 'Do you want to run all benchmark tests?',
default: false
},
{
type: 'input',
name: 'connections',
message: 'How many connections you need?',
default: 100,
validate (value) {
return !Number.isNaN(parseFloat(value)) || 'Please enter a number'
},
filter: Number
},
{
type: 'input',
name: 'pipelining',
message: 'How many pipelining you need?',
default: 10,
validate (value) {
return !Number.isNaN(parseFloat(value)) || 'Please enter a number'
},
filter: Number
},
{
type: 'input',
name: 'duration',
message: 'How long does it takes?',
default: 5,
validate (value) {
return !Number.isNaN(parseFloat(value)) || 'Please enter a number'
},
filter: Number
}
]).then((opts) => {
if (!opts.all) {
select(list => bench(opts, list))
} else {
bench(opts, [
'bare',
'connect',
'connect-router',
'express-route-prefix',
'express',
'hapi',
'koa-router',
'koa',
'restify',
'take-five',
'total.js',
'fastify',
'foxify',
'micro',
'micro-router',
'trek-engine',
'trek-engine-router'
])
}
})
71 changes: 71 additions & 0 deletions benchmark-compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

'use strict'

const {existsSync} = require('fs')
const inquirer = require('inquirer')
const path = require('path')
const chalk = require('chalk')
const {compare} = require('./lib/autocannon')

let choices = [
'bare',
'connect',
'connect-router',
'express-route-prefix',
'express',
'hapi',
'koa-router',
'koa',
'restify',
'take-five',
'total.js',
'fastify',
'foxify',
'micro',
'micro-router',
'trek-engine',
'trek-engine-router'
]

const resultsDirectory = path.join(process.cwd(), 'results')

choices = choices.filter(choice => existsSync(path.join(resultsDirectory, `${choice}.json`)))

if (choices.length === 0) {
console.log(chalk.red('Run benchmark first to gather results to compare'))
} else {
inquirer.prompt([{
type: 'list',
name: 'choice',
message: 'What\'s your first pick?',
choices
}]).then((firstChoice) => {
choices = choices.filter(choice => choice !== firstChoice.choice)

inquirer.prompt([{
type: 'list',
name: 'choice',
message: 'What\'s your second one?',
choices
}]).then((secondChoice) => {
const [a, b] = [firstChoice.choice, secondChoice.choice]
const result = compare(a, b)

if (result === true) {
console.log(chalk.green.bold(`${a} and ${b} both are fast!`))
} else {
const fastest = chalk.bold.yellow(result.fastest)
const fastestAverage = chalk.green(result.fastestAverage)
const slowest = chalk.bold.yellow(result.slowest)
const slowestAverage = chalk.green(result.slowestAverage)
const diff = chalk.bold.green(result.diff)

console.log(`
${chalk.blue('Both are awesome but')} ${fastest} ${chalk.blue('is')} ${diff} ${chalk.blue('faster than')} ${slowest}
${fastest} ${chalk.blue('request average is')} ${fastestAverage}
${slowest} ${chalk.blue('request average is')} ${slowestAverage}`)
}
})
})
}
10 changes: 10 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

'use strict'

const program = require('commander')

program
.command('bench', 'bench one or all packages', { isDefault: true })
.command('compare', 'compare twice package')
.parse(process.argv)
11 changes: 11 additions & 0 deletions benchmarks/bare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const http = require('http')

const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'application/json')

res.end(JSON.stringify({hello: 'world'}))
})

server.listen(3000)
16 changes: 16 additions & 0 deletions benchmarks/connect-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const connect = require('connect')
const router = require('router')()

const app = connect()

router.get('/', (req, res) => {
res.setHeader('Content-Type', 'application/json')

res.end(JSON.stringify({hello: 'world'}))
})

app.use(router)

app.listen(3000)
13 changes: 13 additions & 0 deletions benchmarks/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const connect = require('connect')

const app = connect()

app.use((req, res) => {
res.setHeader('Content-Type', 'application/json')

res.end(JSON.stringify({hello: 'world'}))
})

app.listen(3000)
18 changes: 18 additions & 0 deletions benchmarks/express-route-prefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const express = require('express')

const app = express()

app.disable('etag')
app.disable('x-powered-by')

const router = express.Router()

router.get('/hello', (req, res) => {
res.json({hello: 'world'})
})

app.use('/greet', router)

app.listen(3000)
14 changes: 14 additions & 0 deletions benchmarks/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const express = require('express')

const app = express()

app.disable('etag')
app.disable('x-powered-by')

app.get('/', (req, res) => {
res.json({hello: 'world'})
})

app.listen(3000)
24 changes: 24 additions & 0 deletions benchmarks/fastify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const fastify = require('fastify')()

const schema = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
}
}
}

fastify.get('/', schema, (req, reply) => {
reply.send({hello: 'world'})
})

fastify.listen(3000)
11 changes: 11 additions & 0 deletions benchmarks/foxify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const Foxify = require('foxify')

const app = new Foxify()

app.get('/', (req, res) => {
res.json({hello: 'world'})
})

app.start()
22 changes: 22 additions & 0 deletions benchmarks/hapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const Hapi = require('hapi')

const server = Hapi.server({port: 3000, debug: false})

server.route({
method: 'GET',
path: '/',
config: {
cache: false,
response: {
ranges: false
},
state: {
parse: false
}
},
handler: (request, h) => ({hello: 'world'})
})

server.start()
16 changes: 16 additions & 0 deletions benchmarks/koa-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const Koa = require('koa')
const router = require('koa-router')()

const app = new Koa()

router.get('/', async (ctx) => {
ctx.body = {
hello: 'world'
}
})

app.use(router.routes())

app.listen(3000)
13 changes: 13 additions & 0 deletions benchmarks/koa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const Koa = require('koa')

const app = new Koa()

app.use(async (ctx) => {
ctx.body = {
hello: 'world'
}
})

app.listen(3000)
Loading

0 comments on commit 035373e

Please sign in to comment.