mongodb-app.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Module dependencies.
  3. */
  4. const cluster = require('cluster');
  5. const numCPUs = require('os').cpus().length;
  6. const express = require('express');
  7. const mongoose = require('mongoose');
  8. const conn = mongoose.connect('mongodb://tfb-database/hello_world');
  9. // Middleware
  10. const bodyParser = require('body-parser');
  11. /**
  12. * Note! The benchmarks say we should use "id" as a property name.
  13. * However, Mongo provides a default index on "_id", so to be equivalent to the other tests, we use
  14. * the same, default index provided by the database.
  15. *
  16. */
  17. const WorldSchema = new mongoose.Schema({
  18. _id: Number,
  19. randomNumber: Number
  20. }, {
  21. collection: 'world'
  22. });
  23. const MWorld = mongoose.model('world', WorldSchema);
  24. const FortuneSchema = new mongoose.Schema({
  25. _id: Number,
  26. message: String
  27. }, {
  28. collection: 'fortune'
  29. });
  30. const MFortune = mongoose.model('fortune', FortuneSchema);
  31. if (cluster.isPrimary) {
  32. // Fork workers.
  33. for (let i = 0; i < numCPUs; i++) {
  34. cluster.fork();
  35. }
  36. cluster.on('exit', (worker, code, signal) => console.log('worker ' + worker.pid + ' died'));
  37. } else {
  38. const app = module.exports = express();
  39. const randomTfbNumber = () => Math.floor(Math.random() * 10000) + 1;
  40. const toClientWorld = (world) => {
  41. if (world) {
  42. world.id = world._id;
  43. delete world._id;
  44. }
  45. return world;
  46. };
  47. // Configuration
  48. app.use(bodyParser.urlencoded({extended: true}));
  49. // Set headers for all routes
  50. app.use((req, res, next) => {
  51. res.setHeader("Server", "Express");
  52. return next();
  53. });
  54. app.set('view engine', 'pug');
  55. app.set('views', __dirname + '/views');
  56. async function getRandomWorld() {
  57. return toClientWorld(await MWorld.findOne({_id: randomTfbNumber()}).lean().exec());
  58. }
  59. // Routes
  60. app.get('/mongooseq', async (req, res) => {
  61. const queryCount = Math.min(parseInt(req.query.queries) || 1, 500);
  62. const promises = [];
  63. for (let i = 1; i <= queryCount; i++) {
  64. promises.push(getRandomWorld());
  65. }
  66. res.send(await Promise.all(promises));
  67. });
  68. app.get('/mongoose', async (req, res) => {
  69. const result = await MWorld.findOne({_id: randomTfbNumber()}).lean().exec();
  70. res.send(toClientWorld(result));
  71. });
  72. app.get('/mongoose-fortune', async (req, res) => {
  73. const fortunes = (await MFortune.find({}).lean().exec()).map(toClientWorld);
  74. const newFortune = {id: 0, message: "Additional fortune added at request time."};
  75. fortunes.push(newFortune);
  76. fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1);
  77. res.render('fortunes/index', {fortunes});
  78. });
  79. async function getUpdateRandomWorld() {
  80. // it would be nice to use findOneAndUpdate here, but for some reason the test fails with it.
  81. const world = await MWorld.findOne({_id: randomTfbNumber()}).lean().exec();
  82. world.randomNumber = randomTfbNumber();
  83. await MWorld.updateOne({
  84. _id: world._id
  85. }, {
  86. $set: {
  87. randomNumber: world.randomNumber
  88. }
  89. }).exec();
  90. return toClientWorld(world);
  91. }
  92. app.get('/mongoose-update', async (req, res) => {
  93. const queryCount = Math.min(parseInt(req.query.queries, 10) || 1, 500);
  94. const promises = [];
  95. for (let i = 1; i <= queryCount; i++) {
  96. promises.push(getUpdateRandomWorld());
  97. }
  98. res.send(await Promise.all(promises));
  99. });
  100. app.listen(8080);
  101. }