Christian Kvalheim %!s(int64=12) %!d(string=hai) anos
pai
achega
8a12f888b7
Modificáronse 2 ficheiros con 56 adicións e 21 borrados
  1. 55 21
      nodejs/hello.js
  2. 1 0
      nodejs/package.json

+ 55 - 21
nodejs/hello.js

@@ -1,6 +1,7 @@
 var cluster = require('cluster')
   , numCPUs = require('os').cpus().length;
 
+numCPUs = 1
 if (cluster.isMaster) {
   // Fork workers.
   for (var i = 0; i < numCPUs; i++) {
@@ -18,28 +19,36 @@ var http = require('http')
   , url = require('url')
   , async = require('async')
   , mongoose = require('mongoose')
-  , conn = mongoose.connect('mongodb://172.16.234.132/hello_world')
+  // , conn = mongoose.connect('mongodb://172.16.234.132/hello_world')
+  , conn = mongoose.connect('mongodb://localhost/hello_world')
+  , MongoClient = require('mongodb').MongoClient
   , mysql = require('mysql')
-  , pool  = mysql.createPool({
-      host: '172.16.234.132',
-      user     : 'benchmarkdbuser',
-      password : 'benchmarkdbpass',
-      database : 'hello_world',
-      connectionLimit : 256
-    })
-  , Sequelize = require("sequelize")
-  , sequelize = new Sequelize('hello_world', 'benchmarkdbuser', 'benchmarkdbpass', {
-    host: '172.16.234.132',
-    logging: false,
-    define: { timestamps: false },
-    maxConcurrentQueries: 100,
-    pool: { maxConnections: 800, maxIdleTime: 30 }
-  })
-  , World      = sequelize.define('World', {
-    randomNumber: Sequelize.INTEGER
-  }, {
-    freezeTableName: true
-  });
+  // , pool  = mysql.createPool({
+  //     host: '172.16.234.132',
+  //     user     : 'benchmarkdbuser',
+  //     password : 'benchmarkdbpass',
+  //     database : 'hello_world',
+  //     connectionLimit : 256
+  //   })
+  // , Sequelize = require("sequelize")
+  // , sequelize = new Sequelize('hello_world', 'benchmarkdbuser', 'benchmarkdbpass', {
+  //   host: '172.16.234.132',
+  //   logging: false,
+  //   define: { timestamps: false },
+  //   maxConcurrentQueries: 100,
+  //   pool: { maxConnections: 800, maxIdleTime: 30 }
+  // })
+  // , World      = sequelize.define('World', {
+  //   randomNumber: Sequelize.INTEGER
+  // }, {
+  //   freezeTableName: true
+  // });
+
+var collection = null;
+
+MongoClient.connect('mongodb://localhost:27017/hello_world?maxPoolSize=5', function(err, db) {
+  collection = db.collection('world');
+});
 
 // define model
 var Schema = mongoose.Schema
@@ -63,6 +72,14 @@ function mongooseQuery(callback) {
   });
 }
 
+function mongodbDriverQuery(callback) {
+  process.nextTick(function() {
+  collection.find({ id: getRandomNumber()}).toArray(function(err, world) {
+    callback(err, world[0]);
+  });    
+  })
+}
+
 function sequelizeQuery(callback) {
   World.find(getRandomNumber()).success(function (world) {
     callback(null, world);
@@ -83,6 +100,23 @@ http.createServer(function (req, res) {
     res.end(JSON.stringify(hello));
     break;
 
+  case '/mongodbdriver':
+    // Database Test
+    var values = url.parse(req.url, true);
+    var queries = values.query.queries || 1;
+    var queryFunctions = new Array(queries);
+
+    for (var i = 0; i < queries; i += 1) {
+      queryFunctions[i] = mongodbDriverQuery;
+    }
+
+    res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
+
+    async.parallel(queryFunctions, function(err, results) {
+      res.end(JSON.stringify(results));
+    });    
+    break;
+
   case '/mongoose':
     // Database Test
     var values = url.parse(req.url, true);

+ 1 - 0
nodejs/package.json

@@ -7,6 +7,7 @@
     , "sequelize": "1.6.0-beta4"
     , "async": "0.2.5"
     , "mysql": "2.0.0-alpha7"
+    , "mongodb": "1.2.10"
   }
   , "main": "hello.js"
 }