diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..b55fbcb --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,16 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + Job: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-test.yml@master + with: + os: 'ubuntu-latest' + version: '18, 20, 22, 24' + install: npm run install-wrk > wrk.log && npm install && cat /proc/cpuinfo diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c29442b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -sudo: false -language: node_js -node_js: - - '14' -cache: - directories: - - wrk/bin -before_script: - - '[ ! -f wrk/bin/wrk ] && rm -rf wrk && git clone --depth 1 https://github.com/wg/wrk.git && make -j10 -C wrk && mkdir wrk/bin && mv wrk/wrk wrk/bin || true' - - export PATH=$PWD/wrk/bin:$PATH -script: - - nvm i 14 - - node -p 'process.versions' - - npm test - - mv stats.csv 14.csv - - nvm i 16 - - node -p 'process.versions' - - npm test - - mv stats.csv 16.csv - - nvm i 18 - - node -p 'process.versions' - - npm test - - mv stats.csv 18.csv - - nvm i 19 - - node -p 'process.versions' - - npm test - - mv stats.csv 19.csv diff --git a/README.md b/README.md index 2cc3c2a..c39a9fa 100644 --- a/README.md +++ b/README.md @@ -10,30 +10,31 @@ egg benchmark ## Default Middleware -- egg default enable 15 middlewares (6 security middlewares enable by default) -- enable router -- passport has 17 middlewares (15 default, 2 passport middlewares) -- csrf are disabled, because in most situation we won't caculate csrf token and set to cookie +* egg default enable 15 middlewares (6 security middlewares enable by default) +* enable router +* passport has 17 middlewares (15 default, 2 passport middlewares) +* csrf are disabled, because in most situation we won't calculate csrf token and set to cookie ## Scenes -- Hello World: `$ EGG_SERVER_ENV=prod node benchmarks/simple/dispatch.js` -- nunjucks: `$ EGG_SERVER_ENV=prod node benchmarks/simple_view/dispatch.js` -- Empty passport: `$ EGG_SERVER_ENV=prod node benchmarks/simple_passport/dispatch.js` +* Hello World: `$ EGG_SERVER_ENV=prod node benchmarks/simple/dispatch.js` +* nunjucks: `$ EGG_SERVER_ENV=prod node benchmarks/simple_view/dispatch.js` +* Empty passport: `$ EGG_SERVER_ENV=prod node benchmarks/simple_passport/dispatch.js` ## Scripts -- koa1: `wrk http://127.0.0.1:7001/ -d 10 -c 50 -t 8` -- koa2: `wrk http://127.0.0.1:7002/ -d 10 -c 50 -t 8` -- egg1: `wrk http://127.0.0.1:7003/ -d 10 -c 50 -t 8` -- egg2: `wrk http://127.0.0.1:7004/ -d 10 -c 50 -t 8` -- egg3: `wrk http://127.0.0.1:7005/ -d 10 -c 50 -t 8` -- egg3 with worker_threads: `wrk http://127.0.0.1:7006/ -d 10 -c 50 -t 8` +* koa1: `wrk http://127.0.0.1:7001/ -d 10 -c 50 -t 8` +* koa2: `wrk http://127.0.0.1:7002/ -d 10 -c 50 -t 8` +* egg1: `wrk http://127.0.0.1:7003/ -d 10 -c 50 -t 8` +* egg2: `wrk http://127.0.0.1:7004/ -d 10 -c 50 -t 8` +* egg3: `wrk http://127.0.0.1:7005/ -d 10 -c 50 -t 8` +* egg3 with worker_threads: `wrk http://127.0.0.1:7006/ -d 10 -c 50 -t 8` +* egg3 with reusePort: `wrk http://127.0.0.1:7008/ -d 10 -c 50 -t 8` ## Server -- MacBook Pro (Retina, 15-inch, Late 2013) -- 2 GHz Intel Core i7 (only use 4 core for benchmark) +* MacBook Pro (Retina, 15-inch, Late 2013) +* 2 GHz Intel Core i7 (only use 4 core for benchmark) ## CPU Profiler @@ -41,9 +42,13 @@ egg benchmark ## Known issues -- `Date.now()` cost a lot of CPU time(7%) in `meta` middleware and `CreateContext` method. +* `Date.now()` cost a lot of CPU time(7%) in `meta` middleware and `CreateContext` method. ## Last Results -- [Visualization](https://eggjs.github.io/benchmark/plot/) -- [Statistics data](https://github.com/eggjs/benchmark/blob/master/plot/all.csv) +* [Visualization](https://eggjs.github.io/benchmark/plot/) +* [Statistics data](https://github.com/eggjs/benchmark/blob/master/plot/all.csv) + +## Contributors + +[![contributors](https://contrib.rocks/image?repo=eggjs/benchmark&max=240&columns=26)](https://github.com/eggjs/benchmark/graphs/contributors) diff --git a/install-wrk.sh b/install-wrk.sh new file mode 100755 index 0000000..6f7badf --- /dev/null +++ b/install-wrk.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +[ ! -f wrk/bin/wrk ] && rm -rf wrk && git clone --depth 1 https://github.com/wg/wrk.git && make -j10 -C wrk && mkdir wrk/bin && mv wrk/wrk wrk/bin || true diff --git a/package.json b/package.json index 16d7708..eeb2c3c 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,9 @@ "egg-passport": "^2.0.0", "egg-view-ejs": "^2.0.0", "egg-view-nunjucks": "^2.1.4", - "egg1": "^1.11.0", - "egg2": "^2.37.0", - "egg3": "^3.3.3", + "egg1": "npm:egg@1", + "egg2": "npm:egg@2", + "egg3": "npm:egg@3", "koa": "^2.13.4", "koa-router": "^12.0.0", "nunjucks": "^3.0.1" @@ -22,11 +22,12 @@ "node": ">=14.20.0" }, "scripts": { - "test-simple": "simple/run.sh", + "install-wrk": "pwd && ls -l . && sh install-wrk.sh", + "test-simple": "NODE_DEBUG=egg-cluster:app_worker simple/run.sh", "test-simple-view": "simple_view/run.sh", "test-simple-passport": "simple_passport/run.sh", "test": "cat stats-header.csv > stats.csv && npm run test-simple && npm run test-simple-view && npm run test-simple-passport && cp stats.csv plot/all.csv", - "ci": "EGG_SERVER_ENV=prod npm test" + "ci": "PATH=$PWD/wrk/bin:$PATH EGG_SERVER_ENV=prod npm test" }, "repository": { "type": "git", diff --git a/simple/dispatch.js b/simple/dispatch.js index f7437ff..2893328 100644 --- a/simple/dispatch.js +++ b/simple/dispatch.js @@ -2,13 +2,17 @@ const egg3 = require('egg3'); const egg2 = require('egg2'); const egg1 = require('egg1'); const cluster = require('cluster'); +const os = require('os'); -let workers = Number(process.argv[2] || require('os').cpus().length); +let workers = Number(process.argv[2] || os.cpus().length); if (workers > 4) { workers = 4; } if (cluster.isMaster) { + console.log('os version: %s', os.version()); + console.log('egg-cluster version: %s', require('egg-cluster/package.json').version); + egg1.startCluster({ workers, baseDir: __dirname, @@ -30,6 +34,14 @@ if (cluster.isMaster) { framework: 'egg3', }); + egg3.startCluster({ + workers, + baseDir: __dirname, + port: 7008, + framework: 'egg3', + reusePort: true, + }); + egg3.startCluster({ startMode: 'worker_threads', workers: 1, @@ -38,6 +50,15 @@ if (cluster.isMaster) { framework: 'egg3', }); + egg3.startCluster({ + startMode: 'worker_threads', + workers, + port: 7009, + baseDir: __dirname, + framework: 'egg3', + reusePort: true, + }); + for (let i = 0; i < workers; i++) { cluster.fork(); } diff --git a/simple/run.sh b/simple/run.sh index e3c0a95..56f65cc 100755 --- a/simple/run.sh +++ b/simple/run.sh @@ -14,6 +14,8 @@ curl 'http://127.0.0.1:7003/' curl 'http://127.0.0.1:7004/' curl 'http://127.0.0.1:7005/' curl 'http://127.0.0.1:7006/' +curl 'http://127.0.0.1:7008/' +curl 'http://127.0.0.1:7009/' test `tail -c 1 $CSV` && printf "\n" >> $CSV @@ -25,58 +27,88 @@ function print_head { printf "\"$TITLE\"," >> $CSV } +sleep 5 echo "" -echo "------- koa hello -------" +echo "------- egg3 hello with reusePort=true -------" echo "" -print_head "koa" "koa hello" -wrk 'http://127.0.0.1:7002/' \ - -d 10 \ +print_head "egg3" "egg3 hello with reusePort=true" +wrk 'http://127.0.0.1:7008/' \ + -d 30 \ -c 50 \ -t 8 \ + --latency \ -s $REPORT -sleep 3 +sleep 5 echo "" -echo "------- egg1 hello -------" +echo "------- egg3 hello -------" echo "" -print_head "egg1" "egg1 hello" -wrk 'http://127.0.0.1:7003/' \ - -d 10 \ +print_head "egg3" "egg3 hello" +wrk 'http://127.0.0.1:7005/' \ + -d 30 \ -c 50 \ -t 8 \ + --latency \ -s $REPORT -sleep 3 +sleep 5 echo "" -echo "------- egg2 hello -------" +echo "------- egg3 hello with worker_threads=1 -------" echo "" -print_head "egg2" "egg2 hello" -wrk 'http://127.0.0.1:7004/' \ - -d 10 \ +print_head "egg3" "egg3 hello with worker_threads=1" +wrk 'http://127.0.0.1:7006/' \ + -d 30 \ -c 50 \ -t 8 \ + --latency \ -s $REPORT -sleep 3 +sleep 5 echo "" -echo "------- egg3 hello -------" +echo "------- egg3 hello with worker_threads and reusePort=true -------" echo "" -print_head "egg3" "egg3 hello" -wrk 'http://127.0.0.1:7005/' \ - -d 10 \ +print_head "egg3" "egg3 hello with worker_threads and reusePort=true" +wrk 'http://127.0.0.1:7009/' \ + -d 30 \ -c 50 \ -t 8 \ + --latency \ -s $REPORT -sleep 3 +sleep 5 echo "" -echo "------- egg3 hello with worker_threads=1 -------" +echo "------- koa hello -------" echo "" -print_head "egg3" "egg3 hello with worker_threads=1" -wrk 'http://127.0.0.1:7006/' \ - -d 10 \ +print_head "koa" "koa hello" +wrk 'http://127.0.0.1:7002/' \ + -d 30 \ + -c 50 \ + -t 8 \ + --latency \ + -s $REPORT + +sleep 5 +echo "" +echo "------- egg1 hello -------" +echo "" +print_head "egg1" "egg1 hello" +wrk 'http://127.0.0.1:7003/' \ + -d 30 \ + -c 50 \ + -t 8 \ + --latency \ + -s $REPORT + +sleep 5 +echo "" +echo "------- egg2 hello -------" +echo "" +print_head "egg2" "egg2 hello" +wrk 'http://127.0.0.1:7004/' \ + -d 30 \ -c 50 \ -t 8 \ + --latency \ -s $REPORT kill $pid