Browse Source

Merge branch 'improve-sailjs' of https://github.com/zane-techempower/FrameworkBenchmarks into 1652

Conflicts:
	frameworks/JavaScript/sailsjs/benchmark_config.json
msmith-techempower 10 years ago
parent
commit
c732d5fa6e

+ 0 - 167
frameworks/JavaScript/sailsjs/api/controllers/DatabaseQueryController.js

@@ -1,167 +0,0 @@
-/**
- * DatabaseQueryController
- *
- * @description :: Server-side logic for managing Database Queries
- */
-
-var Sequelize = require('sequelize')
-var sequelize = new Sequelize(
-  'hello_world', 'benchmarkdbuser', 'benchmarkdbpass',
-  {
-    host: '127.0.0.1',
-    dialect: 'mysql',
-    pool: {
-      max: 5000,
-      min: 0,
-      idle: 5000
-    },
-    // hide the SQL queries being run
-    logging: false
-  });
-
-
-var World = sequelize.define('World', {
-  id: Sequelize.INTEGER,
-  randomNumber: Sequelize.INTEGER
-},
-{
-  // prevents sequelize from assuming the table is called 'Worlds'
-  freezeTableName: true,
-  timestamps: false
-});
-
-
-var Fortune = sequelize.define('Fortune', {
-  id: Sequelize.INTEGER,
-  message: Sequelize.STRING
-},
-{
-  // prevents sequelize from assuming the table is called 'Fortunes'
-  freezeTableName: true,
-  timestamps: false
-});
-
-
-var randomTFBnumber = function() {
-  return Math.floor(Math.random() * 10000) + 1;
-}
-
-
-var worldQuery = function(callback) {
-  World.findOne({
-    where: { id: randomTFBnumber()}
-  }).then(function (result) {
-    if (result) {
-      callback(null, result.get())
-    } else {
-      callback("Error in World query")
-    }
-  });
-}
-
-// arr is single-element array containing number of updated rows
-// [ 1 ] or [ 0 ]
-var oneOrMoreUpdates = function (arr) {
-  return arr[0] > 0;
-}
-
-
-var worldUpdate = function(world, callback) {
-  World.update({
-    randomNumber: world.randomNumber
-  },
-  {
-    where: {
-      id: world.id
-    }
-  }).then(function (changed) {
-    if (oneOrMoreUpdates(changed)) {
-      callback(null, world);
-    } else {
-      callback("Error in World update");
-    }
-  });
-}
-
-
-module.exports = {
-
-  /**
-   * Test 2: Single Database Query
-   */
-  single: function(req, res) {
-    World.findOne({
-      where: { id: randomTFBnumber() }
-    }).then(function (results) {
-      return res.json(results.get());
-    })
-  },
-
-
-  /**
-   * Test 3: Multiple Database Query
-   */
-  multiple: function(req, res) {
-    var queries = req.param('queries');
-    var toRun = [];
-
-    queries = Math.min(Math.max(queries, 1), 500) || 1;
-
-    for (var i = 0; i < queries; i++) {
-      toRun.push(worldQuery);
-    }
-
-    async.parallel(toRun, function (err, results) {
-      if (!err) {
-        res.json(results);
-      } else {
-        res.badRequest('Unexpected failure to fetch multiple records.');
-      }
-    });
-  },
-
-
-  /**
-   * Test 4: Fortunes
-   */
-  fortunes: function(req, res) {
-    Fortune.findAll().then(function (fortunes) {
-      fortunes.push({
-        id: 0,
-        message: "Additional fortune added at request time."
-      });
-      fortunes.sort(function (a, b) {
-        return a.message.localeCompare(b.message);
-      });
-      return res.render('fortunes', { 'fortunes': fortunes });
-    })
-  },
-
-
-  /**
-   * Test 5: Database Updates
-   */
-  updates: function(req, res) {
-    var queries = req.param('queries');
-    var worlds = [];
-
-    queries = Math.min(Math.max(queries, 1), 500) || 1;
-
-    for (var i = 0; i < queries; i++) {
-      worlds.push({
-        id: randomTFBnumber(),
-        randomNumber: randomTFBnumber()
-      });
-    }
-
-    async.map(worlds, worldUpdate, function (err, results) {
-      if (!err) {
-        res.json(results);
-      } else {
-        res.badRequest('Unexpected failure to update records.');
-      }
-    });
-  }
-  
-};
-

