Skip to content

Instantly share code, notes, and snippets.

@praveend
Created August 2, 2012 05:52
Show Gist options
  • Select an option

  • Save praveend/3234186 to your computer and use it in GitHub Desktop.

Select an option

Save praveend/3234186 to your computer and use it in GitHub Desktop.
Read behaviour mongo
import java.io.IOException;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class SelectTest {
public static void main(String[] args) throws MongoException, InterruptedException, IOException, Exception {
int numberOfQueries = 100;
int number_of_threads = 50;
String collectionName = "empCollection";
String database = "mongodb";
Mongo m = new Mongo();
SelectWhere[] tests = new SelectWhere[number_of_threads];
Thread[] threads = new Thread[number_of_threads];
for(int index = 1; index <= number_of_threads; index++ ) {
tests[index - 1] = new SelectWhere(database, collectionName, index, numberOfQueries, number_of_threads, m, true);
threads[index -1] = new Thread(tests[index - 1]);
//threads[index -1].start();
}
for(int index = 0 ; index < number_of_threads; index ++) {
threads[index].start();
}
for(int index = 0 ; index < number_of_threads; index ++) {
threads[index].join();
}
long end = System.currentTimeMillis();
}
}
class SelectWhere implements Runnable {
private Mongo _m = null;
private DB _db = null;
private int _threadCount = 0;
private String _collectionName = null;
private boolean _isMongo = false;
private int _numberOfQueries = 0;
private int _numberOfThreads = 0;
private boolean _diffRange = false;
private String _dbName = null;
public SelectWhere(String dbName,
String collectionName,int threadCount, int numberOfQueries, int numberOfThreads, Mongo m, boolean diffRange) throws UnknownHostException, MongoException {
_m = m;
this._dbName = dbName;
this._threadCount = threadCount;
this._collectionName = collectionName;
this._numberOfQueries = numberOfQueries;
this._numberOfThreads = numberOfThreads;
this._diffRange = diffRange;
}
public void run() {
System.out.println("Thread is: " + _threadCount);
DBCollection coll = null;
int lim = 100000;
int slab1 = _numberOfQueries/4;
int slab2 = _numberOfQueries/2;
int slab3 = slab2 + slab1;
int val = 0;
for(int i = 0; i < _numberOfQueries; i++) {
_db = _m.getDB(_dbName);
coll = _db.getCollection(_collectionName);
if(_diffRange) {
if(i > slab3) {
lim = 25000;
} else if(i > slab2) {
lim = 50000;
} else if(i > slab1) {
lim = 75000;
}
}
val = lim - (_threadCount + (_numberOfThreads * (i + 1))) - _numberOfThreads;
BasicDBObject where = new BasicDBObject();
where.put("batch1",new BasicDBObject("$lt", val));
where.put("batch2",new BasicDBObject("$gt", val - 20)); //Hard code to 20 so that we have a range that retrieves 20 rows
DBCursor cur = coll.find(where);
int count = 0;
while(cur.hasNext()) {
cur.next();
count ++;
}
if(count > 20) {
System.out.println("Count is greater than 20");
} else if(count == 0) {
System.out.println("Count is 0");
}
}
System.out.println("Thread end: " + _threadCount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment