Simulating Mongo Write Lock to measure performance

I am trying to simulate a mongo write lock using mongo 1.8.0, but unable to see correct expected results.

I create two mongo collections in two different dbs on the same server. I create an array of DBObjects and insert them in both collections. The batch inserts are triggered simultaneously using two threads. I also keep track of the time before and after the DBCollection.insert(DBObject arr, WriteConcern.SAFE) is called.

Despite of using varying object sizes and array sizes, I always find the time taken to insert into both DBs to be somewhat close. I would expect one thread to write first blocking the other, causing the time taken to be significantly different between both threads. Is there something that I am missing here ?

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();
    Thread t2 = new WriteThread();



   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));



Why don't you just use fsync & lock to simulate the "write lock"?

It will be difficult to simulate the "write lock" as it essentially doesn't hang around, it's only there for a tiny amount of time. There have been many changes across versions (1.8, 2.0 and 2.2) outlined here (so as not to repeat myself).

Here's a really nice blog post on someone-else doing some similar'ish testing on the "write lock".

Need Your Help

HTML5 canvas translate back after scale and rotation

javascript html5 canvas

I'm trying to do a few thing with canvas. First I have a user upload an image, if the image is larger than I want I need to scale it down. That part is working just fine. Recently we ran into an is...