+ 136 - 0
frameworks/JavaScript/sailsjs/api/controllers/RedisController.js

@@ -0,0 +1,136 @@
+/**
+ * RedisController
+ *
+ * @description :: Connects to Redis using the node_redis and hiredis drivers
+ *   Handles redis routes
+ *   "If hiredis [pure C library] is installed, node_redis will use it by default.
+ *   Otherwise, a pure JavaScript parser will be used."
+ *   >> hiredis is installed for these tests
+ */
+
+var h = require('../services/helper')
+var Promise = require('bluebird')
+// Can treat redis library as one that supports Promises
+// these methods will then have "-Async" appended to them.
+var redis = Promise.promisifyAll(require('redis'))
+var client = redis.createClient()
+
+client.on('error', function (err) {
+  console.log('Redis Error: ' + err)
+  // Do nothing further if Redis errors/is unavailable
+});
+
+function redisWorldId(id) {
+  return 'world:' + id
+}
+
+function randomWorldPromise() {
+  var id = h.randomTfbNumber()
+  var redisId = redisWorldId(id)
+
+  var promise = client.getAsync(redisId)
+    .then(function (worldValue) {
+      return {
+        id: id,
+        randomNumber: worldValue
+      }
+    })
+    .catch(function (err) {
+      console.log(err.stack)
+    })
+  return promise
+}
+
+function redisSetWorld(world) {
+  var redisId = redisWorldId(world.id)
+  var promise = client
+    .setAsync(redisId, world.randomNumber)
+    .then(function (result) {
+      return world
+    })
+    .catch(function (err) {
+      console.log(err.stack)
+    })
+  return promise
+}
+
+function redisGetAllFortunes() {
+  var promise = client
+    .lrangeAsync('fortunes', 0, -1)
+    .then(function (fortuneMessages) {
+      var fortunes = fortuneMessages.map(function (e, i) {
+        return { id: i + 1, message: e }
+      })
+      return fortunes;
+    })
+    .catch(function (err) {
+      console.log(err.stack)
+    })
+  return promise
+}
+
+
+module.exports = {
+  
+  Single: function(req, res) {
+    randomWorldPromise()
+      .then(function (world) {
+        res.json(world)
+      })
+      .catch(function (err) {
+        console.log(err.stack)
+      })
+  },
+
+  Multiple: function(req, res) {
+    var queries = h.getQueries(req)
+    var worldPromises = []
+
+    for (var i = 0; i < queries; i++) {
+      worldPromises.push(randomWorldPromise())
+    }
+
+    Promise
+      .all(worldPromises)
+      .then(function (worlds) {
+         res.json(worlds)
+      });
+  },
+
+  Fortunes: function(req, res) {
+    redisGetAllFortunes()
+      .then(function (fortunes) {
+        fortunes.push(h.ADDITIONAL_FORTUNE)
+        fortunes.sort(function (a, b) {
+          return a.message.localeCompare(b.message)
+        })
+        res.render('fortunes', { fortunes: fortunes })
+      })
+      .catch(function (err) {
+        console.log(err.stack)
+      })
+  },
+
+  Updates: function(req, res) {
+    var queries = h.getQueries(req)
+    var worldPromises = []
+
+    for (var i = 0; i < queries; i++) {
+      worldPromises.push(randomWorldPromise())
+    }
+
+    Promise
+      .all(worldPromises)
+      .map(function (world) {
+        world.randomNumber = h.randomTfbNumber()
+        return redisSetWorld(world)
+      })
+      .then(function (updated) {
+        res.json(updated)
+      })
+      .catch(function (err) {
+        console.log(err.stack)
+      })
+  }
+
+};

+ 154 - 0
frameworks/JavaScript/sailsjs/api/controllers/SequelizeMySQLController.js

@@ -0,0 +1,154 @@
+/**
+ * SequelizeMySQLController
+ *
+ * @description :: Server-side logic for managing Database Queries
+ */
+
+var h = require('../services/helper')
+var Promise = require('bluebird')
+
+var Sequelize = require('sequelize')
+var sequelize = new Sequelize(
+  'hello_world', 'benchmarkdbuser', 'benchmarkdbpass',
+  {
+    host: '127.0.0.1',
+    dialect: 'mysql',
+    pool: {
+      max: 5000,
+      min: 0,
+      idle: 5000
+    },
+    // hide the SQL queries being run
+    logging: false
+  })
+
+
+var Worlds = sequelize.define('World', {
+  id: Sequelize.INTEGER,
+  randomNumber: Sequelize.INTEGER
+},
+{
+  // prevents sequelize from assuming the table is called 'Worlds'
+  freezeTableName: true,
+  timestamps: false
+})
+
+
+var Fortunes = sequelize.define('Fortune', {
+  id: Sequelize.INTEGER,
+  message: Sequelize.STRING
+},
+{
+  // prevents sequelize from assuming the table is called 'Fortunes'
+  freezeTableName: true,
+  timestamps: false
+})
+
+
+var randomWorldPromise = function() {
+  var promise = Worlds
+    .findOne({
+      where: { id: h.randomTfbNumber() }
+    })
+    .then(function (world) {
+      return world
+    })
+    .catch(function (err) {
+      process.exit(1)
+    })
+  return promise
+}
+
+var updateWorld = function(world) {
+  var promise = Worlds
+    .update(
+      { randomNumber: world.randomNumber },
+      { where: { id: world.id } }
+    )
+    .then(function (results) {
+      return world
+    })
+    .catch(function (err) {
+      process.exit(1)
+    })
+  return promise
+}
+
+
+module.exports = {
+
+  /**
+   * Test 2: Single Database Query
+   */
+  Single: function(req, res) {
+    randomWorldPromise()
+      .then(function (world) {
+        res.json(world)
+      })
+  },
+
+
+  /**
+   * Test 3: Multiple Database Query
+   */
+  Multiple: function(req, res) {
+    var queries = h.getQueries(req)
+    var toRun = []
+
+    for (var i = 0; i < queries; i++) {
+      toRun.push(randomWorldPromise());
+    }
+
+    Promise
+      .all(toRun)
+      .then(function (worlds) {
+        res.json(worlds)
+      })
+  },
+
+
+  /**
+   * Test 4: Fortunes
+   */
+  Fortunes: function(req, res) {
+    Fortunes
+      .findAll()
+      .then(function (fortunes) {
+        fortunes.push(h.ADDITIONAL_FORTUNE)
+        fortunes.sort(function (a, b) {
+          return a.message.localeCompare(b.message)
+        })
+        res.render('fortunes', { 'fortunes': fortunes })
+      })
+      .catch(function (err) {
+        process.exit(1)
+      })
+  },
+
+
+  /**
+   * Test 5: Database Updates
+   */
+  Updates: function(req, res) {
+    var queries = h.getQueries(req);
+    var worldPromises = [];
+
+    for (var i = 0; i < queries; i++) {
+      worldPromises.push(randomWorldPromise())
+    }
+
+    Promise
+      .all(worldPromises)
+      .map(function (world) {
+        return updateWorld(world)
+      })
+      .then(function (updated) {
+        res.json(updated)
+      })
+      .catch(function (err) {
+        process.exit(1)
+      })
+  }
+  
+};
+

+ 155 - 0
frameworks/JavaScript/sailsjs/api/controllers/SequelizePostgresController.js

@@ -0,0 +1,155 @@
+/**
+ * SequelizePostgresController
+ *
+ * @description :: Server-side logic for managing Database Queries
+ */
+
+var h = require('../services/helper')
+var Promise = require('bluebird')
+
+var Sequelize = require('sequelize')
+var sequelize = new Sequelize(
+  'hello_world', 'benchmarkdbuser', 'benchmarkdbpass',
+  {
+    host: '127.0.0.1',
+    dialect: 'postgres',
+    pool: {
+      max: 5000,
+      min: 0,
+      idle: 5000
+    },
+    // hide the SQL queries being run
+    logging: false
+  })
+
+
+var Worlds = sequelize.define('World', {
+  id: Sequelize.INTEGER,
+  randomnumber: Sequelize.INTEGER
+},
+{
+  // prevents sequelize from assuming the table is called 'Worlds'
+  freezeTableName: true,
+  timestamps: false
+})
+
+
+var Fortunes = sequelize.define('Fortune', {
+  id: Sequelize.INTEGER,
+  message: Sequelize.STRING
+},
+{
+  // prevents sequelize from assuming the table is called 'Fortunes'
+  freezeTableName: true,
+  timestamps: false
+})
+
+
+var randomWorldPromise = function() {
+  var promise = Worlds
+    .findOne({
+      where: { id: h.randomTfbNumber() }
+    })
+    .then(function (world) {
+      return world
+    })
+    .catch(function (err) {
+      process.exit(1)
+    })
+  return promise
+}
+
+var updateWorld = function(world) {
+  var promise = Worlds
+    .update(
+      { randomNumber: world.randomNumber },
+      { where: { id: world.id } }
+    )
+    .then(function (results) {
+      return world
+    })
+    .catch(function (err) {
+      process.exit(1)
+    })
+  return promise
+}
+
+
+module.exports = {
+
+
+  /**
+   * Test 2: Single Database Query
+   */
+  Single: function(req, res) {
+    randomWorldPromise()
+      .then(function (world) {
+        res.json(world)
+      })
+  },
+
+
+  /**
+   * Test 3: Multiple Database Query
+   */
+  Multiple: function(req, res) {
+    var queries = h.getQueries(req)
+    var toRun = []
+
+    for (var i = 0; i < queries; i++) {
+      toRun.push(randomWorldPromise())
+    }
+
+    Promise
+      .all(toRun)
+      .then(function (worlds) {
+        res.json(worlds)
+      })
+  },
+
+
+  /**
+   * Test 4: Fortunes
+   */
+  Fortunes: function(req, res) {
+    Fortunes
+      .findAll()
+      .then(function (fortunes) {
+        fortunes.push(h.ADDITIONAL_FORTUNE)
+        fortunes.sort(function (a, b) {
+          return a.message.localeCompare(b.message)
+        })
+        res.render('fortunes', { 'fortunes': fortunes })
+      })
+      .catch(function (err) {
+        process.exit(1)
+      })
+  },
+
+
+  /**
+   * Test 5: Database Updates
+   */
+  Updates: function(req, res) {
+    var queries = h.getQueries(req)
+    var worldPromises = []
+
+    for (var i = 0; i < queries; i++) {
+      worldPromises.push(randomWorldPromise())
+    }
+
+    Promise
+      .all(worldPromises)
+      .map(function (world) {
+        return updateWorld(world)
+      })
+      .then(function (updated) {
+        res.json(updated)
+      })
+      .catch(function (err) {
+        process.exit(1)
+      })
+  }
+
+}
+

+ 2 - 2
frameworks/JavaScript/sailsjs/api/controllers/StaticTestController.js

@@ -12,7 +12,7 @@ module.exports = {
   /**
   /**
    * Test 1: JSON Serialization
    * Test 1: JSON Serialization
    */
    */
-  json: function (req, res) {
+  Json: function (req, res) {
     return res.json({
     return res.json({
       message: 'Hello, World!'
       message: 'Hello, World!'
     });
     });
@@ -21,7 +21,7 @@ module.exports = {
   /**
   /**
    * Test 6: Plaintext
    * Test 6: Plaintext
    */
    */
-  plaintext: function (req, res) {
+  Plaintext: function (req, res) {
   	res.setHeader('Content-Type', 'text/plain');
   	res.setHeader('Content-Type', 'text/plain');
   	return res.send("Hello, World!");
   	return res.send("Hello, World!");
   }
   }

+ 20 - 0
frameworks/JavaScript/sailsjs/api/services/helper.js

@@ -0,0 +1,20 @@
+
+
+module.exports = {
+
+  randomTfbNumber: function() {
+    return Math.floor(Math.random() * 10000) + 1
+  },
+
+  getQueries: function(req) {
+    var queries = req.param('queries')
+    queries = ~~(queries) || 1
+    return Math.min(Math.max(queries, 1), 500)
+  },
+
+  ADDITIONAL_FORTUNE: {
+    id: 0,
+    message: "Additional fortune added at request time."
+  }
+
+}

+ 48 - 6
frameworks/JavaScript/sailsjs/benchmark_config.json

@@ -4,18 +4,60 @@
     "default": {
     "default": {
       "setup_file": "setup",
       "setup_file": "setup",
       "json_url": "/json",
       "json_url": "/json",
-      "db_url": "/db",
-      "query_url": "/queries?queries=",
-      "fortune_url": "/fortunes",
-      "update_url": "/updates?queries=",
+      "db_url": "/mysql/db",
+      "query_url": "/mysql/queries?queries=",
+      "fortune_url": "/mysql/fortunes",
+      "update_url": "/mysql/updates?queries=",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Fullstack",
       "classification": "Fullstack",
-      "database": "MySQL",
+      "database": "None",
       "framework": "sailsjs",
       "framework": "sailsjs",
       "language": "JavaScript",
       "language": "JavaScript",
-      "orm": "raw",
+      "orm": "Micro",
+      "platform": "nodejs",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Sails.js",
+      "notes": "",
+      "versus": "node"
+    },
+    "postgres": {
+      "setup_file": "setup",
+      "db_url": "/postgres/db",
+      "query_url": "/postgres/queries?queries=",
+      "fortune_url": "/postgres/fortunes",
+      "update_url": "/postgres/updates?queries=",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "Postgres",
+      "framework": "sailsjs",
+      "language": "JavaScript",
+      "orm": "Full",
+      "platform": "nodejs",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Sails.js",
+      "notes": "",
+      "versus": "node"
+    },
+    "redis": {
+      "setup_file": "setup",
+      "db_url": "/hiredis/db",
+      "query_url": "/hiredis/queries?queries=",
+      "fortune_url": "/hiredis/fortunes",
+      "update_url": "/hiredis/updates?queries=",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "Redis",
+      "framework": "sailsjs",
+      "language": "JavaScript",
+      "orm": "Full",
       "platform": "nodejs",
       "platform": "nodejs",
       "webserver": "None",
       "webserver": "None",
       "os": "Linux",
       "os": "Linux",

+ 20 - 7
frameworks/JavaScript/sailsjs/config/routes.js

@@ -36,13 +36,6 @@ module.exports.routes = {
     view: 'homepage'
     view: 'homepage'
   },
   },
 
 
-  'get /json':      'StaticTestController.json',
-  'get /db':        'DatabaseQueryController.single',
-  'get /queries':   'DatabaseQueryController.multiple',
-  'get /fortunes':  'DatabaseQueryController.fortunes',
-  'get /updates':   'DatabaseQueryController.updates',
-  'get /plaintext': 'StaticTestController.plaintext'
-
   /***************************************************************************
   /***************************************************************************
   *                                                                          *
   *                                                                          *
   * Custom routes here...                                                    *
   * Custom routes here...                                                    *
@@ -53,4 +46,24 @@ module.exports.routes = {
   *                                                                          *
   *                                                                          *
   ***************************************************************************/
   ***************************************************************************/
 
 
+  // TFB routes here
+
+  'get /json':              'StaticTestController.Json',
+  'get /plaintext':         'StaticTestController.Plaintext',
+
+  'get /mysql/db':          'SequelizeMySQLController.Single',
+  'get /mysql/queries':     'SequelizeMySQLController.Multiple',
+  'get /mysql/fortunes':    'SequelizeMySQLController.Fortunes',
+  'get /mysql/updates':     'SequelizeMySQLController.Updates',
+
+  'get /postgres/db':       'SequelizePostgresController.Single',
+  'get /postgres/queries':  'SequelizePostgresController.Multiple',
+  'get /postgres/fortunes': 'SequelizePostgresController.Fortunes',
+  'get /postgres/updates':  'SequelizePostgresController.Updates',
+
+  'get /hiredis/db':        'RedisController.Single',
+  'get /hiredis/queries':   'RedisController.Multiple',
+  'get /hiredis/fortunes':  'RedisController.Fortunes',
+  'get /hiredis/updates':   'RedisController.Updates'
+
 };
 };

+ 26 - 21
frameworks/JavaScript/sailsjs/package.json

@@ -2,30 +2,35 @@
   "name": "sailsjs",
   "name": "sailsjs",
   "private": true,
   "private": true,
   "version": "0.0.0",
   "version": "0.0.0",
-  "description": "a Sails application",
+  "description": "a Sails application for TFB testing",
   "keywords": [],
   "keywords": [],
   "dependencies": {
   "dependencies": {
-    "async": "^0.9.0",
-    "ejs": "~0.8.4",
-    "grunt": "0.4.2",
-    "grunt-contrib-clean": "~0.5.0",
-    "grunt-contrib-coffee": "~0.10.1",
-    "grunt-contrib-concat": "~0.3.0",
-    "grunt-contrib-copy": "~0.5.0",
-    "grunt-contrib-cssmin": "~0.9.0",
+    "async": "^1.2.0",
+    "bluebird": "^2.9.27",
+    "ejs": "~2.3.1",
+    "grunt": "0.4.5",
+    "grunt-contrib-clean": "~0.6.0",
+    "grunt-contrib-coffee": "~0.13.0",
+    "grunt-contrib-concat": "~0.5.1",
+    "grunt-contrib-copy": "~0.8.0",
+    "grunt-contrib-cssmin": "~0.12.3",
     "grunt-contrib-jst": "~0.6.0",
     "grunt-contrib-jst": "~0.6.0",
-    "grunt-contrib-less": "0.11.1",
-    "grunt-contrib-uglify": "~0.4.0",
-    "grunt-contrib-watch": "~0.5.3",
-    "grunt-sails-linker": "~0.9.5",
-    "grunt-sync": "~0.0.4",
+    "grunt-contrib-less": "1.0.1",
+    "grunt-contrib-uglify": "~0.9.1",
+    "grunt-contrib-watch": "~0.6.1",
+    "grunt-sails-linker": "~0.10.1",
+    "grunt-sync": "~0.2.3",
     "handlebars": "^3.0.3",
     "handlebars": "^3.0.3",
-    "include-all": "~0.1.3",
-    "mysql": "^2.6.2",
-    "rc": "~0.5.0",
+    "hiredis": "^0.4.0",
+    "include-all": "~0.1.6",
+    "mysql": "^2.7.0",
+    "pg": "^4.4.0",
+    "pg-hstore": "^2.3.2",
+    "rc": "~1.0.3",
+    "redis": "^0.12.1",
     "sails": "~0.11.0",
     "sails": "~0.11.0",
-    "sails-disk": "~0.10.0",
-    "sequelize": "^3.0.0"
+    "sails-disk": "~0.10.8",
+    "sequelize": "^3.2.0"
   },
   },
   "scripts": {
   "scripts": {
     "start": "node app.js",
     "start": "node app.js",
@@ -34,8 +39,8 @@
   "main": "app.js",
   "main": "app.js",
   "repository": {
   "repository": {
     "type": "git",
     "type": "git",
-    "url": "git://github.com/zane/sailsjs.git"
+    "url": "https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/JavaScript/sailsjs"
   },
   },
-  "author": "zane",
+  "author": "zane-techempower",
   "license": ""
   "license": ""
 }
 }