插入节点-mongodb-native的性能


Insert performance of node-mongodb-native

我正在用MongoDB测试Node.js的性能。 我知道这些中的每一个都很好,彼此独立,但我正在尝试一些测试来感受它们。 我遇到了这个问题,但我在确定来源时遇到了问题。

问题所在

我正在尝试在单个 Node.js 程序中插入 1,000,000 条记录。 它绝对会爬行。 我们说的是 20 分钟的执行时间。 无论是我的 Mac 还是 CentOS,都会发生这种情况,尽管两者之间的行为略有不同。 它最终会完成。

效果类似于交换,但事实并非如此(内存永远不会超过 2 GB)。 只有3个连接打开到MongoDB,大多数时候没有插入任何数据。 它似乎正在做很多上下文切换,并且 Node.js CPU 内核已达到最大值。

效果与此线程中提到的效果类似。

我使用 PHP 尝试相同的操作,它在 2-3 分钟内完成。没有戏剧性。

为什么?

可能的原因

我目前认为这要么是 Node.js 套接字问题,要么是幕后 libev 发生的事情,要么是其他一些 node-mongodb 原生问题。 我可能完全错了,所以我在这里寻找一些指导。

至于其他 Node.js MongoDB 适配器,我尝试过蒙古语,它似乎将文档排队以便批量插入它们,最终内存不足。 所以就出来了。 (旁注:我也不知道为什么会这样做,因为它甚至没有接近我的 16 GB 盒限制——但我没有费心进一步调查。

可能应该提到,我确实测试了一个有 4 个工作线程的主/工作线程集群(在四核机器上),它在 2-3 分钟内完成。

《守则》

这是我的 Node.js CoffeeScript 程序:

mongodb = require "mongodb"
microtime = require "microtime"
crypto = require "crypto"
times = 1000000
server = new mongodb.Server "127.0.0.1", 27017
db = mongodb.Db "test", server
db.open (error, client) ->
  throw error if error?
  collection = mongodb.Collection client, "foo"
  for i in [0...times]
    console.log "Inserting #{i}..." if i % 100000 == 0
    hash = crypto.createHash "sha1"
    hash.update "" + microtime.now() + (Math.random() * 255 | 0)
    key = hash.digest "hex"
    doc =
      key: key,
      foo1: 1000,
      foo2: 1000,
      foo3: 1000,
      bar1: 2000,
      bar2: 2000,
      bar3: 2000,
      baz1: 3000,
      baz2: 3000,
      baz3: 3000
    collection.insert doc, safe: true, (error, response) ->
      console.log error.message if error

这是大致等效的PHP程序:

<?php
$mongo = new Mongo();
$collection = $mongo->test->foo;
$times = 1000000;
for ($i = 0; $i < $times; $i++) {
    if ($i % 100000 == 0) {
        print "Inserting $i...'n";
    }
    $doc = array(
        "key" => sha1(microtime(true) + rand(0, 255)),
        "foo1" => 1000,
        "foo2" => 1000,
        "foo3" => 1000,
        "bar1" => 2000,
        "bar2" => 2000,
        "bar3" => 2000,
        "baz1" => 3000,
        "baz2" => 3000,
        "baz3" => 3000
    );
    try {
        $collection->insert($doc, array("safe" => true));
    } catch (MongoCursorException $e) {
        print $e->getMessage() . "'n";
    }
}

听起来您在 V8 中遇到了默认的堆限制。我写了一篇关于消除此限制的博客文章。

垃圾收集器可能会发疯并咀嚼CPU,因为它会不断执行,直到您低于1.4GB的限制。

如果在 db.open 回调函数的末尾显式返回一个值,会发生什么情况?你生成的javascript代码正在将你所有的collection.insert返回推送到一个大的"_results"数组上,我想这会变得越来越慢。

db.open(function(error, client) {
  var collection, doc, hash, i, key, _i, _results;
  if (error != null) {
    throw error;
  }
  collection = mongodb.Collection(client, "foo");
  _results = [];
  for (i = _i = 0; 0 <= times ? _i < times : _i > times; i = 0 <= times ? ++_i : --_i) {
    ...
    _results.push(collection.insert(doc, {
      safe: true
    }, function(error, response) {
      if (error) {
        return console.log(error.message);
      }
    }));
  }
  return _results;
});

尝试在咖啡脚本的末尾添加以下内容:

    collection.insert doc, safe: true, (error, response) ->
      console.log error.message if error
  return

*更新: *所以,我实际上尝试运行您的程序,并注意到更多问题:

最大的问题是你试图以同步方式生成一百万次插入,这真的会杀死你的 RAM,并最终停止插入(至少对我来说是这样)。我在 800MB RAM 左右杀死了它。

您需要更改调用 collection.insert() 的方式,以便它异步工作。

我像这样重写了它,为了清楚起见,分解了几个函数:

mongodb = require "mongodb"
microtime = require "microtime"
crypto = require "crypto"
gen  = () ->
  hash = crypto.createHash "sha1"
  hash.update "" + microtime.now() + (Math.random() * 255 | 0)
  key = hash.digest "hex"
  key: key,
  foo1: 1000,
  foo2: 1000,
  foo3: 1000,
  bar1: 2000,
  bar2: 2000,
  bar3: 2000,
  baz1: 3000,
  baz2: 3000,
  baz3: 3000
times = 1000000
i = times
insertDocs = (collection) ->
  collection.insert gen(), {safe:true}, () ->
    console.log "Inserting #{times-i}..." if i % 100000 == 0
    if --i > 0
      insertDocs(collection)
    else
      process.exit 0
  return
server = new mongodb.Server "127.0.0.1", 27017
db = mongodb.Db "test", server
db.open (error, db) ->
  throw error if error?
  db.collection "foo", (err, collection) ->
    insertDocs(collection)
    return
  return

在~3分钟内完成:

wfreeman$ time coffee mongotest.coffee
Inserting 0...
Inserting 100000...
Inserting 200000...
Inserting 300000...
Inserting 400000...
Inserting 500000...
Inserting 600000...
Inserting 700000...
Inserting 800000...
Inserting 900000...
real    3m31.991s
user    1m55.211s
sys 0m23.420s

此外,它还具有使用 <100MB RAM、节点上 70% CPU 和 mongod 上 40% CPU 的附带好处(在 2 核盒子上,所以看起来它没有最大化 CPU)。