feat: framework benchmark (#2915)

* feat: framework benchmark

* feat: add readme doc

* feat: koa-resourcer

* fix: doc

* fix: wrk

* feat: actions
This commit is contained in:
chenos 2023-10-27 10:00:49 +08:00 committed by GitHub
parent e35e4af5e7
commit 3b02c6e6e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 356 additions and 0 deletions

View File

@ -27,3 +27,4 @@ packages/core/cli/templates/plugin/src/client/*.tpl
packages/app/client/src/.plugins packages/app/client/src/.plugins
docker docker
storage storage
benchmark

37
benchmark/README.md Normal file
View File

@ -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=
```

View File

@ -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/');
});

View File

@ -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');
});

View File

@ -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/');
});

View File

@ -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');
});