/*
 *
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.cache.aop;

import junit.framework.TestCase;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.aop.PojoCache;
import org.jboss.cache.aop.test.Student;
import org.jboss.cache.aop.test.Address;

import java.util.Hashtable;
import java.util.Random;

/**
 * Driver test for performance...
 * <ul>
 * <li>automatic state fail over</li>
 * <li>fine-grained replication</li>
 * <li>preservation of object graph relationship</li>
 * </ul>
 */
public class StudentMetrics extends TestCase {

   Hashtable hash = new Hashtable();
   // cache1 and cache2 are in the same clustering group.
   private PojoCache cache1_;
   private PojoCache cache2_;
   int TIMES = 1000;

   protected void setUp() throws Exception {
      cache1_ = createCache("TestCluster");
      cache2_ = createCache("TestCluster");
      init();
   }

   protected void tearDown() throws Exception {
      cache1_.remove("/");
      cache1_.stop();
      cache2_.stop();
   }

   private PojoCache createCache(String name) throws Exception {
      PojoCache tree = new PojoCache();
      PropertyConfigurator config = new PropertyConfigurator();   // configure the cache through injection
       // read in the replSync xml. Here we use synchronous mode replication.
      config.configure(tree, "META-INF/replSync-service.xml");
//      config.configure(tree, "META-INF/local-service.xml");
      tree.setClusterName(name); // We can set a different cluster group.
      tree.start(); // kick start the cache
      return tree;
   }

   /**
    * Populate the propagation tree.
    *
    * @throws Exception
    */
   protected void init() throws Exception {
      Random r = new Random();

      for (int i = 0; i < TIMES; i++) {
        Student unk = new Student();
        unk.setName(""+ i );

        Address address = new Address();
        address.setStreet(r.nextInt() + "Oak Drive");
        address.setCity("Pleasantville, CA" + r.nextInt());
        address.setZip(r.nextInt(99999));
        unk.setAddress(address);

        hash.put(unk.getName(),unk);
      }
      System.out.println("Finished with init...");
   }

   public void testPropagation() throws Exception {

      //iterate through all without the cache...
      long ms = System.currentTimeMillis();
      System.out.print("running through hash by itself...");
      for(int i = 0; i < TIMES; i++)  {
        Student x = (Student)hash.get("" + i);
        if(i % 100 == 0) {
            System.out.println(x);
        }
      }
      System.out.println(TIMES + " hashmap ops took "+(System.currentTimeMillis() - ms));

      System.out.println("adding to cache 1...");
      //iterate through all while adding to the cache...
      ms = System.currentTimeMillis();
      for(int i = 0; i < TIMES; i++)  {
        cache1_.putObject("test/"+i, hash.get(""+i));
      }
      System.out.println(TIMES +" putObject ops took "+(System.currentTimeMillis() - ms));

      // Retrieve All from Server #2
      System.out.println("retreiving from cache 2...");
      ms = System.currentTimeMillis();
/*
      for(int i = 0; i < TIMES; i++)  {
        Student y = (Student)cache2_.getObject("test/"+i);
        if(i % 100 == 0) {
            System.out.println(y);
        }
      }
      System.out.println(TIMES +" getObject ops took "+(System.currentTimeMillis() - ms));
*/
   }

   public static void main(String[] args) throws Exception {
      StudentMetrics smTest = new StudentMetrics();
      smTest.setUp();
      smTest.testPropagation();
      smTest.tearDown();
   }
}
