Skip to content

Commit

Permalink
benchmark script
Browse files Browse the repository at this point in the history
  • Loading branch information
jbielick committed May 31, 2018
1 parent e0ae639 commit d88b179
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bench/Makefile
@@ -0,0 +1,23 @@
all: middleware

middleware:
@./run 1 false
@./run 5 false
@./run 10 false
@./run 20 false
@./run 50 false
@./run 100 false
@./run 200 false
@./run 500 false
@./run 1000 false
@./run 1 true
@./run 5 true
@./run 10 true
@./run 20 true
@./run 50 true
@./run 100 true
@./run 200 true
@./run 500 true
@./run 1000 true

.PHONY: all middleware
31 changes: 31 additions & 0 deletions bench/run
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -e

export FACTOR=$1
export USE_MIDDLEWARE=$2
export PORT=3333

host="http://localhost:$PORT"

node "$(dirname $0)/server.js" &

pid=$!

curl \
--retry-connrefused \
--retry 5 \
--retry-delay 0 \
-s \
"$host/_health" \
> /dev/null

# siege -c 50 -t 8 "$host/10/child/grandchild/%40"
wrk "$host/10/child/grandchild/%40" \
-d 3 \
-c 50 \
-t 8 \
| grep 'Requests/sec' \
| awk '{ print " " $2 }'

kill $pid
47 changes: 47 additions & 0 deletions bench/server.js
@@ -0,0 +1,47 @@
const Koa = require('koa');
const Router = require('../');

const app = new Koa();
const router = new Router();

const ok = ctx => ctx.status = 200;
const n = parseInt(process.env.FACTOR || '10', 10);
const useMiddleware = process.env.USE_MIDDLEWARE === 'true';

router.get('/_health', ok);

for (let i = n; i > 0; i--) {
if (useMiddleware) router.use((ctx, next) => next());
router.get(`/${i}/one`, ok);
router.get(`/${i}/one/two`, ok);
router.get(`/${i}/one/two/:three`, ok);
router.get(`/${i}/one/two/:three/:four?`, ok);
router.get(`/${i}/one/two/:three/:four?/five`, ok);
router.get(`/${i}/one/two/:three/:four?/five/six`, ok);
}

const grandchild = new Router();

if (useMiddleware) grandchild.use((ctx, next) => next());
grandchild.get('/', ok);
grandchild.get('/:id', ok);
grandchild.get('/:id/seven', ok);
grandchild.get('/:id/seven(/eight)?', ok);

for (let i = n; i > 0; i--) {
let child = new Router();
if (useMiddleware) child.use((ctx, next) => next());
child.get(`/:${''.padStart(i, 'a')}`, ok);
child.nest('/grandchild', grandchild);

This comment has been minimized.

Copy link
@MarkHerhold

MarkHerhold Mar 5, 2019

@jbielick Hey, do you remember how you were using child.nest() when writing this? I can't find the relevant API and would love to know this was meant to work!

This comment has been minimized.

Copy link
@jbielick

jbielick Mar 5, 2019

Author Collaborator

This was used for the 8.x branch (rewrite which I abandoned).

https://github.com/ZijianHe/koa-router/blob/8.x/lib/router.js

This comment has been minimized.

Copy link
@fl0w

fl0w Mar 5, 2019

I do this monkey patch

const { flatten } = require('lodash')
const Router = require('koa-router')

/**
 * Adds Router.expand which accepts and expands Router instances for Router.use.
 * @returns {Router<this>}
 */

Router.prototype.expand = function (...args) {
  return this.use(
    ...flatten(
      args.map(
        arg => arg instanceof Router
          ? [arg.routes(), arg.allowedMethods()]
          : arg
      )
    )
  )
}
router.nest(`/${i}/child`, child);
}

if (process.env.DEBUG) {
console.log(require('../lib/utils').inspect(router));
}

app.use(router.routes());

process.stdout.write(`mw: ${useMiddleware} factor: ${n}`);

app.listen(process.env.PORT);

0 comments on commit d88b179

Please sign in to comment.