【问题标题】:Simulating Mongo Write Lock to measure performance模拟 Mongo 写锁来衡量性能
【发布时间】:2012-08-22 23:45:47
【问题描述】:

我正在尝试使用 mongo 1.8.0 模拟 mongo 写锁,但无法看到正确的预期结果。

我在同一服务器上的两个不同数据库中创建了两个 mongo 集合。我创建了一个 DBObject 数组并将它们插入到两个集合中。使用两个线程同时触发批量插入。我还跟踪调用 DBCollection.insert(DBObject arr, WriteConcern.SAFE) 之前和之后的时间。

尽管使用了不同的对象大小和数组大小,但我总是发现插入两个 DB 所花费的时间有点接近。我希望一个线程首先写入阻塞另一个线程,导致两个线程之间花费的时间显着不同。我在这里有什么遗漏吗?

class BenchTest {

public static void main() {

    Mongo m = new Mongo(host,port);
    DBCollection coll1 = m.getDB("db0").getColl("coll0");
    DBCollection coll2 = m.getDB("db1").getColl("coll0");

    Thread t1 = new WriteThread();
    t1.setCollection(coll1);
    Thread t2 = new WriteThread();
    t2.setCollection(coll2);

    t1.run();
    t2.run();

}

}


   class WriteThread extends Thread {

    DBCollection coll; 

    public void setCollection (DBCollection coll) {
        this.coll = coll;
    }

    long startTime = System.currentTimeMillis();
    coll.insert( (DBObject1, DBObject2, …, DBObjectn), WriteConcern.SAFE);
    long endTime = System.currentTimeMillis();

    System.out.println ("Time taken = "+(endTime-startTime));

}

【问题讨论】:

    标签: java multithreading performance mongodb mongo-java


    【解决方案1】:

    为什么不直接用fsync & lock来模拟“写锁”呢?

    很难模拟“写锁”,因为它基本上不会挂起,它只存在很短的时间。 here 概述的版本(1.8、2.0 和 2.2)有很多变化(以免重复我自己)。

    这是一个非常棒的blog post,关于其他人正在对“写锁”进行一些类似的测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2012-11-08
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多