ソースを参照

Add promisified-redis tests

Zane Kansil 10 年 前
コミット
92c86831e0

+ 21 - 0
frameworks/JavaScript/hapi/benchmark_config.json

@@ -82,6 +82,27 @@
       "display_name": "hapi",
       "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": "Micro",
+      "database": "Redis",
+      "framework": "hapi",
+      "language": "JavaScript",
+      "orm": "Full",
+      "platform": "nodejs",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "hapi",
+      "notes": "",
+      "versus": "node"
     }
   }]
 }

+ 6 - 0
frameworks/JavaScript/hapi/create-server.js

@@ -14,6 +14,7 @@ server.views({
 var MongooseHandler = require('./handlers/mongoose');
 var SequelizeHandler = require('./handlers/sequelize');
 var SequelizePgHandler = require('./handlers/sequelize-postgres');
+var RedisHandler = require('./handlers/redis');
 
 Route('/json', JsonSerialization);
 Route('/plaintext', Plaintext);
@@ -33,6 +34,11 @@ Route('/sequelize-pg/queries', SequelizePgHandler.MultipleQueries);
 Route('/sequelize-pg/fortunes', SequelizePgHandler.Fortunes);
 Route('/sequelize-pg/updates', SequelizePgHandler.Updates);
 
+Route('/hiredis/db', RedisHandler.SingleQuery);
+Route('/hiredis/queries', RedisHandler.MultipleQueries);
+Route('/hiredis/fortunes', RedisHandler.Fortunes);
+Route('/hiredis/updates', RedisHandler.Updates);
+
 function JsonSerialization(req, reply) {
   reply({ message: 'Hello, World!' })
     .header('Server', 'hapi');

+ 132 - 0
frameworks/JavaScript/hapi/handlers/redis.js

@@ -0,0 +1,132 @@
+// Connects to Redis using the node_redis and hiredis drivers
+// Handles related 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('../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) {
+      process.exit(1);
+    });
+  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) {
+      process.exit(1);
+    });
+  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) {
+      if (err) { return process.exit(1); }
+    });
+  return promise;
+}
+
+
+module.exports = {
+  
+  SingleQuery: function(req, reply) {
+    randomWorldPromise()
+      .then(function (world) {
+        reply(world)
+          .header('Server', 'hapi');
+      })
+      .catch(function (err) {
+        if (err) { return process.exit(1); }
+      })
+  },
+
+  MultipleQueries: function(req, reply) {
+    var queries = h.getQueries(req);
+    var worldPromises = h.fillArray(randomWorldPromise(), queries);
+
+    Promise
+      .all(worldPromises)
+      .then(function (worlds) {
+         reply(worlds)
+          .header('Server', 'hapi');
+      });
+  },
+
+  Fortunes: function(req, reply) {
+    redisGetAllFortunes()
+      .then(function (fortunes) {
+        fortunes.push(h.ADDITIONAL_FORTUNE);
+        fortunes.sort(function (a, b) {
+          return a.message.localeCompare(b.message);
+        });
+
+        reply.view('fortunes', {
+          fortunes: fortunes
+        })
+          .header('Content-Type', 'text/html');
+      })
+      .catch(function (err) {
+        process.exit(1);
+      })
+  },
+
+  Updates: function(req, reply) {
+    var queries = h.getQueries(req)
+    var worldPromises = h.fillArray(randomWorldPromise(), queries);
+
+    Promise
+      .all(worldPromises)
+      .map(function (world) {
+        world.randomNumber = h.randomTfbNumber();
+        return redisSetWorld(world);
+      })
+      .then(function (updated) {
+        reply(updated)
+          .header('Server', 'hapi');
+      })
+      .catch(function (err) {
+        process.exit(1);
+      });
+  }
+
+};

+ 8 - 8
frameworks/JavaScript/hapi/handlers/sequelize-postgres.js

@@ -33,7 +33,7 @@ var randomWorldPromise = function() {
   }).then(function (results) {
     return results;
   }).catch(function (err) {
-    console.log(err.stack);
+    process.exit(1);
   });
 }
 
@@ -73,7 +73,7 @@ module.exports = {
         .header('Content-Type', 'text/html')
         .header('Server', 'hapi');
     }).catch(function (err) {
-      console.log(err.stack);
+      process.exit(1);
     }); 
   },
 
@@ -93,11 +93,11 @@ module.exports = {
           { where: { id: world.id } }
         )
         .then(function (results) {
-            return world;
-          })
+          return world;
+        })
         .catch(function (err) {
-            console.log(err.stack);
-          });
+          process.exit(1);
+        });
     }
 
     Promise
@@ -109,8 +109,8 @@ module.exports = {
         reply(updated)
           .header('Server', 'hapi')
       })
-      .catch(function (e) {
-        console.log(err.stack);
+      .catch(function (err) {
+        process.exit(1);
       });
   }
 

+ 4 - 4
frameworks/JavaScript/hapi/handlers/sequelize.js

@@ -93,11 +93,11 @@ module.exports = {
           { where: { id: world.id } }
         )
         .then(function (results) {
-            return world;
-          })
+          return world;
+        })
         .catch(function (err) {
-            process.exit(1);
-          });
+          process.exit(1);
+        });
     }
 
     Promise

+ 2 - 0
frameworks/JavaScript/hapi/package.json

@@ -7,10 +7,12 @@
     "bluebird": "^2.9.27",
     "handlebars": "3.0.3",
     "hapi": "8.6.0",
+    "hiredis": "^0.4.0",
     "mongoose": "4.0.4",
     "mysql": "2.7.0",
     "pg": "^4.3.0",
     "pg-hstore": "^2.3.2",
+    "redis": "^0.12.1",
     "sequelize": "3.1.1"
   }
 }