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