[jboss-cvs] JBossCache/tests/perf/org/jboss/cache/marshall ...

Galder Zamarreno galder.zamarreno at jboss.com
Fri Jan 12 12:09:34 EST 2007


  User: gzamarreno
  Date: 07/01/12 12:09:34

  Added:       tests/perf/org/jboss/cache/marshall 
                        VersionAwareMarshallerPerfTest.java
  Log:
  [JBCACHE-879] Performance test for version aware marshaller has been introduced. Use this as base to compare different types of marshalling methods and possibly add comparison failures.
  
  Revision  Changes    Path
  1.1      date: 2007/01/12 17:09:34;  author: gzamarreno;  state: Exp;JBossCache/tests/perf/org/jboss/cache/marshall/VersionAwareMarshallerPerfTest.java
  
  Index: VersionAwareMarshallerPerfTest.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   * 
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.marshall;
  
  import junit.framework.TestCase;
  import junit.awtui.TestRunner;
  import org.jboss.cache.RegionManager;
  import org.jboss.cache.misc.TestingUtil;
  
  import java.io.ByteArrayOutputStream;
  import java.io.ObjectOutputStream;
  import java.io.ByteArrayInputStream;
  import java.io.ObjectInputStream;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Random;
  
  /**
   * Performance test on different marshalling methods
   *
   * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
   */
  public class VersionAwareMarshallerPerfTest extends TestCase
  {
     private Random random = new Random();
  
     // Tune as necessary
     private static final int NUM_TIMES_PER_THREAD = 1;
     private static final int NUM_THREADS = 1;
     private static final String threadNamePrefix = "TesterThread-";
  
     private long[] write = new long[NUM_THREADS * NUM_TIMES_PER_THREAD];
     private long[] read = new long[NUM_THREADS * NUM_TIMES_PER_THREAD];
     private long[] size = new long[NUM_THREADS * NUM_TIMES_PER_THREAD];
  
     protected void setUp() throws Exception
     {
     }
  
     protected void tearDown() throws Exception
     {
        // go a gc() to clear up unused threads from each test
        System.gc();
        TestingUtil.sleepThread(1000);      
     }
  
     public void testStandardSerialization()
     {
        run('S', "Standard Java Serialization");
     }
  
     public void testVamJavaSerialization()
     {
        System.setProperty("serialization.jboss", "false");
        ObjectSerializationFactory.factory = new JavaObjectStreamFactory();
        ObjectSerializationFactory.useJBossSerialization = false;
        run('V', "Version Aware Marshaller - with Java Serialization");
     }
  
     public void testVamJBossSerialization() throws Exception
     {
        System.setProperty("serialization.jboss", "true");
        ObjectSerializationFactory.factory = (ObjectStreamFactory) Class.forName("org.jboss.cache.marshall.JBossObjectStreamFactory").newInstance();
        ObjectSerializationFactory.useJBossSerialization = true;
        run('V', "Version Aware Marshaller - with JBoss Serialization");
     }
  
     public void run(final char type, String name)
     {
        Thread threads[] = new Thread[NUM_THREADS];
  
        for (int i = 0; i < NUM_THREADS; i++)
        {
           threads[i] = new Thread()
           {
              public void run()
              {
                 int id = retrieveThreadId(getName());
  
                 try
                 {
                    Map node = new HashMap();
                    for (int j = 0; j < NUM_TIMES_PER_THREAD; j++)
                    {
                       long start;
                       long timeTaken;
                       byte[] buffer;
  
                       node.put("key" + j, "value" + j);
                       
                       switch(type)
                       {
                          case 'S' :
                             start = System.currentTimeMillis();
                             buffer = writeStandardSerialization(node);
                             timeTaken = System.currentTimeMillis() - start;
  
                             write[j+id] = timeTaken;
  
                             start = System.currentTimeMillis();
                             readStandardSerialization(buffer);
                             timeTaken = System.currentTimeMillis() - start;
  
                             read[j+id] = timeTaken;
                             break;
                          case 'V' :
                             VersionAwareMarshaller marshaller = new VersionAwareMarshaller(new RegionManager(), false, false, "2.0.0.GA");
  
                             start = System.currentTimeMillis();
                             buffer = writeVam(marshaller, node);
                             timeTaken = System.currentTimeMillis() - start;
  
                             write[j+id] = timeTaken;
  
                             start = System.currentTimeMillis();
                             readVam(marshaller, buffer);
                             timeTaken = System.currentTimeMillis() - start;
  
                             read[j+id] = timeTaken;
                             break;
                       }
                    }
                 }
                 catch (Exception e)
                 {
                    e.printStackTrace();
                 }
              }
           };
        }
  
        // start threads
        startThreads(threads);
  
        // wait for all the threads to join
        waitForThreads(threads);
  
        printResults(name, write, read);
     }
  
     private byte[] writeStandardSerialization(Object o) throws Exception
     {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
        return baos.toByteArray();
     }
  
     private void readStandardSerialization(byte[] buffer) throws Exception
     {
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        ObjectInputStream ois = new ObjectInputStream(bais);
        Object nodeRead = ois.readObject();
        ois.close();
     }
  
     private byte[] writeVam(VersionAwareMarshaller marshaller, Object o) throws Exception
     {
        byte[] marshalled = marshaller.objectToByteBuffer(o);
        return marshalled;
     }
  
     private void readVam(VersionAwareMarshaller marshaller, byte[] buffer) throws Exception
     {
        marshaller.objectFromByteBuffer(buffer);
     }
  
     private int retrieveThreadId(String name)
     {
        int separatorIndex = name.indexOf('-');
        return Integer.valueOf(name.substring(separatorIndex + 1, name.length())) * NUM_TIMES_PER_THREAD;
     }
  
     private void startThreads(Thread[] threads)
     {
        for (int i = 0; i < NUM_THREADS; i++)
        {
           // add a random delay here.
           randomSleep();
           threads[i].setName(threadNamePrefix + i);
           threads[i].start();
        }
     }
  
     private void waitForThreads(Thread[] threads)
     {
        for (int i = 0; i < NUM_THREADS; i++)
        {
           try
           {
              threads[i].join();
           }
           catch (InterruptedException e)
           {
           }
        }
     }   
  
     private void randomSleep()
     {
        TestingUtil.sleepThread(random.nextInt(1000));
     }
  
     private void printResults(String name, long[] writeStats, long[] readStats)
     {
        Map writeResults = getStats(writeStats);
        Map readResults = getStats(readStats);
  
        System.out.println("------------------------------------");
        System.out.println("  (all times in ms)");
        System.out.println();
        System.out.println("  Test name:         Writes for " + name);
        System.out.println("  NumThreads:        " + NUM_THREADS);
        System.out.println("  NumLoopsPerThread: " + NUM_TIMES_PER_THREAD);
        System.out.println();
        System.out.println("  Writes Total time:        " + writeResults.get("total"));
        System.out.println("  Writes Avg time:          " + ((Long) writeResults.get("total")).doubleValue() / writeStats.length);
        System.out.println("  Writes shortest time:     " + writeResults.get("shortest"));
        System.out.println("  Writes longest time:      " + writeResults.get("longest"));
        System.out.println();
        System.out.println("  Reads Total time:         " + readResults.get("total"));
        System.out.println("  Reads Avg time:           " + ((Long) readResults.get("total")).doubleValue() / readStats.length);
        System.out.println("  Reads shortest time:      " + readResults.get("shortest"));
        System.out.println("  Reads longest time:       " + readResults.get("longest"));
        System.out.println("------------------------------------");
     }
  
     public Map<String, Long> getStats(long[] stats)
     {
        long statTotal = 0;
        long largest = 0;
        long smallest = stats[0];
        for (long stat : stats)
        {
           statTotal = statTotal + stat;
           if (stat > largest) largest = stat;
           if (stat < smallest) smallest = stat;
        }
  
        Map<String, Long> results = new HashMap<String, Long>();
        results.put("total", statTotal);
        results.put("shortest", smallest);
        results.put("longest", largest);
  
        return results;
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list