Skip to content

Commit 56e5498

Browse files
refactor(javascript/fastify): upgrade deps, removed mongodb (failed with updates). (#9402)
1 parent 771117c commit 56e5498

File tree

13 files changed

+84
-170
lines changed

13 files changed

+84
-170
lines changed

frameworks/JavaScript/fastify/README.md

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,3 @@ Information about Fastify can be found at https://github.com/fastify/fastify
1919
### JSON Encoding Test
2020

2121
http://TFB-server:8080/json
22-
23-
### Data-Store/Database Mapping Test
24-
25-
MongoDB:
26-
http://TFB-server:8080/mongoose/
27-
28-
MySQL:
29-
http://TFB-server:8080/mysql-orm/
30-
31-
### Variable Query Test
32-
33-
MongoDB:
34-
http://TFB-server:8080/mongoose/2
35-
36-
MySQL:
37-
http://TFB-server:8080/mysql-orm/2
38-
39-
### Fortune Test
40-
41-
MySQL:
42-
http://TFB-server:8080/fortune
43-
44-
### Database Update Test
45-
46-
MongoDB:
47-
http://TFB-server:8080/mongoose-update/2
48-
49-
MySQL:
50-
http://TFB-server:8080/mysql-orm-update/2
51-

frameworks/JavaScript/fastify/benchmark_config.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
"default": {
66
"json_url": "/json",
77
"plaintext_url": "/plaintext",
8-
"db_url": "/db",
9-
"query_url": "/queries?queries=",
10-
"fortune_url": "/fortunes",
11-
"update_url": "/updates?queries=",
128
"port": 8080,
139
"approach": "Realistic",
1410
"classification": "Micro",
15-
"database": "MongoDB",
1611
"framework": "fastify",
1712
"language": "JavaScript",
1813
"flavor": "NodeJS",
@@ -42,7 +37,7 @@
4237
"webserver": "None",
4338
"os": "Linux",
4439
"database_os": "Linux",
45-
"display_name": "fastify",
40+
"display_name": "fastify [mysql]",
4641
"notes": "",
4742
"versus": "nodejs"
4843
},
@@ -63,7 +58,7 @@
6358
"webserver": "None",
6459
"os": "Linux",
6560
"database_os": "Linux",
66-
"display_name": "fastify",
61+
"display_name": "fastify [postgres]",
6762
"notes": "",
6863
"versus": "nodejs"
6964
}

frameworks/JavaScript/fastify/config.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ name = "fastify"
44
[main]
55
urls.plaintext = "/plaintext"
66
urls.json = "/json"
7-
urls.db = "/db"
8-
urls.query = "/queries?queries="
9-
urls.update = "/updates?queries="
10-
urls.fortune = "/fortunes"
117
approach = "Realistic"
128
classification = "Micro"
13-
database = "MongoDB"
149
database_os = "Linux"
1510
os = "Linux"
1611
orm = "Raw"

frameworks/JavaScript/fastify/create-server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const fastify = require("fastify")();
22
const handlers = require("./handlers");
33

