Skip to content

Commit 743f18a

Browse files
zaroNateBrady23
authored andcommittedSep 23, 2019
Add mysql, fortunes to NestJS, speed up update test. (#5078)
* Add mysql test to NestJS * Add fortunes benchmark to NestJS * cleanup and simplify query and update handlers for NestJS
1 parent e357083 commit 743f18a

11 files changed

+137
-32
lines changed
 

‎frameworks/TypeScript/nest/benchmark_config.json

+19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"update_url": "/bench/updates?queries=",
88
"query_url": "/bench/queries?queries=",
99
"db_url": "/bench/db",
10+
"fortune_url": "/bench/fortunes",
1011
"port": 8080,
1112
"approach": "Realistic",
1213
"classification": "Micro",
@@ -18,6 +19,24 @@
1819
"database_os": "Linux",
1920
"display_name": "nestjs",
2021
"versus": "nodejs"
22+
},
23+
"mysql": {
24+
"json_url": "/bench/json",
25+
"plaintext_url": "/bench/plaintext",
26+
"update_url": "/bench/updates?queries=",
27+
"query_url": "/bench/queries?queries=",
28+
"db_url": "/bench/db",
29+
"port": 8080,
30+
"approach": "Realistic",
31+
"classification": "Micro",
32+
"framework": "nestjs",
33+
"language": "TypeScript",
34+
"os": "Linux",
35+
"orm": "Full",
36+
"database": "MySQL",
37+
"database_os": "Linux",
38+
"display_name": "nestjs",
39+
"versus": "nodejs"
2140
}
2241
}]
2342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM node:12.3.1-slim
2+
3+
COPY ./ ./
4+
5+
RUN yarn install
6+
7+
ENV NODE_ENV production
8+
ENV DATABASE_CONFIGURATION_PROFILE=mysql
9+
10+
CMD ["yarn", "start:prod"]

‎frameworks/TypeScript/nest/nestjs.dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ COPY ./ ./
55
RUN yarn install
66

77
ENV NODE_ENV production
8+
ENV DATABASE_CONFIGURATION_PROFILE=postgres
89

910
CMD ["yarn", "start:prod"]
+30-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
module.exports = {
2-
"type": "postgres",
3-
"host": "tfb-database",
4-
"port": 5432,
5-
"username": "benchmarkdbuser",
6-
"password": "benchmarkdbpass",
7-
"database": "hello_world",
1+
let config = {
82
"synchronize":false,
93
"logging": false,
104
"entities": process.env.NODE_ENV === 'production' ? ["./dist/**/*.entity.js"] : ["./dist/**/*.entity.js", "./src/**/*.entity.ts"]
11-
}
5+
}
6+
7+
switch(process.env.DATABASE_CONFIGURATION_PROFILE) {
8+
case 'mysql':
9+
config = {
10+
...config,
11+
"type": "mysql",
12+
"host": "tfb-database",
13+
"port": 3306,
14+
"username": "benchmarkdbuser",
15+
"password": "benchmarkdbpass",
16+
"database": "hello_world",
17+
}
18+
break;
19+
case 'postgres':
20+
default:
21+
config = {
22+
...config,
23+
"type": "postgres",
24+
"host": "tfb-database",
25+
"port": 5432,
26+
"username": "benchmarkdbuser",
27+
"password": "benchmarkdbpass",
28+
"database": "hello_world",
29+
}
30+
break;
31+
}
32+
33+
module.exports = config

‎frameworks/TypeScript/nest/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"start": "ts-node -r tsconfig-paths/register src/main.ts",
1010
"start:dev": "nodemon",
1111
"start:debug": "nodemon --config nodemon-debug.json",
12-
"prestart:prod": "rm -rf dist && tsc",
12+
"prestart:prod": "rm -rf dist && tsc && cp -r src/views/ dist/",
1313
"start:prod": "node dist/main.js"
1414
},
1515
"dependencies": {
@@ -21,6 +21,8 @@
2121
"express-cluster": "^0.0.5",
2222
"fastify": "2.8.0",
2323
"fastify-formbody": "3.1.0",
24+
"hbs": "^4.0.4",
25+
"mysql2": "^1.7.0",
2426
"pg": "7.12.1",
2527
"reflect-metadata": "0.1.13",
2628
"rxjs": "6.5.3",

‎frameworks/TypeScript/nest/src/bench/bench.controller.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Controller, Get, Header, Query } from '@nestjs/common';
1+
import { Controller, Get, Header, Query, Res, Render } from '@nestjs/common';
2+
import { Response } from 'express';
23

34
import { BenchService } from './bench.service';
45

@@ -42,4 +43,14 @@ export class BenchController {
4243

4344
return 'Hello, World!';
4445
}
46+
47+
@Get('/fortunes')
48+
@Render('fortunes')
49+
@Header('Server', 'NestJS')
50+
fortunes(@Res() res: Response) {
51+
return this.benchService.getFortunes().then( fortunes => ({
52+
fortunes,
53+
}));
54+
}
55+
4556
}

