I created a small Java application (with Spring Data) to test MongoDB performance with geospatial queries .
public class MongoThread {
public static void main(String[] args) throws Exception {
if(args.length != 1) {
System.out.println("Usage: MongoThread <nb_thread>");
System.exit(0);
}
int nbThread = Integer.parseInt(args[0]);
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfigStandalone.class);
MongoOperations db = (MongoOperations) ctx.getBean("mongoTemplate");
for (int x=0; x<nbThread; x++) {
double lat = Math.random() * 60;
double lng = Math.random() * 60;
double radius = 3000 + 50 * Math.random();
MyThread t = new MyThread("Thread #" + x, db, lat, lng, radius);
t.start();
}
//create1MEntries(db);
}
private static void create1MEntries(MongoOperations db) {
if (!db.getCollectionNames().contains("Item")) {
db.createCollection("Item");
}
db.indexOps(Item.class).ensureIndex(new GeospatialIndex("loc"));
for(int i=0; i<1000000; i++) {
Item item = new Item();
item.setName("item" + i);
item.setLoc(new double[]{Math.random() * 180, Math.random() * 180});
db.save(item, "Item");
}
}
}
class MyThread extends Thread {
String name;
MongoOperations db;
double lat, lng, radius;
public MonThread (String name, MongoOperations db, double lat, double lng, double radius) {
this.name = name;
this.db = db;
this.lat = lat;
this.lng = lng;
this.radius = radius;
}
public void run() {
long t1 = Calendar.getInstance().getTimeInMillis();
List<Item> items = db.find(new Query(Criteria.where("loc")
.near(new Point(lat,lng)).maxDistance(radius/111.12)).limit(100), Item.class, "Item");
System.out.println(name + " - " + items.size() + " results found around " + radius + " of (" + lat + "," + lng + ")");
long t2 = Calendar.getInstance().getTimeInMillis();
System.out.println(name + " - " + (t2-t1) + "ms");
}
}
public class Item {
@Id
private String id;
private String name;
private double[] loc;
}
After running the tests multiple times to load data in memory, here are the results I get:
-
On my dev computer (Win7 64 bits, i7 860 @2.8 Ghz, RAM 8GB 1066Mhz): For 100 threads, I get all responses in about 500 ms (between 450 ms and 550 ms)
-
On my server (hosted by OVH: Debian 6.0 64 bits, i3 2130 2×2(HT) 3.4GHz, RAM 16GB 1333Mhz): For 100 threads, I get all responses in about 1700 ms (between 1600 ms and 1900 ms)
I am not a hardware neither Linux specialist but I was expecting this server to do better than my Windows computer (or at least as good).
I read on several forums that MongoDB was really faster on Linux and that important hardware features were (in that order): RAM, CPU (but Mongo does not use multiple cores) and hard drives.
I increased the max open files (using ulimit -n 99999) because I read that it can slow down MongoDB but it had no effect on the results.
Do you have an idea where the bottleneck come from?
I don’t think this is a linux vs windows issue. I mean that i3 processor is pretty low end compared to the beast that i7 is on your windows machine.
If you really want to compare performance between the two operating systems, I recommend to get your setup running on identical hardware..