Browse Source

refactor(javascript/fastify): upgrade deps, removed mongodb (failed with updates). (#9402)

Костя Третяк 8 months ago
parent
commit
56e54982cd

+ 0 - 30
frameworks/JavaScript/fastify/README.md

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

+ 2 - 7
frameworks/JavaScript/fastify/benchmark_config.json

@@ -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",
@@ -42,7 +37,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "fastify",
+        "display_name": "fastify [mysql]",
         "notes": "",
         "versus": "nodejs"
       },
@@ -63,7 +58,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "fastify",
+        "display_name": "fastify [postgres]",
         "notes": "",
         "versus": "nodejs"
       }

+ 0 - 5
frameworks/JavaScript/fastify/config.toml

@@ -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"

+ 5 - 0
frameworks/JavaScript/fastify/create-server.js

@@ -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")

+ 0 - 47
frameworks/JavaScript/fastify/db/mongo.js

@@ -1,47 +0,0 @@
-const { MongoClient } = require("mongodb");
-
-const mongoUrl = "mongodb://tfb-database:27017";
-const dbName = "hello_world";
-
-let client;
-
-async function getCollection(name) {
-  if (!client) {
-    client = await new MongoClient(mongoUrl).connect();
-  }
-
-  const db = client.db(dbName);
-
-  return db.collection(name);
-}
-
-async function allFortunes() {
-  const collection = await getCollection("fortune");
-  const fortunes = await collection.find({}, { projection: { _id: 0 } });
-
-  return fortunes.toArray();
-}
-
-async function getWorld(id) {
-  const collection = await getCollection("world");
-
-  return collection.findOne({ id }, { projection: { _id: 0 } });
-}
-
-async function saveWorlds(worlds) {
-  const collection = await getCollection("world");
-
-  const bulk = collection.initializeUnorderedBulkOp();
-
-  worlds.forEach(world => {
-    bulk.find({ id: world.id }).updateOne(world);
-  });
-
-  return bulk.execute();
-}
-
-module.exports = {
-  getWorld,
-  saveWorlds,
-  allFortunes
-};

+ 23 - 29
frameworks/JavaScript/fastify/db/mysql.js

@@ -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,
 };

+ 25 - 28
frameworks/JavaScript/fastify/db/postgres.js

@@ -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,
 };

+ 5 - 1
frameworks/JavaScript/fastify/fastify-mysql.dockerfile

@@ -1,4 +1,4 @@
-FROM node:20.12.2-alpine
+FROM node:20.16-slim
 
 COPY ./ ./
 
@@ -6,6 +6,10 @@ 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
 

+ 5 - 1
frameworks/JavaScript/fastify/fastify-postgres.dockerfile

@@ -1,4 +1,4 @@
-FROM node:20.12.2-alpine
+FROM node:20.16-slim
 
 COPY ./ ./
 
@@ -6,6 +6,10 @@ 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
 

+ 1 - 2
frameworks/JavaScript/fastify/fastify.dockerfile

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

+ 10 - 10
frameworks/JavaScript/fastify/handlers.js

@@ -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());
 
@@ -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;

+ 3 - 3
frameworks/JavaScript/fastify/helper.js

@@ -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.",
+  },
 };

+ 5 - 7
frameworks/JavaScript/fastify/package.json

@@ -5,12 +5,10 @@
   "main": "app.js",
   "private": true,
   "dependencies": {
-    "@fastify/view": "7.4.1",
-    "fastify": "4.13.0",
-    "handlebars": "4.7.6",
-    "knex": "2.4.2",
-    "mongodb": "6.5.0",
-    "mysql2": "3.9.7",
-    "pg": "8.5.1"
+    "@fastify/view": "^10.0.1",
+    "fastify": "^5.1.0",
+    "handlebars": "4.7.8",
+    "mariadb": "^3.4.0",
+    "postgres": "^3.4.5"
   }
 }