ソースを参照

Update Express.js (node version) (#6706)

* Fix to use sequelize without using pg

* Fixed Expressjs
JaeHyeok Kim 4 年 前
コミット
543ec196fb

+ 0 - 65
frameworks/JavaScript/express/benchmark_config.json

@@ -95,71 +95,6 @@
       "display_name": "express",
       "notes": "",
       "versus": "nodejs"
-    },
-    "graphql-mysql": {
-      "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",
-      "framework": "express",
-      "language": "JavaScript",
-      "flavor": "NodeJS",
-      "orm": "Full",
-      "platform": "NodeJS",
-      "webserver": "None",
-      "os": "Linux",
-      "database": "MySQL",
-      "database_os": "Linux",
-      "display_name": "express",
-      "notes": "",
-      "versus": "None"
-    },
-    "graphql-postgres": {
-      "db_url": "/db",
-      "query_url": "/queries?queries=",
-      "update_url": "/updates?queries=",
-      "fortune_url": "/fortunes",
-      "port": 8080,
-      "approach": "Realistic",
-      "classification": "Micro",
-      "framework": "express",
-      "language": "JavaScript",
-      "flavor": "NodeJS",
-      "orm": "Full",
-      "platform": "NodeJS",
-      "webserver": "None",
-      "os": "Linux",
-      "database": "Postgres",
-      "database_os": "Linux",
-      "display_name": "express",
-      "notes": "",
-      "versus": "None"
-    },
-    "graphql-mongodb": {
-      "db_url": "/db",
-      "query_url": "/queries?queries=",
-      "fortune_url": "/fortunes",
-      "update_url": "/updates?queries=",
-      "port": 8080,
-      "approach": "Realistic",
-      "classification": "Micro",
-      "framework": "express",
-      "language": "JavaScript",
-      "flavor": "NodeJS",
-      "orm": "Full",
-      "platform": "NodeJS",
-      "webserver": "None",
-      "os": "Linux",
-      "database": "MongoDB",
-      "database_os": "Linux",
-      "display_name": "express",
-      "notes": "",
-      "versus": "None"
     }
   }]
 }

+ 0 - 47
frameworks/JavaScript/express/config.toml

@@ -1,38 +1,6 @@
 [framework]
 name = "express"
 
-[graphql-postgres]
-urls.db = "/db"
-urls.query = "/queries?queries="
-urls.update = "/updates?queries="
-urls.fortune = "/fortunes"
-approach = "Realistic"
-classification = "Micro"
-database = "Postgres"
-database_os = "Linux"
-os = "Linux"
-orm = "Full"
-platform = "NodeJS"
-webserver = "None"
-versus = "None"
-
-[graphql-mysql]
-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 = "MySQL"
-database_os = "Linux"
-os = "Linux"
-orm = "Full"
-platform = "NodeJS"
-webserver = "None"
-versus = "None"
-
 [postgres]
 urls.db = "/db"
 urls.query = "/queries?queries="
@@ -99,18 +67,3 @@ os = "Linux"
 platform = "nodejs"
 webserver = "None"
 versus = "nodejs"
-
-[graphql-mongodb]
-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 = "Full"
-platform = "NodeJS"
-webserver = "None"
-versus = "None"

+ 0 - 11
frameworks/JavaScript/express/express-graphql-mongodb.dockerfile

@@ -1,11 +0,0 @@
-FROM node:14.15.0-slim
-
-COPY ./ ./
-
-RUN npm install
-
-ENV NODE_ENV production
-
-EXPOSE 8080
-
-CMD ["node", "graphql-mongodb-app.js"]

+ 0 - 11
frameworks/JavaScript/express/express-graphql-mysql.dockerfile

@@ -1,11 +0,0 @@
-FROM node:14.15.0-slim
-
-COPY ./ ./
-
-RUN npm install
-
-ENV NODE_ENV production
-
-EXPOSE 8080
-
-CMD ["node", "graphql-mysql-app.js"]

+ 0 - 11
frameworks/JavaScript/express/express-graphql-postgres.dockerfile

@@ -1,11 +0,0 @@
-FROM node:14.15.0-slim
-
-COPY ./ ./
-
-RUN npm install
-
-ENV NODE_ENV production
-
-EXPOSE 8080
-
-CMD ["node", "graphql-postgres-app.js"]

+ 1 - 1
frameworks/JavaScript/express/express-mongodb.dockerfile

@@ -1,4 +1,4 @@
-FROM node:14.15.0-slim
+FROM node:14.17.3-slim
 
 COPY ./ ./
 

+ 1 - 1
frameworks/JavaScript/express/express-mysql.dockerfile

@@ -1,4 +1,4 @@
-FROM node:14.15.0-slim
+FROM node:14.17.3-slim
 
 COPY ./ ./
 

+ 1 - 1
frameworks/JavaScript/express/express-postgres.dockerfile

@@ -1,4 +1,4 @@
-FROM node:14.15.0-slim
+FROM node:14.17.3-slim
 
 COPY ./ ./
 

+ 1 - 1
frameworks/JavaScript/express/express.dockerfile

@@ -1,4 +1,4 @@
-FROM node:14.15.0-slim
+FROM node:14.17.3-slim
 
 COPY ./ ./
 

+ 0 - 3
frameworks/JavaScript/express/package.json

@@ -7,9 +7,6 @@
     "dateformat": "3.0.3",
     "escape-html": "1.0.3",
     "express": "4.17.1",
