Skip to content

Instantly share code, notes, and snippets.

@danharvey
Created October 30, 2010 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danharvey/655263 to your computer and use it in GitHub Desktop.
Save danharvey/655263 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import com.google.common.base.CharMatcher;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;
public class ReadBenchmark {
private static CharMatcher stripQuotes = CharMatcher.is('"');
public static void main(String[] args) throws IOException {
// Connect to mongoDB
MongoOptions mo = new MongoOptions();
mo.autoConnectRetry = true;
Mongo mongo = new Mongo(Arrays.asList(new ServerAddress("data-node-1"), new ServerAddress("data-node-2"), new ServerAddress("data-node-3")), mo);
DB db = mongo.getDB("datamining");
db.setReadOnly(true);
DBCollection docs = db.getCollection("canonical_documents");
for (int i=0; i<10000; i++) {
// Create new stats collector
DescriptiveStatistics stats = new DescriptiveStatistics();
// Run though gets
BufferedReader r = new BufferedReader(new FileReader(args[0]));
for (String id = r.readLine(); id != null; id = r.readLine()) {
try {
// Strip quotes
id = stripQuotes.trimFrom(id);
// Do stats on response time
DBObject key = new BasicDBObject();
key.put("id", id);
// Benchmark this
long time = System.nanoTime();
DBCursor results = docs.find(key);
// Get the results
int n = 0;
for (DBObject o: results) {
System.out.println(o);
n++;
}
results.close();
long delta = System.nanoTime() - time;
stats.addValue(delta);
} catch (RuntimeException e) {
System.out.println(e);
continue;
}
}
// Dump out stats
System.out.println(i + ":" + stats.getMean()/1000 + ", " + stats.getPercentile(99.00)/1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment