Skip to content

Commit

Permalink
refactor(javascript/fastify): upgrade deps, removed mongodb (failed w…
Browse files Browse the repository at this point in the history
…ith updates). (#9402)
  • Loading branch information
KostyaTretyak authored Nov 25, 2024
1 parent 771117c commit 56e5498
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 170 deletions.
30 changes: 0 additions & 30 deletions frameworks/JavaScript/fastify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,3 @@ Information about Fastify can be found at https://github.com/fastify/fastify
### JSON Encoding Test

http://TFB-server:8080/json

### Data-Store/Database Mapping Test

MongoDB:
http://TFB-server:8080/mongoose/

MySQL:
http://TFB-server:8080/mysql-orm/

### Variable Query Test

MongoDB:
http://TFB-server:8080/mongoose/2

MySQL:
http://TFB-server:8080/mysql-orm/2

### Fortune Test

MySQL:
http://TFB-server:8080/fortune

### Database Update Test

MongoDB:
http://TFB-server:8080/mongoose-update/2

MySQL:
http://TFB-server:8080/mysql-orm-update/2

9 changes: 2 additions & 7 deletions frameworks/JavaScript/fastify/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
"default": {
"json_url": "/json",
"plaintext_url": "/plaintext",
"db_url": "/db",
"query_url": "/queries?queries=",
"fortune_url": "/fortunes",
"update_url": "/updates?queries=",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
"database": "MongoDB",
"framework": "fastify",
"language": "JavaScript",
"flavor": "NodeJS",
Expand Down Expand Up @@ -42,7 +37,7 @@
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "fastify",
"display_name": "fastify [mysql]",
"notes": "",
"versus": "nodejs"
},
Expand All @@ -63,7 +58,7 @@
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "fastify",
"display_name": "fastify [postgres]",
"notes": "",
"versus": "nodejs"
}
Expand Down
5 changes: 0 additions & 5 deletions frameworks/JavaScript/fastify/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ name = "fastify"
[main]
urls.plaintext = "/plaintext"
urls.json = "/json"
urls.db = "/db"
urls.query = "/queries?queries="
urls.update = "/updates?queries="
urls.fortune = "/fortunes"
approach = "Realistic"
classification = "Micro"
database = "MongoDB"
database_os = "Linux"
os = "Linux"
orm = "Raw"
Expand Down
5 changes: 5 additions & 0 deletions frameworks/JavaScript/fastify/create-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const fastify = require("fastify")();
const handlers = require("./handlers");

fastify.setErrorHandler((error, request, reply) => {
console.log(error)
reply.status(500).send({ ok: false })
})

fastify.register(require("@fastify/view"), {
engine: {
handlebars: require("handlebars")
Expand Down
47 changes: 0 additions & 47 deletions frameworks/JavaScript/fastify/db/mongo.js

This file was deleted.

52 changes: 23 additions & 29 deletions frameworks/JavaScript/fastify/db/mysql.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
const knex = require("knex")({
client: "mysql2",
connection: {
host: "tfb-database",
user: "benchmarkdbuser",
password: "benchmarkdbpass",
database: "hello_world"
}
});
const { createPool } = require("mariadb");

const clientOpts = {
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PSWD,
database: process.env.MYSQL_DBNAME,
};

const pool = createPool({ ...clientOpts, connectionLimit: 1 });
const execute = (text, values) => pool.execute(text, values || undefined);

async function allFortunes() {
return knex("Fortune").select("*");
return execute("select id, message from fortune", []);
}

async function getWorld(id) {
return knex("World")
.first()
.where({ id });
return execute("select id, randomNumber from world where id = ?", [id]).then(
(arr) => arr[0]
);
}

async function saveWorlds(worlds) {
const updates = [];

worlds.forEach(world => {
const { id, randomNumber } = world;

updates.push(
knex("World")
.update({ randomNumber })
.where({ id })
);
});

return Promise.all(updates);
async function bulkUpdate(worlds) {
const sql = "update world set randomNumber = ? where id = ?";
const values = worlds
.map((world) => [world.randomnumber, world.id])
.sort((a, b) => (a[0] < b[0] ? -1 : 1));
return pool.batch(sql, values);
}

module.exports = {
getWorld,
saveWorlds,
allFortunes
bulkUpdate,
allFortunes,
};
53 changes: 25 additions & 28 deletions frameworks/JavaScript/fastify/db/postgres.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
const knex = require("knex")({
client: "pg",
connection: {
host: "tfb-database",
user: "benchmarkdbuser",
password: "benchmarkdbpass",
database: "hello_world"
}
});
const postgres = require("postgres");

const clientOpts = {
host: process.env.PG_HOST,
user: process.env.PG_USER,
password: process.env.PG_PSWD,
database: process.env.PG_DBNAME,
};

const sql = postgres({ ...clientOpts, max: 1 });

async function allFortunes() {
return knex("Fortune").select("*");
return sql`select id, message from fortune`;
}

async function getWorld(id) {
return knex("World")
.first()
.where({ id });
return sql`select id, randomNumber from world where id = ${id}`.then(
(arr) => arr[0]
);
}

async function saveWorlds(worlds) {
const updates = [];

worlds.forEach(world => {
const { id, randomNumber } = world;

updates.push(
knex("World")
.update({ randomnumber: randomNumber })
.where({ id })
);
});
async function bulkUpdate(worlds) {
const values = sql(
worlds
.map((world) => [world.id, world.randomnumber])
.sort((a, b) => (a[0] < b[0] ? -1 : 1))
);

return Promise.all(updates);
return sql`update world set randomNumber = (update_data.randomNumber)::int
from (values ${values}) as update_data (id, randomNumber)
where world.id = (update_data.id)::int`;
}

module.exports = {
getWorld,
saveWorlds,
allFortunes
bulkUpdate,
allFortunes,
};
6 changes: 5 additions & 1 deletion frameworks/JavaScript/fastify/fastify-mysql.dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
FROM node:20.12.2-alpine
FROM node:20.16-slim

COPY ./ ./

RUN npm install

ENV NODE_ENV production
ENV DATABASE mysql
ENV MYSQL_HOST tfb-database
ENV MYSQL_USER benchmarkdbuser
ENV MYSQL_PSWD benchmarkdbpass
ENV MYSQL_DBNAME hello_world

EXPOSE 8080

Expand Down
6 changes: 5 additions & 1 deletion frameworks/JavaScript/fastify/fastify-postgres.dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
FROM node:20.12.2-alpine
FROM node:20.16-slim

COPY ./ ./

RUN npm install

ENV NODE_ENV production
ENV DATABASE postgres
ENV PG_HOST tfb-database
ENV PG_USER benchmarkdbuser
ENV PG_PSWD benchmarkdbpass
ENV PG_DBNAME hello_world

EXPOSE 8080

Expand Down
3 changes: 1 addition & 2 deletions frameworks/JavaScript/fastify/fastify.dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
FROM node:20.12.2-alpine
FROM node:20.16-slim

COPY ./ ./

RUN npm install

ENV NODE_ENV production
ENV DATABASE mongo

EXPOSE 8080

Expand Down
20 changes: 10 additions & 10 deletions frameworks/JavaScript/fastify/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const h = require("./helper");
* @param databaseLayer
* @returns {{singleQuery: function(*), multipleQueries: function(*), fortunes: function(*), updates: function(*)}}
*/
module.exports = databaseLayer => ({
module.exports = (databaseLayer) => ({
singleQuery: async (req, reply) => {
const world = await databaseLayer.getWorld(h.randomTfbNumber());

Expand Down Expand Up @@ -34,29 +34,29 @@ module.exports = databaseLayer => ({
},

updates: async (req, reply) => {
const queries = h.getQueries(req.query.queries);
const num = h.getQueries(req.query.queries);
const worldPromises = [];

for (let i = 0; i < queries; i++) {
worldPromises.push(databaseLayer.getWorld(h.randomTfbNumber()));
for (let i = 0; i < num; i++) {
const id = h.randomTfbNumber();
worldPromises.push(databaseLayer.getWorld(id));
}

const worlds = await Promise.all(worldPromises);

const worldsToUpdate = worlds.map(world => {
world.randomNumber = h.randomTfbNumber();
const worldsToUpdate = worlds.map((world) => {
world.randomnumber = h.randomTfbNumber();
return world;
});

await databaseLayer.saveWorlds(worldsToUpdate);

await databaseLayer.bulkUpdate(worldsToUpdate);
reply.send(worldsToUpdate);
}
},
});

// faster than localeCompare
function compare(a, b) {
if(a.message < b.message){
if (a.message < b.message) {
return -1;
} else if (a.message > b.message) {
return 1;
Expand Down
6 changes: 3 additions & 3 deletions frameworks/JavaScript/fastify/helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = {
randomTfbNumber: () => Math.floor(Math.random() * 10000) + 1,

getQueries: queries => {
getQueries: (queries) => {
return Math.min(Math.max(parseInt(queries) || 1, 1), 500);
},

additionalFortune: {
id: 0,
message: "Additional fortune added at request time."
}
message: "Additional fortune added at request time.",
},
};
Loading

0 comments on commit 56e5498

Please sign in to comment.