4+
fastify.setErrorHandler((error, request, reply) => {
5+
console.log(error)
6+
reply.status(500).send({ ok: false })
7+
})
8+
49
fastify.register(require("@fastify/view"), {
510
engine: {
611
handlebars: require("handlebars")

frameworks/JavaScript/fastify/db/mongo.js

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
1-
const knex = require("knex")({
2-
client: "mysql2",
3-
connection: {
4-
host: "tfb-database",
5-
user: "benchmarkdbuser",
6-
password: "benchmarkdbpass",
7-
database: "hello_world"
8-
}
9-
});
1+
const { createPool } = require("mariadb");
2+
3+
const clientOpts = {
4+
host: process.env.MYSQL_HOST,
5+
user: process.env.MYSQL_USER,
6+
password: process.env.MYSQL_PSWD,
7+
database: process.env.MYSQL_DBNAME,
8+
};
9+
10+
const pool = createPool({ ...clientOpts, connectionLimit: 1 });
11+
const execute = (text, values) => pool.execute(text, values || undefined);
1012

1113
async function allFortunes() {
12-
return knex("Fortune").select("*");
14+
return execute("select id, message from fortune", []);
1315
}
1416

1517
async function getWorld(id) {
16-
return knex("World")
17-
.first()
18-
.where({ id });
18+
return execute("select id, randomNumber from world where id = ?", [id]).then(
19+
(arr) => arr[0]
20+
);
1921
}
2022

21-
async function saveWorlds(worlds) {
22-
const updates = [];
23-
24-
worlds.forEach(world => {
25-
const { id, randomNumber } = world;
26-
27-
updates.push(
28-
knex("World")
29-
.update({ randomNumber })
30-
.where({ id })
31-
);
32-
});
33-
34-
return Promise.all(updates);
23+
async function bulkUpdate(worlds) {
24+
const sql = "update world set randomNumber = ? where id = ?";
25+
const values = worlds
26+
.map((world) => [world.randomnumber, world.id])
27+
.sort((a, b) => (a[0] < b[0] ? -1 : 1));
28+
return pool.batch(sql, values);
3529
}
3630

3731
module.exports = {
3832
getWorld,
39-
saveWorlds,
40-
allFortunes
33+
bulkUpdate,
34+
allFortunes,
4135
};
Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1-
const knex = require("knex")({
2-
client: "pg",
3-
connection: {
4-
host: "tfb-database",
5-
user: "benchmarkdbuser",
6-
password: "benchmarkdbpass",
7-
database: "hello_world"
8-
}
9-
});
1+
const postgres = require("postgres");
2+
3+
const clientOpts = {
4+
host: process.env.PG_HOST,
5+
user: process.env.PG_USER,
6+
password: process.env.PG_PSWD,
7+
database: process.env.PG_DBNAME,
8+
};
9+
10+
const sql = postgres({ ...clientOpts, max: 1 });
1011

1112
async function allFortunes() {
12-
return knex("Fortune").select("*");
13+
return sql`select id, message from fortune`;
1314
}
1415

1516
async function getWorld(id) {
16-
return knex("World")
17-
.first()
18-
.where({ id });
17+
return sql`select id, randomNumber from world where id = ${id}`.then(
18+
(arr) => arr[0]
19+
);
1920
}
2021

21-
async function saveWorlds(worlds) {
22-
const updates = [];
23-
24-
worlds.forEach(world => {
25-
const { id, randomNumber } = world;
26-
27-
updates.push(
28-
knex("World")
29-
.update({ randomnumber: randomNumber })
30-
.where({ id })
31-
);
32-
});
22+
async function bulkUpdate(worlds) {
23+
const values = sql(
24+
worlds
25+
.map((world) => [world.id, world.randomnumber])
26+
.sort((a, b) => (a[0] < b[0] ? -1 : 1))
27+
);
3328

34-
return Promise.all(updates);
29+
return sql`update world set randomNumber = (update_data.randomNumber)::int
30+
from (values ${values}) as update_data (id, randomNumber)
31+
where world.id = (update_data.id)::int`;
3532
}
3633

3734
module.exports = {
3835
getWorld,
39-
saveWorlds,
40-
allFortunes
36+
bulkUpdate,
37+
allFortunes,
4138
};

frameworks/JavaScript/fastify/fastify-mysql.dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
FROM node:20.12.2-alpine
1+
FROM node:20.16-slim
22

33
COPY ./ ./
44

55
RUN npm install
66

77
ENV NODE_ENV production
88
ENV DATABASE mysql
9+
ENV MYSQL_HOST tfb-database
10+
ENV MYSQL_USER benchmarkdbuser
11+
ENV MYSQL_PSWD benchmarkdbpass
12+
ENV MYSQL_DBNAME hello_world
913

1014
EXPOSE 8080
1115

frameworks/JavaScript/fastify/fastify-postgres.dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
FROM node:20.12.2-alpine
1+
FROM node:20.16-slim
22

33
COPY ./ ./
44

55
RUN npm install
66

77
ENV NODE_ENV production
88
ENV DATABASE postgres
9+
ENV PG_HOST tfb-database
10+
ENV PG_USER benchmarkdbuser
11+
ENV PG_PSWD benchmarkdbpass
12+
ENV PG_DBNAME hello_world
913

1014
EXPOSE 8080
1115

frameworks/JavaScript/fastify/fastify.dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
FROM node:20.12.2-alpine
1+
FROM node:20.16-slim
22

33
COPY ./ ./
44

55
RUN npm install
66

77
ENV NODE_ENV production
8-
ENV DATABASE mongo
98

109
EXPOSE 8080
1110

0 commit comments

Comments
 (0)