-    "express-graphql": "0.6.12",
-    "graphql": "0.13.2",
-    "graphql-tools": "3.1.1",
     "mongoose": "5.7.5",
     "mysql2": "2.2.5",
     "pg": "8.5.0",

+ 72 - 37
frameworks/JavaScript/express/postgresql-app.js

@@ -1,26 +1,53 @@
-
 /**
  * Module dependencies.
  */
-
 const cluster = require('cluster'),
   numCPUs = require('os').cpus().length,
   express = require('express'),
-  pgp = require('pg-promise')(),
   helper = require('./helper');
 
 // Middleware
 const bodyParser = require('body-parser');
 
-const connection = {
-  db: 'hello_world',
-  username: 'benchmarkdbuser',
-  password: 'benchmarkdbpass',
+const Sequelize = require('sequelize');
+const sequelize = new Sequelize('hello_world', 'benchmarkdbuser', 'benchmarkdbpass', {
   host: 'tfb-database',
-  dialect: 'postgres'
-}
-
-const db = pgp(`postgres://${connection.username}:${connection.password}@${connection.host}:5432/${connection.db}`);
+  dialect: 'postgres',
+  logging: false,
+  pool: {
+    min: 20, max: 20
+  }
+});
+
+const Worlds = sequelize.define('world', {
+  id: {
+    type: 'Sequelize.INTEGER',
+    primaryKey: true
+  },
+  randomnumber: { type: 'Sequelize.INTEGER' }
+}, {
+  timestamps: false,
+  freezeTableName: true
+});
+
+const Fortunes = sequelize.define('fortune', {
+  id: {
+    type: 'Sequelize.INTEGER',
+    primaryKey: true
+  },
+  message: { type: 'Sequelize.STRING' }
+}, {
+  timestamps: false,
+  freezeTableName: true
+});
+
+const randomWorldPromise = () => {
+  return Worlds.findOne({
+    where: { id: helper.randomizeNum() }
+  }).then((results) => {
+    return results;
+  }).catch((err) => process.exit(1));
+};
 
 if (cluster.isMaster) {
   // Fork workers.
@@ -33,10 +60,7 @@ if (cluster.isMaster) {
 } else {
   const app = module.exports = express();
 
-  app.set('view engine', 'pug');
-  app.set('views', __dirname + '/views');
   app.use(bodyParser.urlencoded({ extended: true }));
-  app.use(bodyParser.json());
 
   // Set headers for all routes
   app.use((req, res, next) => {
@@ -44,27 +68,31 @@ if (cluster.isMaster) {
     return next();
   });
 
+  app.set('view engine', 'pug');
+  app.set('views', __dirname + '/views');
+
   // Routes
   app.get('/db', async (req, res) => {
-    let world = await getRandomWorld();
+    let world = await randomWorldPromise();
 
     res.setHeader("Content-Type", "application/json");
     res.json(world);
   });
 
   app.get('/queries', async (req, res) => {
-    const results = [],
-      queries = Math.min(parseInt(req.query.queries) || 1, 500);
+    const queries = Math.min(parseInt(req.query.queries) || 1, 500);
+
+    const promisesArray = [];
 
     for (let i = 0; i < queries; i++) {
-      results.push(await getRandomWorld());
+      promisesArray.push(randomWorldPromise());
     }
 
-    res.json(results)
+    res.json(await Promise.all(promisesArray))
   });
 
   app.get('/fortunes', async (req, res) => {
-    let fortunes = await getAllFortunes()
+    let fortunes = await Fortunes.findAll();
     const newFortune = { id: 0, message: "Additional fortune added at request time." };
     fortunes.push(newFortune);
     fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1);
@@ -73,28 +101,35 @@ if (cluster.isMaster) {
   });
 
   app.get('/updates', async (req, res) => {
-    const results = [],
-      queries = Math.min(parseInt(req.query.queries) || 1, 500);
+    const queries = Math.min(parseInt(req.query.queries) || 1, 500);
+    const worldPromises = [];
 
-    for (let i = 1; i <= queries; i++) {
-      results.push(await updateRandomWorld())
+    for (let i = 0; i < queries; i++) {
+      worldPromises.push(randomWorldPromise());
     }
 
-    res.json(results);
+    const worldUpdate = (world) => {
+      world.randomnumber = helper.randomizeNum();
+
+      return Worlds.update({
+            randomnumber: world.randomnumber
+          },
+          {
+            where: { id: world.id }
+          }).then((results) => {
+        return world;
+      }).catch((err) => process.exit(1));
+    };
+
+    Promise.all(worldPromises).then((worlds) => {
+      const updates = worlds.map((e) => worldUpdate(e));
+
+      Promise.all(updates).then((updated) => {
+        res.json(updated);
+      });
+    });
   });
 
-  const getRandomWorld = async () => {
-    return await db.one(`select * from world where id = ${helper.randomizeNum()}`, [true])
-  };
-
-  const updateRandomWorld = async () => {
-    return await db.oneOrNone(`update world set randomNumber = ${helper.randomizeNum()} where id = ${helper.randomizeNum()} returning id, randomNumber`, [true])
-  };
-
-  const getAllFortunes = async () => {
-    return await db.many('select * from fortune', [true]);
-  };
-
   app.listen(8080, () => {
     console.log('listening on port 8080');
   });