diff --git a/frameworks/JavaScript/fastify/README.md b/frameworks/JavaScript/fastify/README.md index 705c282aee8..fe63567a9d5 100644 --- a/frameworks/JavaScript/fastify/README.md +++ b/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 - diff --git a/frameworks/JavaScript/fastify/benchmark_config.json b/frameworks/JavaScript/fastify/benchmark_config.json index 944ae63a3ed..8ce94eac312 100644 --- a/frameworks/JavaScript/fastify/benchmark_config.json +++ b/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" } diff --git a/frameworks/JavaScript/fastify/config.toml b/frameworks/JavaScript/fastify/config.toml index c512a025989..d47b5822fa4 100644 --- a/frameworks/JavaScript/fastify/config.toml +++ b/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" diff --git a/frameworks/JavaScript/fastify/create-server.js b/frameworks/JavaScript/fastify/create-server.js index 5680ef31f04..fd0833223db 100644 --- a/frameworks/JavaScript/fastify/create-server.js +++ b/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") diff --git a/frameworks/JavaScript/fastify/db/mongo.js b/frameworks/JavaScript/fastify/db/mongo.js deleted file mode 100644 index 666dd49d412..00000000000 --- a/frameworks/JavaScript/fastify/db/mongo.js +++ /dev/null @@ -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 -}; diff --git a/frameworks/JavaScript/fastify/db/mysql.js b/frameworks/JavaScript/fastify/db/mysql.js index 3887cc20dd1..ed2c26b44a4 100644 --- a/frameworks/JavaScript/fastify/db/mysql.js +++ b/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, }; diff --git a/frameworks/JavaScript/fastify/db/postgres.js b/frameworks/JavaScript/fastify/db/postgres.js index 1d5820eeeac..45f62c5eee4 100644 --- a/frameworks/JavaScript/fastify/db/postgres.js +++ b/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, }; diff --git a/frameworks/JavaScript/fastify/fastify-mysql.dockerfile b/frameworks/JavaScript/fastify/fastify-mysql.dockerfile index a77917aceb0..8da0cc484d0 100644 --- a/frameworks/JavaScript/fastify/fastify-mysql.dockerfile +++ b/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 diff --git a/frameworks/JavaScript/fastify/fastify-postgres.dockerfile b/frameworks/JavaScript/fastify/fastify-postgres.dockerfile index 069b66c250a..e5b63795e13 100644 --- a/frameworks/JavaScript/fastify/fastify-postgres.dockerfile +++ b/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 diff --git a/frameworks/JavaScript/fastify/fastify.dockerfile b/frameworks/JavaScript/fastify/fastify.dockerfile index 48a7611a848..ec726dd2800 100644 --- a/frameworks/JavaScript/fastify/fastify.dockerfile +++ b/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 diff --git a/frameworks/JavaScript/fastify/handlers.js b/frameworks/JavaScript/fastify/handlers.js index df2d37fc378..5032b4b829d 100644 --- a/frameworks/JavaScript/fastify/handlers.js +++ b/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; diff --git a/frameworks/JavaScript/fastify/helper.js b/frameworks/JavaScript/fastify/helper.js index 8ed4447dbc4..6369564b902 100644 --- a/frameworks/JavaScript/fastify/helper.js +++ b/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.", + }, }; diff --git a/frameworks/JavaScript/fastify/package.json b/frameworks/JavaScript/fastify/package.json index c1a261a4328..4de1056bfe7 100644 --- a/frameworks/JavaScript/fastify/package.json +++ b/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" } }