Created
August 2, 2012 05:56
-
-
Save praveend/3234199 to your computer and use it in GitHub Desktop.
Insert test for Mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class InsertTestMain { | |
| public static void main(String[] args) throws Exception { | |
| int number_of_threads = 10; | |
| String database = "mongodb"; | |
| int numOfDocsToInsert = 10000; | |
| String collectionName = "empCollection"; | |
| Mongo m = new Mongo(); | |
| DB db = null; | |
| db = m.getDB(database); | |
| DBCollection coll = db.getCollection(collectionName); | |
| //Ensure empty table before any operations | |
| coll.drop(); | |
| BasicDBObject empObj = new BasicDBObject(); | |
| empObj.put("serial", 1); | |
| empObj.put("firstName", "praveen"); | |
| empObj.put("lastName", "D"); | |
| empObj.put("middleName", "foo"); | |
| empObj.put("age", 19); | |
| empObj.put("salary", 40000); | |
| empObj.put("isNuts", true); | |
| empObj.put("batch1", 0); | |
| empObj.put("batch2", 0); | |
| BasicDBObject deptObj = new BasicDBObject(); | |
| deptObj.put("name", "production"); | |
| deptObj.put("region", "Asia"); | |
| empObj.put("dept", deptObj); | |
| coll.insert(empObj); | |
| /**/ | |
| BasicDBObject obj = new BasicDBObject(); | |
| BasicDBList list = new BasicDBList(); | |
| list.add(1); | |
| list.add("$integer"); | |
| obj.put("age", 1); | |
| BasicDBObject obj1 = new BasicDBObject(); | |
| obj1.put("salary", 1); | |
| BasicDBObject obj2 = new BasicDBObject(); | |
| obj2.put("batch1", 1); | |
| BasicDBObject obj3 = new BasicDBObject(); | |
| obj3.put("batch2", 1); | |
| coll.createIndex(obj); | |
| coll.createIndex(obj1); | |
| coll.createIndex(obj2); | |
| coll.createIndex(obj3); | |
| InsertTest1[] tests = new InsertTest1[number_of_threads]; | |
| Thread[] threads = new Thread[number_of_threads]; | |
| for(int index = 1; index <= number_of_threads; index++ ) { | |
| tests[index - 1] = new InsertTest1(database, collectionName, index, number_of_threads, numOfDocsToInsert, m); | |
| threads[index -1] = new Thread(tests[index - 1]); | |
| //threads[index -1].start(); | |
| } | |
| long start = System.currentTimeMillis(); | |
| 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(); | |
| System.out.println("Start: " + start + "| End: " + end); | |
| System.out.println("Time taken: "+ (end - start)); | |
| System.out.println("Time taken: "+ (System.currentTimeMillis() - start)); | |
| } | |
| } | |
| class InsertTest1 implements Runnable { | |
| private Mongo _m = null; | |
| private DB _db = null; | |
| private int _threadCount = 0; | |
| private int _numThreads = 0; | |
| private String _collectionName = null; | |
| private int _numOfDocsToInsert = 0; | |
| private String _dbName = null; | |
| public InsertTest1(String dbName, | |
| String collectionName,int threadCount, int numThreads, int numOfDocsToInsert, Mongo m) throws UnknownHostException, MongoException { | |
| _m = m; | |
| this._dbName = dbName; | |
| this._threadCount = threadCount; | |
| this._collectionName = collectionName; | |
| this._numThreads = numThreads; | |
| this._numOfDocsToInsert = numOfDocsToInsert; | |
| } | |
| public void run() { | |
| int arraySize = 100; | |
| System.out.println("Thread is: " + _threadCount + " - " + Thread.currentThread().getName()); | |
| BasicDBObject dbObj[] = null; | |
| int index = 0; | |
| int arrayIndex = 0; | |
| _db = null; | |
| DBCollection coll = null; | |
| int writeConcern = 1; | |
| WriteConcern wConcern = WriteConcern.NONE; | |
| switch(writeConcern) { | |
| case 0: | |
| wConcern = WriteConcern.NONE; | |
| break; | |
| case 1: | |
| wConcern = WriteConcern.SAFE; | |
| break; | |
| case 2: | |
| wConcern = WriteConcern.JOURNAL_SAFE; | |
| break; | |
| case 3: | |
| wConcern = WriteConcern.FSYNC_SAFE; | |
| break; | |
| } | |
| _db = _m.getDB(_dbName); | |
| coll = _db.getCollection(_collectionName); | |
| for (int loopIndex = 0; loopIndex < _numOfDocsToInsert; loopIndex++,arrayIndex++) { | |
| if(dbObj == null) { | |
| dbObj = new BasicDBObject[arraySize]; | |
| } | |
| index = ((loopIndex + 1) % 100) * _threadCount; | |
| dbObj[arrayIndex] = new BasicDBObject(); | |
| dbObj[arrayIndex].put("serial", index); | |
| dbObj[arrayIndex].put("firstName", "Praveen" + index); | |
| dbObj[arrayIndex].put("lastName", "D" + index); | |
| dbObj[arrayIndex].put("middleName", "foo" + index); | |
| dbObj[arrayIndex].put("age",loopIndex + (index % _numOfDocsToInsert)); | |
| dbObj[arrayIndex].put("salary", index * _numOfDocsToInsert | |
| + ((index * _numOfDocsToInsert) % _numOfDocsToInsert)); | |
| dbObj[arrayIndex].put("isNuts", true); | |
| dbObj[arrayIndex].put("batch1", (_threadCount + (_numThreads * (loopIndex +1))) - _numThreads); | |
| dbObj[arrayIndex].put("batch2", (_threadCount + (_numThreads * (loopIndex +1))) - _numThreads); | |
| BasicDBObject deptObj = new BasicDBObject(); | |
| deptObj.put("name", "production"); | |
| deptObj.put("region", "Asia"); | |
| dbObj[arrayIndex].put("dept", deptObj); | |
| if((loopIndex + 1) % arraySize == 0) { | |
| coll.insert(dbObj,wConcern); | |
| dbObj = null; | |
| arrayIndex = -1; | |
| } | |
| } | |
| if(dbObj != null) { | |
| coll.insert(dbObj,wConcern); | |
| } | |
| System.out.println("Thread end: " + _threadCount); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment