Browse Source

Add Redis testing to nodejs + tweak config

Zane Kansil 10 years ago
parent
commit
b1ccb70249

+ 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)
+      })
+  }
+
+};

+ 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!");
   }
   }

+ 23 - 2
frameworks/JavaScript/sailsjs/benchmark_config.json

@@ -12,10 +12,10 @@
       "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": "Full",
+      "orm": "Micro",
       "platform": "nodejs",
       "platform": "nodejs",
       "webserver": "None",
       "webserver": "None",
       "os": "Linux",
       "os": "Linux",
@@ -44,6 +44,27 @@
       "display_name": "Sails.js",
       "display_name": "Sails.js",
       "notes": "",
       "notes": "",
       "versus": "node"
       "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",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Sails.js",
+      "notes": "",
+      "versus": "node"
     }
     }
   }]
   }]
 }
 }

+ 15 - 10
frameworks/JavaScript/sailsjs/config/routes.js

@@ -48,17 +48,22 @@ module.exports.routes = {
 
 
   // TFB routes here
   // TFB routes here
 
 
-  'get /json':              'StaticTestController.json',
-  'get /plaintext':         'StaticTestController.plaintext',
+  '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 /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 /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'
 
 
 };
 };

+ 5 - 3
frameworks/JavaScript/sailsjs/package.json

@@ -2,7 +2,7 @@
   "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": "^1.2.0",
     "async": "^1.2.0",
@@ -21,11 +21,13 @@
     "grunt-sails-linker": "~0.10.1",
     "grunt-sails-linker": "~0.10.1",
     "grunt-sync": "~0.2.3",
     "grunt-sync": "~0.2.3",
     "handlebars": "^3.0.3",
     "handlebars": "^3.0.3",
+    "hiredis": "^0.4.0",
     "include-all": "~0.1.6",
     "include-all": "~0.1.6",
     "mysql": "^2.7.0",
     "mysql": "^2.7.0",
     "pg": "^4.4.0",
     "pg": "^4.4.0",
     "pg-hstore": "^2.3.2",
     "pg-hstore": "^2.3.2",
     "rc": "~1.0.3",
     "rc": "~1.0.3",
+    "redis": "^0.12.1",
     "sails": "~0.11.0",
     "sails": "~0.11.0",
     "sails-disk": "~0.10.8",
     "sails-disk": "~0.10.8",
     "sequelize": "^3.2.0"
     "sequelize": "^3.2.0"
@@ -37,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": ""
 }
 }