bench: add bench for async/await (#1036)

This commit is contained in:
Yu Qi 2017-08-03 09:33:51 +08:00 committed by Yiyu He
parent 7294eae078
commit 78832ff6c6
3 changed files with 30 additions and 12 deletions

View file

@ -2,14 +2,22 @@
all: middleware
middleware:
@./run 1 $@
@./run 5 $@
@./run 10 $@
@./run 15 $@
@./run 20 $@
@./run 30 $@
@./run 50 $@
@./run 100 $@
@./run 1 false $@
@./run 5 false $@
@./run 10 false $@
@./run 15 false $@
@./run 20 false $@
@./run 30 false $@
@./run 50 false $@
@./run 100 false $@
@./run 1 true $@
@./run 5 true $@
@./run 10 true $@
@./run 15 true $@
@./run 20 true $@
@./run 30 true $@
@./run 50 true $@
@./run 100 true $@
@echo
.PHONY: all middleware

View file

@ -7,14 +7,24 @@ const app = new Koa();
// number of middleware
let n = parseInt(process.env.MW || '1', 10);
console.log(` ${n} middleware`);
let useAsync = process.env.USE_ASYNC === 'true';
console.log(` ${n}${useAsync ? ' async' : ''} middleware`);
while (n--) {
if (useAsync) {
app.use(async (ctx, next) => await next());
} else {
app.use((ctx, next) => next());
}
}
const body = Buffer.from('Hello World');
if (useAsync) {
app.use(async (ctx, next) => { await next(); ctx.body = body; });
} else {
app.use((ctx, next) => next().then(() => ctx.body = body));
}
app.listen(3333);

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
echo
MW=$1 node $2 &
MW=$1 USE_ASYNC=$2 node $3 &
pid=$!
sleep 2