package com.test; import com.mongodb.*; import java.util.*; import java.util.concurrent.atomic.*; import java.util.Random; public class SlaveTest { static Mongo mongo; static DB db; static DBCollection coll; public static ServerAddress createAddress(String host_and_port) throws Exception { String info[] = host_and_port.split(":"); return new ServerAddress(info[0], Integer.parseInt(info[1])); } public static void main( String args[] ) throws Exception { MongoOptions mo = new MongoOptions(); mo.autoConnectRetry = true; List connectionList = new ArrayList(); String addresses[] = args[0].split(","); for (int i=0; i < addresses.length; i++) { connectionList.add(createAddress(addresses[i])); } Mongo mongo = new Mongo( connectionList , mo ); db = mongo.getDB( args[1] ); coll = db.getCollection( args[2] ); // Create some data System.out.println("Inserting data :"); for (int j=0; j < 1000; j++) { BasicDBObject o = new BasicDBObject(); o.put("author", "jim"); o.put("clocktime", new Date()); o.put("ts", j); ArrayList tags = new ArrayList(); tags.add("yes"); tags.add("no"); o.put("tags", tags); coll.insert(o); } int type = Integer.parseInt(args[3]); if ( type == 1 ) { db.slaveOk(); } if ( type == 4 ) { coll.slaveOk(); } int doCount = Integer.parseInt(args[4]); DBCursor cur = null; int count = 0; int row_count = 0; System.out.println("Querying data :"); long benchStart = System.currentTimeMillis(); while ( System.currentTimeMillis() - benchStart < 60000) { BasicDBObject query = new BasicDBObject(); query.put("author", "jim"); if ( type == 2) { cur = coll.find(query).limit(20).addOption(Bytes.QUERYOPTION_SLAVEOK); } else { cur = coll.find(query).limit(20); } if ( doCount == 1) { row_count += cur.itcount(); } else if ( doCount == 2) { row_count += cur.count(); } else if ( doCount == 3) { row_count += cur.size(); } count++; } System.out.println("Queries : " + count); System.out.println("Rows found : " + row_count); BasicDBObject r = new BasicDBObject(); coll.remove(r); System.out.println("Finished :"); } }