From 3b02c6e6e1fcfcd3380bfe3606476c8cf6d4586e Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 27 Oct 2023 10:00:49 +0800 Subject: [PATCH] feat: framework benchmark (#2915) * feat: framework benchmark * feat: add readme doc * feat: koa-resourcer * fix: doc * fix: wrk * feat: actions --- .eslintignore | 1 + benchmark/README.md | 37 ++++++++++ benchmark/koa-database/index.js | 85 +++++++++++++++++++++++ benchmark/koa-resourcer/index.js | 106 +++++++++++++++++++++++++++++ benchmark/koa-sequelize/index.js | 44 ++++++++++++ benchmark/nocobase-server/index.js | 83 ++++++++++++++++++++++ 6 files changed, 356 insertions(+) create mode 100644 benchmark/README.md create mode 100644 benchmark/koa-database/index.js create mode 100644 benchmark/koa-resourcer/index.js create mode 100644 benchmark/koa-sequelize/index.js create mode 100644 benchmark/nocobase-server/index.js diff --git a/.eslintignore b/.eslintignore index 4e5924fea..4acae1f7b 100755 --- a/.eslintignore +++ b/.eslintignore @@ -27,3 +27,4 @@ packages/core/cli/templates/plugin/src/client/*.tpl packages/app/client/src/.plugins docker storage +benchmark \ No newline at end of file diff --git a/benchmark/README.md b/benchmark/README.md new file mode 100644 index 000000000..274189cf7 --- /dev/null +++ b/benchmark/README.md @@ -0,0 +1,37 @@ +## koa-database + +```bash +yarn pm2 start benchmark/koa-database/index.js --name koa-database +wrk -t20 -c20 -d20s http://localhost:13010/ +``` + +## koa-sequelize + +```bash +yarn pm2 start benchmark/koa-sequelize/index.js --name koa-sequelize +wrk -t20 -c20 -d20s http://localhost:13020/ +``` + +## nocobase-server + +```bash +yarn pm2 start benchmark/nocobase-server/index.js --name nocobase-server +wrk -t20 -c20 -d20s http://localhost:13030/api/users +``` + +## koa-resourcer + +```bash +yarn pm2 start benchmark/koa-resourcer/index.js --name koa-resourcer +wrk -t20 -c20 -d20s http://localhost:13040/api/users +``` + +## nocobase-app + +```bash +yarn install +yarn build +yarn nocobase install +yarn start +wrk -t20 -c20 -d20s http://localhost:13000/api/users?token= +``` diff --git a/benchmark/koa-database/index.js b/benchmark/koa-database/index.js new file mode 100644 index 000000000..199fab1db --- /dev/null +++ b/benchmark/koa-database/index.js @@ -0,0 +1,85 @@ +const Koa = require('koa'); +const { Database } = require('@nocobase/database'); + +const dotenv = require('dotenv'); +dotenv.config(); + +const db = new Database({ + logging: false, + dialect: process.env.DB_DIALECT, + storage: process.env.DB_STORAGE, + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + host: process.env.DB_HOST, + port: process.env.DB_PORT, + timezone: process.env.DB_TIMEZONE, + tablePrefix: process.env.DB_TABLE_PREFIX, + schema: process.env.DB_SCHEMA, + underscored: process.env.DB_UNDERSCORED === 'true', +}); + +db.collection({ + name: 'users', + sortable: 'sort', + fields: [ + { + name: 'id', + type: 'bigInt', + autoIncrement: true, + primaryKey: true, + allowNull: false, + }, + { + type: 'string', + name: 'nickname', + }, + { + type: 'string', + name: 'username', + unique: true, + }, + { + type: 'string', + name: 'email', + unique: true, + }, + { + type: 'string', + name: 'phone', + unique: true, + }, + { + type: 'password', + name: 'password', + hidden: true, + }, + { + type: 'string', + name: 'appLang', + }, + { + type: 'string', + name: 'resetToken', + unique: true, + hidden: true, + }, + { + type: 'json', + name: 'systemSettings', + defaultValue: {}, + }, + ], +}); + +const app = new Koa(); + +app.use(async (ctx, next) => { + const repository = db.getRepository('users'); + ctx.body = await repository.find(); + await next(); +}); + +app.listen(13010, () => { + console.log('koa-database: http://localhost:13010/'); +}); diff --git a/benchmark/koa-resourcer/index.js b/benchmark/koa-resourcer/index.js new file mode 100644 index 000000000..5bc473690 --- /dev/null +++ b/benchmark/koa-resourcer/index.js @@ -0,0 +1,106 @@ +const Koa = require('koa'); +const { Database } = require('@nocobase/database'); +const { middlewares } = require('@nocobase/server'); +const { Resourcer } = require('@nocobase/resourcer'); + +const dotenv = require('dotenv'); +const { list, get } = require('@nocobase/actions/lib/actions'); +dotenv.config(); + +const db = new Database({ + logging: false, + dialect: process.env.DB_DIALECT, + storage: process.env.DB_STORAGE, + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + host: process.env.DB_HOST, + port: process.env.DB_PORT, + timezone: process.env.DB_TIMEZONE, + tablePrefix: process.env.DB_TABLE_PREFIX, + schema: process.env.DB_SCHEMA, + underscored: process.env.DB_UNDERSCORED === 'true', +}); + +db.collection({ + name: 'users', + sortable: 'sort', + fields: [ + { + name: 'id', + type: 'bigInt', + autoIncrement: true, + primaryKey: true, + allowNull: false, + }, + { + type: 'string', + name: 'nickname', + }, + { + type: 'string', + name: 'username', + unique: true, + }, + { + type: 'string', + name: 'email', + unique: true, + }, + { + type: 'string', + name: 'phone', + unique: true, + }, + { + type: 'password', + name: 'password', + hidden: true, + }, + { + type: 'string', + name: 'appLang', + }, + { + type: 'string', + name: 'resetToken', + unique: true, + hidden: true, + }, + { + type: 'json', + name: 'systemSettings', + defaultValue: {}, + }, + ], +}); + +const app = new Koa(); +const resourcer = new Resourcer({ + prefix: '/api', +}); + +resourcer.define({ + name: 'users', + actions: { + list, + get, + // async list(ctx, next) { + // const repository = db.getRepository('users'); + // ctx.body = await repository.find(); + // await next(); + // }, + }, +}); + +// resourcer.registerActionHandlers({ list }); + +app.use(async (ctx, next) => { + ctx.db = db; + await next(); +}); +app.use(resourcer.restApiMiddleware()); +// app.use(middlewares.db2resource); +app.listen(13040, () => { + console.log('koa-resourcer: http://localhost:13040/api/users'); +}); diff --git a/benchmark/koa-sequelize/index.js b/benchmark/koa-sequelize/index.js new file mode 100644 index 000000000..d159d6b6c --- /dev/null +++ b/benchmark/koa-sequelize/index.js @@ -0,0 +1,44 @@ +const Koa = require('koa'); +const { Sequelize, DataTypes } = require('sequelize'); +const dotenv = require('dotenv'); + +dotenv.config(); + +const sequelize = new Sequelize({ + dialect: process.env.DB_DIALECT, + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + host: process.env.DB_HOST, + port: process.env.DB_PORT, + logging: false, +}); + +const User = sequelize.define( + 'users', + { + nickname: DataTypes.STRING, + username: { + type: DataTypes.STRING, + unique: true, + }, + email: { + type: DataTypes.STRING, + unique: true, + }, + }, + { + underscored: true, + }, +); + +const app = new Koa(); + +app.use(async (ctx, next) => { + ctx.body = await User.findAll(); + await next(); +}); + +app.listen(13020, () => { + console.log('koa-sequelize: http://localhost:13020/'); +}); diff --git a/benchmark/nocobase-server/index.js b/benchmark/nocobase-server/index.js new file mode 100644 index 000000000..c08db982c --- /dev/null +++ b/benchmark/nocobase-server/index.js @@ -0,0 +1,83 @@ +const { Application } = require('@nocobase/server'); +const dotenv = require('dotenv'); + +dotenv.config(); + +const app = new Application({ + database: { + logging: false, + dialect: process.env.DB_DIALECT, + storage: process.env.DB_STORAGE, + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + host: process.env.DB_HOST, + port: process.env.DB_PORT, + timezone: process.env.DB_TIMEZONE, + tablePrefix: process.env.DB_TABLE_PREFIX, + schema: process.env.DB_SCHEMA, + underscored: process.env.DB_UNDERSCORED === 'true', + }, + resourcer: { + prefix: '/api', + }, + acl: false, + plugins: [], +}); + +app.db.collection({ + name: 'users', + sortable: 'sort', + fields: [ + { + name: 'id', + type: 'bigInt', + autoIncrement: true, + primaryKey: true, + allowNull: false, + }, + { + type: 'string', + name: 'nickname', + }, + { + type: 'string', + name: 'username', + unique: true, + }, + { + type: 'string', + name: 'email', + unique: true, + }, + { + type: 'string', + name: 'phone', + unique: true, + }, + { + type: 'password', + name: 'password', + hidden: true, + }, + { + type: 'string', + name: 'appLang', + }, + { + type: 'string', + name: 'resetToken', + unique: true, + hidden: true, + }, + { + type: 'json', + name: 'systemSettings', + defaultValue: {}, + }, + ], +}); + +app.listen(13030, (err) => { + console.log('nocobase-server: http://localhost:13030/api/users'); +});