‎frameworks/TypeScript/nest/src/bench/bench.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
44
import { BenchController } from './bench.controller';
55
import { BenchService } from './bench.service';
66
import { WorldEntity } from './../models/world.entity';
7-
// import { FortuneEntity } from './../models/fortune.entity';
7+
import { FortuneEntity } from './../models/fortune.entity';
88

99
@Module({
10-
imports: [TypeOrmModule.forFeature([WorldEntity])],
10+
imports: [TypeOrmModule.forFeature([WorldEntity, FortuneEntity])],
1111
controllers: [BenchController],
1212
providers: [BenchService],
1313
exports: [BenchService],

‎frameworks/TypeScript/nest/src/bench/bench.service.ts

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Injectable } from '@nestjs/common';
22
import { Repository } from 'typeorm';
33
import { WorldEntity } from '../models/world.entity';
4+
import { FortuneEntity } from '../models/fortune.entity';
45
import { InjectRepository } from '@nestjs/typeorm';
56

67
@Injectable()
78
export class BenchService {
89
constructor(
910
@InjectRepository(WorldEntity)
10-
private worldRepository: Repository<WorldEntity>){}
11+
private worldRepository: Repository<WorldEntity>,
12+
@InjectRepository(FortuneEntity)
13+
private fortuneRepository: Repository<FortuneEntity>,
14+
){}
1115

1216
getOne(){
1317

@@ -16,28 +20,33 @@ export class BenchService {
1620

1721
async getMultiple(totalQueries: string) {
1822

19-
return new Promise(async (resolve, reject) => {
20-
const worldArr = [];
21-
const total = this.sanitizeQueries(totalQueries);
22-
for (let i = 0; i < total; i++) {
23-
worldArr.push(await this.getOne());
24-
}
25-
resolve(worldArr);
26-
});
23+
const worldArr = [];
24+
const total = this.sanitizeQueries(totalQueries);
25+
for (let i = 0; i < total; i++) {
26+
worldArr.push(await this.getOne());
27+
}
28+
return worldArr;
2729
}
2830

2931
async updateMultiple(totalQueries: string) {
3032

31-
return new Promise(async (resolve, reject) => {
32-
const worldArr = [];
33-
const total = this.sanitizeQueries(totalQueries);
34-
for (let i = 0; i < total; i++) {
35-
let worldToUpdate = await this.getOne();
36-
worldToUpdate.randomnumber = Math.floor(Math.random() * 10000) + 1;
37-
worldToUpdate = await this.worldRepository.save(worldToUpdate);
38-
worldArr.push(worldToUpdate);
39-
}
40-
resolve(worldArr);
33+
const worldArr = [];
34+
const total = this.sanitizeQueries(totalQueries);
35+
for (let i = 0; i < total; i++) {
36+
let worldToUpdate = await this.getOne();
37+
worldToUpdate.randomnumber = Math.floor(Math.random() * 10000) + 1;
38+
worldArr.push(worldToUpdate);
39+
await this.worldRepository.update(worldToUpdate.id, worldToUpdate);
40+
}
41+
return worldArr;
42+
}
43+
44+
async getFortunes(){
45+
return this.fortuneRepository.find().then((fortunes) => {
46+
const newFortune = { id: 0, message: "Additional fortune added at request time." };
47+
fortunes.push(newFortune);
48+
fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1);
49+
return fortunes;
4150
});
4251
}
4352

‎frameworks/TypeScript/nest/src/main.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'dotenv/config';
22
import { NestFactory } from '@nestjs/core';
3+
import { NestExpressApplication } from '@nestjs/platform-express';
34
import { AppModule } from './app.module';
5+
import { join } from 'path';
46
import { Logger } from '@nestjs/common';
57
import * as cluster from 'express-cluster';
68

@@ -9,7 +11,11 @@ const port = process.env.PORT || 8080;
911

1012
async function bootstrap() {
1113
await cluster(async (w)=>{
12-
const app = await NestFactory.create(AppModule);
14+
const app = await NestFactory.create<NestExpressApplication>(AppModule);
15+
16+
app.setBaseViewsDir(join(__dirname, 'views'));
17+
app.setViewEngine('hbs');
18+
1319
Logger.log(`Listening on port ${port}`, 'Nest Server');
1420
return app.listen(port)
1521
}, {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Fortunes</title>
5+
</head>
6+
<body>
7+
<table>
8+
<tr>
9+
<th>id</th>
10+
<th>message</th>
11+
</tr>
12+
{{#fortunes}}
13+
<tr>
14+
<td>{{id}}</td>
15+
<td>{{message}}</td>
16+
</tr>
17+
{{/fortunes}}
18+
</table>
19+
</body>
20+
</html>

‎package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"hbs": "^4.0.4"
4+
}
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.