Skip to content

Instantly share code, notes, and snippets.

@tswaters
Last active September 13, 2019 05:49
Show Gist options
  • Save tswaters/4b56e7250ead01ee5695cfa6fa0e50a1 to your computer and use it in GitHub Desktop.
Save tswaters/4b56e7250ead01ee5695cfa6fa0e50a1 to your computer and use it in GitHub Desktop.
seneca-web-adapter-express bench
'use strict'
const { runMain, show } = require('bench')
const express = require('express');
const Seneca = require('seneca');
const SenecaWeb = require('seneca-web');
const adapter = require('..');
const Request = require('request')
const PRINT_PORT = 44313
const NON_PRINT_PORT = 44312
const servers = []
const instances = []
const setup = async (print, port) => {
const instance = Seneca()
if (print) instance.test('print')
instance.use(SenecaWeb, {
context: express(),
adapter,
routes: [{
pin: 'cmd:*',
map: {
test: {get: true}
}
}],
})
instance.add('cmd:test', (_, done) => setTimeout(() => done({ok: true}), 40))
await new Promise((resolve, reject) =>
instance.ready((err) => err ? reject(err) : resolve()))
const app = instance.export('web/context')()
await new Promise((resolve, reject) =>
servers.push(app.listen(port, (err) => err ? reject(err) : resolve())))
instances.push(instance)
}
(async () => {
await setup(true, PRINT_PORT)
await setup(false, NON_PRINT_PORT)
runMain()
})()
.catch(err => {
console.error(err)
process.exit(1)
})
exports.compare = {
'with test("print")': done => {
Request(`http://localhost:${PRINT_PORT}/test`, {method: 'GET'}, () => done())
},
'without test("print")': done => {
Request(`http://localhost:${NON_PRINT_PORT}/test`, {method: 'GET'}, () => done())
}
}
exports.done = async (data) => {
await Promise.all(servers.map(server => new Promise(resolve => server.close(resolve))))
await Promise.all(instances.map(instance => new Promise(resolve => instance.close(resolve))))
show(data)
}
Scores: (bigger is better)
without test("print")
Raw:
> 0.02205177372962608
> 0.022351797862001945
> 0.02205177372962608
> 0.022977022977022976
Average (mean) 0.02235809207456927
with test("print")
Raw:
> 0.019801980198019802
> 0.01968503937007874
> 0.02127659574468085
> 0.020689655172413793
Average (mean) 0.020363317621298296
Winner: without test("print")
Compared with next highest (with test("print")), it's:
8.92% faster
1.1 times as fast
0.04 order(s) of magnitude faster
A LITTLE FASTER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment