简体   繁体   English

如何确定 MongoDB 还是 Node.js 是瓶颈?

[英]How do I determine if MongoDB or Node.js is the bottle neck?

I'm very new to systems design in general, so let me try and explain my question to the best of my ability!总的来说,我对系统设计很陌生,所以让我尝试尽我所能解释我的问题!

I have two EC2 t2.micro instances up and running: one is housing my MongoDB, which is storing 10,000,000 primary records, and the other has my express server on it.我有两个 EC2 t2.micro 实例启动并运行:一个是容纳我的 MongoDB,它存储了 10,000,000 条主记录,另一个是我的快速服务器。

The structure of my MongoDB documents are as follows:我的MongoDB文档结构如下:

{
  _id: 1,
  images: ["url_1.jpg", "url_2.jpg", "url_3.jpg"],
}

This is what my mongo connection looks like:这是我的 mongo 连接的样子:

const { MongoClient } = require('mongodb');
const { username, password, ip } = require('./config.js');

const client = new MongoClient(`mongodb://${username}:${password}@${ip}`,
  { useUnifiedTopology: true, poolSize: 10 });

client.connect();

const Images = client.db('imagecarousel').collection('images');

module.exports = Images;

I am using loader.io to run a 1000PRS stress test to my servers GET API endpoint.我正在使用 loader.io 对我的服务器 GET API 端点运行 1000PRS 压力测试。 The first test uses a .findOne() query, the second a .find().limit(1) query, like so:第一个测试使用.findOne()查询,第二个测试使用.find().limit(1)查询,如下所示:

const query = { _id: +req.params.id };
Images.findOne(query).then((data) => res.status(200).send(data))
    .catch((err) => {
      console.log(err);
      res.status(500).send(errorMessage);
    });

//////////////////////////////////////////

const query = { _id: +req.params.id };
Images.find(query).limit(1).toArray().then((data) => res.status(200).send(data[0]))
    .catch((err) => {
      console.log(err);
      res.status(500).send(errorMessage);
    });

When I looked at the results on New Relic, I was a little perplexed by what I saw: New Relic Results当我在 New Relic 上查看结果时,我对所看到的内容感到有些困惑: New Relic 结果

After some research, I figured this has something to do with .findOne() returning a document, and .find() returning a cursor?经过一些研究,我认为这与.findOne()返回文档和.find()返回游标有关?

So my question is: How do I determine if the bottle neck is node.js or MongoDB, and do the queries I use determine that for me (in this specific case)?所以我的问题是:我如何确定瓶颈是 node.js 还是 MongoDB,我使用的查询是否为我确定(在这种特定情况下)?

I would suggest that you start with the mongodb console and explore your queries in detail.我建议您从 mongodb 控制台开始并详细探索您的查询。 This way you will isolate the mongodb behavior from the driver behavior.通过这种方式,您可以将 mongodb 行为与驱动程序行为隔离开来。

A good way to analyse your queries is:分析查询的一个好方法是:

If you aim at pitch-perfect database performance tuning, you need to understand every detail of the execution of your queries.如果您的目标是完美的数据库性能调优,您需要了解查询执行的每个细节。 It will take some time to get grip of it, but it's totally worth it!掌握它需要一些时间,但这是完全值得的!

Another detail of interest is the real-world performance monitoring and profiling in production , which reveals the true picture of the bottlenecks in your application, as opposed to the more "sterile" non-production stress-testing.另一个有趣的细节是真实世界的性能监控和分析生产,从而揭示你的应用程序中的瓶颈的真实情况,而不是更多的“无菌”非生产压力测试。 Here is a good profiler, which allows you to insert custom profiling points in your app and to easily turn profiling on and off without restarting the app:这是一个很好的分析器,它允许您在应用程序中插入自定义分析点,并在不重新启动应用程序的情况下轻松打开和关闭分析:

https://www.npmjs.com/package/mc-profiler https://www.npmjs.com/package/mc-profiler

A good practice would be to first let the application run in production as a beta, inspect profiling data and optimize the slow code.一个好的做法是首先让应用程序作为测试版在生产中运行,检查分析数据并优化慢代码。 Otherwise you could waste swathes of time going after some optimizations, which have little to no impact to the general app performance.否则,您可能会浪费大量时间进行一些优化,这对一般应用程序性能几乎没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM