package com.github.fakemongo;

import ch.qos.logback.classic.Level;
import com.github.fakemongo.impl.ExpressionParser;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.FongoDBCollection;
import org.slf4j.LoggerFactory;

public class PerfTest {
  public static void main(String[] args) {
    // Desactivate logback
    ch.qos.logback.classic.Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(FongoDBCollection.class);
    log.setLevel(Level.ERROR);
    log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ExpressionParser.class);
    log.setLevel(Level.ERROR);

    System.out.println("Warming jvm");
    // Microbenchmark warm
    for (int i = 0; i < 10000; i++) {
      doit(100);
      doitFindN(100);
      doitFindUniqueIndex(100);
      doitFindNWithIndex(100);
    }
    System.out.println("Warming jvm done.");
    long startTime = System.currentTimeMillis();
    doit(10000);
    System.out.println("Took " + (System.currentTimeMillis() - startTime) + " ms");

    startTime = System.currentTimeMillis();
    doitFindUniqueIndex(10000);
    System.out.println("Took " + (System.currentTimeMillis() - startTime) + " ms with one useless index.");

    startTime = System.currentTimeMillis();
    doitFindN(10000);
    System.out.println("Took " + (System.currentTimeMillis() - startTime) + " ms with no index.");

    startTime = System.currentTimeMillis();
    doitFindNWithIndex(10000);
    System.out.println("Took " + (System.currentTimeMillis() - startTime) + " ms with index.");
  }

  public static void doit(int size) {
    Fongo fongo = new Fongo("fongo");
    for (int i = 0; i < 1; i++) {
      DB db = fongo.getDB("db");
      DBCollection collection = db.getCollection("coll");
      for (int k = 0; k < size; k++) {
        collection.insert(new BasicDBObject("_id", k).append("n", new BasicDBObject("a", 1)));
        collection.findOne(new BasicDBObject("_id", k));
      }
      db.dropDatabase();
    }
  }

  public static void doitFindN(int size) {
    Fongo fongo = new Fongo("fongo");
    for (int i = 0; i < 1; i++) {
      DB db = fongo.getDB("db");
      DBCollection collection = db.getCollection("coll");
      for (int k = 0; k < size; k++) {
        collection.insert(new BasicDBObject("_id", k).append("n", new BasicDBObject("a", k)));
        collection.findOne(new BasicDBObject("a", k));
      }
      db.dropDatabase();
    }
  }

  public static void doitFindUniqueIndex(int size) {
    Fongo fongo = new Fongo("fongo");
    for (int i = 0; i < 1; i++) {
      DB db = fongo.getDB("db");
      DBCollection collection = db.getCollection("coll");
      collection.createIndex(new BasicDBObject("n", 1));
      for (int k = 0; k < size; k++) {
        collection.insert(new BasicDBObject("_id", k).append("n", new BasicDBObject("a", 1)));
        collection.findOne(new BasicDBObject("_id", k));
      }
      db.dropDatabase();
    }
  }

  public static void doitFindNWithIndex(int size) {
    Fongo fongo = new Fongo("fongo");
    for (int i = 0; i < 1; i++) {
      DB db = fongo.getDB("db");
      DBCollection collection = db.getCollection("coll");
      collection.createIndex(new BasicDBObject("n", 1));
      for (int k = 0; k < size; k++) {
        collection.insert(new BasicDBObject("_id", k).append("n", k % 100));
        collection.findOne(new BasicDBObject("n.a", k % 100));
      }
      db.dropDatabase();
    }
  }
}

@@ -273,16 +273,208 @@
 ctory;%0A%0A
+/**%0A * Before :%0A * %3Cpre%3E%0A * Took 204 ms%0A * Took 205 ms with one useless index.%0A * Took 6815 ms with no index.%0A * Took 12016 ms with index.%0A * Took 15782 ms to remove with index.%0A * %3C/pre%3E%0A */%0A
 public c
@@ -1037,24 +1037,56 @@
 Index(100);%0A
+      doitRemoveWithIndex(100);%0A
     %7D%0A    Sy
@@ -1796,16 +1796,199 @@
 ndex.%22);
+%0A%0A    startTime = System.currentTimeMillis();%0A    doitRemoveWithIndex(10000);%0A    System.out.println(%22Took %22 + (System.currentTimeMillis() - startTime) + %22 ms to remove with index.%22);
 %0A  %7D%0A%0A  
@@ -3752,24 +3752,24 @@
  k %25 100));%0A
-
         coll
@@ -3861,11 +3861,553 @@
    %7D%0A  %7D
+%0A%0A  public static void doitRemoveWithIndex(int size) %7B%0A    Fongo fongo = new Fongo(%22fongo%22);%0A    for (int i = 0; i %3C 1; i++) %7B%0A      DB db = fongo.getDB(%22db%22);%0A      DBCollection collection = db.getCollection(%22coll%22);%0A      collection.createIndex(new BasicDBObject(%22n%22, 1));%0A      for (int k = 0; k %3C size; k++) %7B%0A        collection.insert(new BasicDBObject(%22_id%22, k).append(%22n%22, k %25 100));%0A      %7D%0A      for (int k = 0; k %3C size; k++) %7B%0A        collection.remove(new BasicDBObject(%22n.a%22, k %25 100));%0A      %7D%0A      db.dropDatabase();%0A    %7D%0A  %7D
 %0A%7D%0A

add test for remove