hello.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var cluster = require('cluster')
  2. , numCPUs = require('os').cpus().length
  3. , windows = require('os').platform() == 'win32';
  4. if(cluster.isMaster) {
  5. // Fork workers.
  6. for (var i = 0; i < numCPUs; i++) {
  7. cluster.fork();
  8. }
  9. cluster.on('exit', function(worker, code, signal) {
  10. console.log('worker ' + worker.pid + ' died');
  11. });
  12. return;
  13. }
  14. var http = require('http')
  15. , url = require('url')
  16. , async = require('async')
  17. , mongoose = require('mongoose')
  18. , conn = mongoose.connect('mongodb://localhost/hello_world')
  19. , MongoClient = require('mongodb').MongoClient
  20. , connMap = { user: 'benchmarkdbuser', password: 'benchmarkdbpass', database: 'hello_world', host: 'localhost' };
  21. if (!windows) {
  22. var Mapper = require('mapper')
  23. , libmysql = require('mysql-libmysqlclient').createConnectionSync();
  24. Mapper.connect(connMap, {verbose: false, strict: false});
  25. var World = Mapper.map("World", "id", "randomNumber")
  26. libmysql.connectSync('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  27. }
  28. var collection = null;
  29. MongoClient.connect('mongodb://localhost/hello_world?maxPoolSize=5', function(err, db) {
  30. collection = db.collection('world');
  31. });
  32. // define model
  33. var Schema = mongoose.Schema
  34. , ObjectId = Schema.ObjectId;
  35. var WorldSchema = new Schema({
  36. id : Number
  37. , randomNumber : Number
  38. }, { collection : 'world' });
  39. var MWorld = conn.model('World', WorldSchema);
  40. function getRandomNumber() {
  41. return Math.floor(Math.random() * 10000) + 1;
  42. }
  43. function mongooseQuery(callback) {
  44. MWorld.findOne({ id: getRandomNumber()}).exec(function (err, world) {
  45. callback(err, world);
  46. });
  47. }
  48. function mongodbDriverQuery(callback) {
  49. collection.findOne({ id: getRandomNumber()}, function(err, world) {
  50. callback(err, world);
  51. });
  52. }
  53. function mongodbDriverUpdateQuery(callback) {
  54. collection.findAndModify({ id: getRandomNumber()}, [['_id','asc']], {$set: {randomNumber: getRandomNumber()}}, {}, function(err, world) {
  55. callback(err, world);
  56. });
  57. }
  58. function sequelizeQuery(callback) {
  59. World.findById(getRandomNumber(), function (err, world) {
  60. callback(null, world);
  61. });
  62. }
  63. http.createServer(function (req, res) {
  64. // JSON response object
  65. var hello = {message: "Hello, world"};
  66. var path = url.parse(req.url).pathname;
  67. // mysql on windows is not supported
  68. if (windows && (path.substr(0, 3) == '/my' || path == '/update')) {
  69. path = '/doesntexist';
  70. }
  71. switch (path) {
  72. case '/json':
  73. // JSON Response Test
  74. res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
  75. // Write JSON object to response
  76. res.end(JSON.stringify(hello));
  77. break;
  78. case '/mongodbdriver':
  79. // Database Test
  80. var values = url.parse(req.url, true);
  81. var queries = values.query.queries || 1;
  82. var queryFunctions = new Array(queries);
  83. for (var i = 0; i < queries; i += 1) {
  84. queryFunctions[i] = mongodbDriverQuery;
  85. }
  86. res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
  87. async.parallel(queryFunctions, function(err, results) {
  88. res.end(JSON.stringify(results));
  89. });
  90. break;
  91. case '/mongoose':
  92. // Database Test
  93. var values = url.parse(req.url, true);
  94. var queries = values.query.queries || 1;
  95. var queryFunctions = new Array(queries);
  96. for (var i = 0; i < queries; i += 1) {
  97. queryFunctions[i] = mongooseQuery;
  98. }
  99. res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
  100. async.parallel(queryFunctions, function(err, results) {
  101. res.end(JSON.stringify(results));
  102. });
  103. break;
  104. case '/mysql-orm':
  105. var values = url.parse(req.url, true);
  106. var queries = values.query.queries || 1;
  107. var queryFunctions = new Array(queries);
  108. for (var i = 0; i < queries; i += 1) {
  109. queryFunctions[i] = sequelizeQuery;
  110. }
  111. res.writeHead(200, {'Content-Type': 'application/json'});
  112. async.parallel(queryFunctions, function(err, results) {
  113. res.end(JSON.stringify(results));
  114. });
  115. break;
  116. case '/mysql':
  117. res.writeHead(200, {'Content-Type': 'application/json'});
  118. function libmysqlQuery(callback) {
  119. libmysql.query("SELECT * FROM world WHERE id = " + getRandomNumber(), function (err, res) {
  120. if (err) {
  121. throw err;
  122. }
  123. res.fetchAll(function(err, rows) {
  124. if (err) {
  125. throw err;
  126. }
  127. res.freeSync();
  128. callback(null, rows[0]);
  129. });
  130. });
  131. }
  132. var values = url.parse(req.url, true);
  133. var queries = values.query.queries || 1;
  134. var queryFunctions = new Array(queries);
  135. for (var i = 0; i < queries; i += 1) {
  136. queryFunctions[i] = libmysqlQuery;
  137. }
  138. async.parallel(queryFunctions, function(err, results) {
  139. if (err) {
  140. res.writeHead(500);
  141. return res.end('MYSQL CONNECTION ERROR.');
  142. }
  143. res.end(JSON.stringify(results));
  144. });
  145. break;
  146. case '/update':
  147. res.writeHead(200, {'Content-Type': 'application/json'});
  148. function libmysqlQuery(callback) {
  149. libmysql.query("SELECT * FROM world WHERE id = " + getRandomNumber(), function (err, res) {
  150. if (err) {
  151. throw err;
  152. }
  153. res.fetchAll(function(err, rows) {
  154. if (err) {
  155. throw err;
  156. }
  157. res.freeSync();
  158. rows[0].randomNumber = getRandomNumber();
  159. libmysql.query("UPDATE World SET randomNumber = " + rows[0].randomNumber + " WHERE id = " + rows[0]['id'], function (err, res) {
  160. if (err) {
  161. throw err;
  162. }
  163. callback(null, rows[0]);
  164. });
  165. });
  166. });
  167. }
  168. var values = url.parse(req.url, true);
  169. var queries = values.query.queries || 1;
  170. if(queries < 1) {
  171. queries = 1;
  172. } else if(queries > 500) {
  173. queries = 500;
  174. }
  175. var queryFunctions = new Array(queries);
  176. for (var i = 0; i < queries; i += 1) {
  177. queryFunctions[i] = libmysqlQuery;
  178. }
  179. async.parallel(queryFunctions, function(err, results) {
  180. if (err) {
  181. res.writeHead(500);
  182. return res.end('MYSQL CONNECTION ERROR.');
  183. }
  184. res.end(JSON.stringify(results));
  185. });
  186. break;
  187. case '/update-mongodb':
  188. // Database Test
  189. var values = url.parse(req.url, true);
  190. var queries = values.query.queries || 1;
  191. if (queries < 1) {
  192. queries = 1;
  193. } else if (queries > 500) {
  194. queries = 500;
  195. }
  196. var queryFunctions = new Array(queries);
  197. for (var i = 0; i < queries; i += 1) {
  198. queryFunctions[i] = mongodbDriverUpdateQuery;
  199. }
  200. res.writeHead(200, {'Content-Type': 'application/json; charset=UTF-8'});
  201. async.parallel(queryFunctions, function(err, results) {
  202. res.end(JSON.stringify(results));
  203. });
  204. break;
  205. default:
  206. // File not found handler
  207. res.writeHead(501, {'Content-Type': 'text/plain; charset=UTF-8'});
  208. res.end("NOT IMPLEMENTED");
  209. }
  210. }).listen(8080);