dwj*_*ton 6 latency mongoose mongodb node.js
(为了后代,相关代码粘贴在底部)。
我发现,如果我使用 Mongo 的本地实例运行这些实验(在我的情况下,使用 docker)
docker run -d -p 27017:27017 -v ~/data:/data/db mongo
然后我得到了非常好的性能,与博客文章中概述的结果类似:
finished populating the database with 10000 users
default_query: 277.986ms
query_with_index: 262.886ms
query_with_select: 157.327ms
query_with_select_index: 136.965ms
lean_query: 58.678ms
lean_with_index: 65.777ms
lean_with_select: 23.039ms
lean_select_index: 21.902ms
[nodemon] clean exit - waiting 
但是,当我切换使用 Mongo 的云实例时,在我的情况下是一个 Atlas 沙箱实例,具有以下配置:
CLUSTER TIER
M0 Sandbox (General)
REGION
GCP / Iowa (us-central1)
TYPE
Replica Set - 3 nodes
LINKED STITCH APP
None Linked
(请注意,我位于澳大利亚墨尔本)。
然后我的表现要差得多。
adding 10000 users to the database
finished populating the database with 10000 users
default_query: 8279.730ms
query_with_index: 8791.286ms
query_with_select: 5234.338ms
query_with_select_index: 4933.209ms
lean_query: 13489.728ms
lean_with_index: 10854.134ms
lean_with_select: 4906.428ms
lean_select_index: 4710.345ms
我明白我的计算机和 mongo 实例之间显然会有一些往返开销,但我希望最多增加 200 毫秒。
似乎必须多次添加往返时间,或者我不知道的其他完全不同的东西 - 有人可以解释一下是什么导致了这个问题吗?
一个好的答案可能涉及制定解释计划,并根据网络延迟进行解释。
针对不同 Atlas 实例的测试 - 对于那些暗示问题是我使用的是 Atlas 的 Sandbox 实例的人 - 以下是 M20 和 M30 实例的结果:
BACKUPS
Active
CLUSTER TIER
M20 (General)
REGION
GCP / Iowa (us-central1)
TYPE
Replica Set - 3 nodes
LINKED STITCH APP
None Linked
BI CONNECTOR
Disabled
adding 10000 users to the database
finished populating the database with 10000 users
default_query: 9015.309ms
query_with_index: 8779.388ms
query_with_select: 4568.794ms
query_with_select_index: 4696.811ms
lean_query: 7694.718ms
lean_with_index: 7886.828ms
lean_with_select: 3654.518ms
lean_select_index: 5014.867ms
BACKUPS
Active
CLUSTER TIER
M30 (General)
REGION
GCP / Iowa (us-central1)
TYPE
Replica Set - 3 nodes
LINKED STITCH APP
None Linked
BI CONNECTOR
Disabled
adding 10000 users to the database
finished populating the database with 10000 users
default_query: 8268.799ms
query_with_index: 8933.502ms
query_with_select: 4740.234ms
query_with_select_index: 5457.168ms
lean_query: 9296.202ms
lean_with_index: 9111.568ms
lean_with_select: 4385.125ms
lean_select_index: 4812.982ms
这些确实没有显示出任何显着差异(请注意,任何差异可能只是网络噪音)。
测试配置 Mongo 客户端和 mongo 数据库实例
我创建了一个 docker 容器并在 Google 的 Cloud Run 上运行它,在同一地区(US Central1),结果是:
2019-12-30 11:46:06.814 AEDTfinished populating the database with 10000 users
2019-12-30 11:46:07.885 AEDTdefault_query: 1071.233ms
2019-12-30 11:46:08.917 AEDTquery_with_index: 1031.952ms
2019-12-30 11:46:09.375 AEDTquery_with_select: 457.659ms
2019-12-30 11:46:09.657 AEDTquery_with_select_index: 281.678ms
2019-12-30 11:46:10.281 AEDTlean_query: 623.417ms
2019-12-30 11:46:10.961 AEDTlean_with_index: 680.622ms
2019-12-30 11:46:11.056 AEDTlean_with_select: 94.722ms
2019-12-30 11:46:11.148 AEDTlean_select_index: 91.984ms
因此,虽然这并没有像在我自己的机器上运行那样快速给出结果 - 它确实表明将客户端和数据库放在一起提供了非常大的性能改进。
那么问题又来了——为什么改进了~7000ms?
测试代码:
(async () => {
  try {
    await mongoose.connect('mongodb://localhost:27017/perftest', {
      useNewUrlParser: true,
      useCreateIndex: true
    })
    await init()
    // const query = { age: { $gt: 22 } }
    const query = { favoriteFruit: 'potato' }
    console.time('default_query')
    await User.find(query)
    console.timeEnd('default_query')
    console.time('query_with_index')
    await UserWithIndex.find(query)
    console.timeEnd('query_with_index')
    console.time('query_with_select')
    await User.find(query)
      .select({ name: 1, _id: 1, age: 1, email: 1 })
    console.timeEnd('query_with_select')
    console.time('query_with_select_index')
    await UserWithIndex.find(query)
      .select({ name: 1, _id: 1, age: 1, email: 1 })
    console.timeEnd('query_with_select_index')
    console.time('lean_query')
    await User.find(query).lean()
    console.timeEnd('lean_query')
    console.time('lean_with_index')
    await UserWithIndex.find(query).lean()
    console.timeEnd('lean_with_index')
    console.time('lean_with_select')
    await User.find(query)
      .select({ name: 1, _id: 1, age: 1, email: 1 })
      .lean()
    console.timeEnd('lean_with_select')
    console.time('lean_select_index')
    await UserWithIndex.find(query)
      .select({ name: 1, _id: 1, age: 1, email: 1 })
      .lean()
    console.timeEnd('lean_select_index')
    process.exit(0)
  } catch (err) {
    console.error(err)
  }
})()
通常,请求在网络上传播需要一点时间。这取决于连接速度、延迟和到服务器的距离等等许多因素。但本地计算机上的服务器不会面临上述问题,因为它是针对云环境的。
但由于您确信网络传播导致的最大延迟约为 200 毫秒。
可能还有其他几个可能的原因需要考虑
通常,沙盒计划用于测试,分配给它们的资源有限。他们不使用 SSD 驱动器来存储数据,而是使用廉价的存储解决方案。他们认为沙盒计划通常只是为了探索功能。大多数时候,这些实例都在共享虚拟机上运行。
确保您的计算机上没有运行其他消耗较高数据速率的服务,例如:( torrent 应用程序)
| 归档时间: | 
 | 
| 查看次数: | 2378 次 | 
| 最近记录: |