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

Jason Thomas Greene jgreene at jboss.com
Thu Apr 12 13:25:13 EDT 2007


  User: jgreene 
  Date: 07/04/12 13:25:13

  Added:       tests/perf/org/jboss/cache/test/perf       Address.java
                        Course.java Person.java RandomString.java
                        Server.java Student.java
  Log:
  Port benchmark server from 1.4 branch
  
  Revision  Changes    Path
  1.1      date: 2007/04/12 17:25:13;  author: jgreene;  state: Exp;JBossCache/tests/perf/org/jboss/cache/test/perf/Address.java
  
  Index: Address.java
  ===================================================================
  package org.jboss.cache.test.perf;
  
  import org.jboss.cache.pojo.annotation.Replicable;
  
  /**
   * @author Ben Wang
   */
  @Replicable
  public class Address implements java.io.Serializable {
     protected String city;
     protected int zip;
     protected String street;
  
     public void setCity(String city)
     {
        this.city = city;
     }
  
     public String getCity()
     {
        return this.city;
     }
  
     public void setZip(int zip)
     {
        this.zip = zip;
     }
  
     public int getZip()
     {
        return zip;
     }
  
     public void setStreet(String street)
     {
        this.street = street;
     }
  
     public String getStreet()
     {
        return this.street;
     }
     
     public String getSimpleAddress()
     {
        StringBuffer buf = new StringBuffer(street);      
        buf.append(" "  + city);
        if (zip > 0)
           buf.append("  " + zip);
  
        return buf.toString();
     }
  
     public String toString()
     {
        StringBuffer buf = new StringBuffer();
        buf.append("{City = " +city).append(" ,zip = " +zip).append(" ,street = " +street + "}\n");
  
        return buf.toString();
     }
  }
  
  
  
  1.1      date: 2007/04/12 17:25:13;  author: jgreene;  state: Exp;JBossCache/tests/perf/org/jboss/cache/test/perf/Course.java
  
  Index: Course.java
  ===================================================================
  package org.jboss.cache.test.perf;
  
  import org.jboss.cache.pojo.annotation.Replicable;
  
  /**
   * @author Brian Stansberry
   */
  @Replicable
  public class Course  implements java.io.Serializable {
     protected String title;
     protected String instructor;
     protected String room;
  
     public void setTitle(String title)
     {
        this.title = title;
     }
  
     public String getTitle()
     {
        return this.title;
     }
  
     public void setRoom(String room)
     {
        this.room = room;
     }
  
     public String getRoom()
     {
        return this.room;
     }
     
     public void setInstructor(String instructor)
     {
        this.instructor = instructor;
     }
     
     public String getInstructor()
     {
        return instructor;
     }
  
     public String toString()
     {
        StringBuffer buf = new StringBuffer();
        buf.append("{Title = " +title).append(", Instructor = " + instructor).append(", Room = " +room + "}\n");
  
        return buf.toString();
     }
     
     public boolean equals(Object other)
     {
        if (this == other)
           return true;
           
        if (other instanceof Course) {
           String otherTitle = ((Course) other).getTitle();
           return (title == otherTitle || (title != null && title.equals(otherTitle)));
        } 
        
        return false;
     }
     
     public int hashCode()
     {
        return title == null ? 0 : title.hashCode();
     }
  }
  
  
  
  1.1      date: 2007/04/12 17:25:13;  author: jgreene;  state: Exp;JBossCache/tests/perf/org/jboss/cache/test/perf/Person.java
  
  Index: Person.java
  ===================================================================
  package org.jboss.cache.test.perf;
  
  import java.util.List;
  
  import org.jboss.cache.pojo.annotation.Replicable;
  
  /**
   * @author Ben Wang
   */
  @Replicable
  public class Person  implements java.io.Serializable {
     protected String name;
     protected Address address;
  
     public void setName(String name)
     {
        this.name = name;
     }
  
     public String getName()
     {
        return this.name;
     }
  
     public void setAddress(Address address)
     {
        this.address = address;
     }
  
     public Address getAddress()
     {
        return this.address;
     }
  
     public String toString()
     {
        StringBuffer buf = new StringBuffer();
        buf.append("{Name = " +name);
        if (address != null)
           buf.append(", Address = " + address.getSimpleAddress());
        buf.append("}\n");
  
        return buf.toString();
     }
  
  }
  
  
  
  1.1      date: 2007/04/12 17:25:13;  author: jgreene;  state: Exp;JBossCache/tests/perf/org/jboss/cache/test/perf/RandomString.java
  
  Index: RandomString.java
  ===================================================================
  package org.jboss.cache.test.perf;
  
  import java.util.*;
  
  import org.jboss.cache.pojo.annotation.Replicable;
  
  @Replicable
  public class RandomString {
  
         private static Random rn = new Random(12);
  
         public static int rand(int lo, int hi)
         {
                 int n = hi - lo + 1;
                 int i = rn.nextInt() % n;
                 if (i < 0)
                         i = -i;
                 return lo + i;
         }
  
         public static String randomstring(int lo, int hi)
         {
                 int n = rand(lo, hi);
                 byte b[] = new byte[n];
                 for (int i = 0; i < n; i++)
                         b[i] = (byte)rand('a', 'z');
                 return new String(b, 0);
         }
  
         public static String randomstring()
         {
                 return randomstring(5, 25);
         }
  }
  
  
  
  
  1.1      date: 2007/04/12 17:25:13;  author: jgreene;  state: Exp;JBossCache/tests/perf/org/jboss/cache/test/perf/Server.java
  
  Index: Server.java
  ===================================================================
  /*
   *
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.test.perf;
  
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Random;
  import java.util.StringTokenizer;
  import java.util.Vector;
  import java.util.concurrent.CountDownLatch;
  
  import javax.transaction.SystemException;
  import javax.transaction.Transaction;
  import javax.transaction.TransactionManager;
  
  import org.jboss.aop.Advised;
  import org.jboss.cache.CacheException;
  import org.jboss.cache.CacheImpl;
  import org.jboss.cache.CacheListener;
  import org.jboss.cache.CacheSPI;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.factories.XmlConfigurationParser;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jgroups.View;
  
  /**
   * A standalone dummy server that accepts data from other replicated cache.
   */
  public class Server {
     static PojoCache pojoCache;
     static CacheImpl<Object, Object> plainCache;
     Properties props_ = new Properties();
     static int threads_;  // how many threads to send the put.
     static int loops_; // how many loops
     static int sleepIntervalInMillis_;  // sleep interval between request
     static boolean randomSleep_ = false;
     static int objectListSize_ = 10; // what is the list size in the Courses list
     final static String ROOT = "/JSESSION/localhost/"; // emulates the http test
     static boolean isReady_ = false;
     static Throwable ex_;
     static long exceptionCounts_ = 0;
     static CountDownLatch countdown;
     static boolean receiver_ = false;
     static boolean transaction_ = false;
     static Random random = new Random(10);
     static int updatePojoInterval_ = 0;
     static boolean runPojoCache_ = false;
     static long startTime_ = 0;
     static long endTime_ = 0;
     static int operationType_ = 1;
  
     void initCache() throws Exception {
      
        String file = (String)props_.get("cache_config");
        if(file==null)
        {
           throw new RuntimeException("Cache config xml is not specified.");
        }
        XmlConfigurationParser parser = new XmlConfigurationParser();
        Configuration config = parser.parseFile(file);
        config.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
        pojoCache = PojoCacheFactory.createCache(config, false);
        plainCache = (CacheImpl)pojoCache.getCache();
     }
  
     void destroyCache() throws Exception {
        pojoCache.stop();
        pojoCache = null;
     }
  
     PojoCache getCache() {
        return pojoCache;
     }
  
     void parseConfig(String fileName) throws Exception {
        String line;
        StringTokenizer st;
        String key, val;
        BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
        while ((line = fileReader.readLine()) != null) {
           if (line.startsWith("#"))
              continue;
           line = line.trim();
           if (line.length() == 0)
              continue;
           st = new StringTokenizer(line, "=", false);
           key = st.nextToken().toLowerCase();
           val = st.nextToken();
           props_.put(key, val);
           System.out.println("Read in config key, value: "+ key + " "+ val);
        }
        fileReader.close();
     }
  
     void startLoadTest() throws InterruptedException, CacheException {
        String str = (String)props_.get("threads");
        if(str == null)
           throw new RuntimeException("Can't find threads property");
  
        threads_ = Integer.parseInt(str);
  
        str = (String)props_.get("loops");
        if(str == null)
           throw new RuntimeException("Can't find loops property");
  
        loops_ = Integer.parseInt(str);
  
        str = (String)props_.get("sleep_interval_millis");
        if(str == null)
           throw new RuntimeException("Can't find sleepIntervalInMillis property");
  
        sleepIntervalInMillis_ = Integer.parseInt(str);
  
        str = (String)props_.get("random_sleep_interval");
        if(str == null)
           throw new RuntimeException("Can't find random_sleep_interval property");
  
        randomSleep_ = new Boolean(str).booleanValue();
  
        str = (String)props_.get("object_list_size");
        if(str == null)
           throw new RuntimeException("Can't find objectListSize property");
  
        objectListSize_ = Integer.parseInt(str);
  
        str = (String)props_.get("transaction");
        if(str == null)
           throw new RuntimeException("Can't find transaction property");
  
        transaction_ = new Boolean(str).booleanValue();
  
        /*
        str = (String)props_.get("operation_type");
        if(str == null)
           throw new RuntimeException("Can't find operation_type property");
  
        operationType_ = Integer.parseInt(str); */
  
        str = (String)props_.get("update_pojo_interval");
        if(str == null)
           throw new RuntimeException("Can't find update_pojo_interval property");
  
        updatePojoInterval_ = Integer.parseInt(str);
  
        countdown = new CountDownLatch(threads_);
  
        // Warm up the cache first to avoid any simultaneous write contention.
        if (plainCache.getCoordinator().equals(plainCache.getLocalAddress()))
        {
           System.out.println("I am the coordinator: " + plainCache.getLocalAddress());
           plainCache.put(ROOT + plainCache.getLocalAddress().toString(), "test", "test");
        }
  
        sleep_(300);
        if(runPojoCache_)
        {
           // This is to pre-load the POJO class definition
           Object pojo = Loader.constructObject();
           pojoCache.attach(ROOT + plainCache.getLocalAddress().toString() +"/test", pojo);
        }
  
        startTime_ = System.currentTimeMillis();
        for(int i=0; i < threads_ ; i++)
        {
           Loader loader = new Loader(pojoCache, i, plainCache.getLocalAddress().toString());
           loader.start();
        }
     }
  
     static int getSleepInterval()
     {
        if(sleepIntervalInMillis_ ==0) return sleepIntervalInMillis_;
  
        if(randomSleep_)
        {
           return random.nextInt(sleepIntervalInMillis_);
        } else
        {
           return sleepIntervalInMillis_;
        }
     }
  
     List getMembers(String addrListStr)
     {
        StringTokenizer tok = new StringTokenizer(addrListStr, ",");
        List list = new ArrayList();
        while(tok.hasMoreTokens())
        {
           list.add(tok.nextToken());
        }
  
        return list;
     }
  
     static void sleep_(long msec)
     {
        try {
           Thread.sleep(msec);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }
     }
     void printUsage()
     {
        System.out.println("Options: -config to specify config file (like bench.txt)\n" +
                           "         -receiver receiver only mode, e.g., no send\n");
     }
  
     public void printInfo()
     {
        System.out.println("\n************************************");
        System.out.println("\nThis is a simple performance test for JBossCache.\n" +
        "The load pattern is like those of http session repl, that is,\neach put " +
        "is under a separate sub-tree.\n" +"As a result, there should not be write contention.\n");
     }
  
     public static void main(String[] args) throws Exception 
     {
        Server server = new Server();
        if(args.length ==0)
        {
           server.printUsage();
           return;
        }
  
        for (int i = 0; i < args.length; i++) {
           if ("-config".equals(args[i])) {
              System.out.println("Configuration file is: " + args[i+1]);
              server.parseConfig(args[++i]);
           } else if("-receiver".equals(args[i])) {
              receiver_ = true;
           } else {
              System.err.println("Unknown argument:" + args[i]);
              server.printUsage();
              return;
           }
        }
        
        String str = (String)server.props_.get("run_pojocache");
        if(str == null)
           throw new RuntimeException("Can't find run_PojoCache property");
  
        server.runPojoCache_ = new Boolean(str).booleanValue();
        if (server.runPojoCache_ && ! Advised.class.isAssignableFrom(Address.class))
        {
           System.err.println("Aop not present, either install the JVM agent or run aopc!");
           System.exit(1);
        }
  
        server.printInfo();
  
        String addrListStr = (String)server.props_.get("members");
        List addrList = server.getMembers(addrListStr);
  
        server.initCache();
        ViewListener listener = new ViewListener(addrList);
        pojoCache.getCache().addCacheListener(listener); // register for view change
        pojoCache.start();
        System.out.println("Cache started .. ");
  
        System.out.println("Waiting for the other nodes to start up...");
        while (!isReady_) {
           sleep_(100);
        }
  
        // Let's remove the tree cache listener since we are done. This is needed so we don't cloud
        // the perf number.
        pojoCache.getCache().removeCacheListener(listener);
  
        sleep_(2000);
  
        if(!receiver_)
        {
           server.startLoadTest();
           System.out.println("Waiting for the test to finish...");
           // blocked untill all done
           countdown.await();
           endTime_ = System.currentTimeMillis();
           System.out.println("\nThroughtput for this node with:\n" + "threads = " + threads_
                   +"\nloops = " +loops_
           + "\nsleep interval = " +sleepIntervalInMillis_ + "\nobject list size = "
           + objectListSize_ + "\ntranasaction? " +transaction_ +"\n"
           + "is: " + (loops_ *threads_ *1000)/(endTime_ -startTime_) + " requests/sec\n");
        } else
        {
           System.out.println("Receiver mode only. Won't send request...");
        }
  
        if(ex_ != null)
        {
           System.err.println("Exception counts: " +exceptionCounts_);
           throw new RuntimeException("Exception occurred during the run: " +ex_);
        }
  
        System.out.println("Test is finished. hit ctrl-c to kill the process ...");
        while(true)
        {
           sleep_(1000);
        }
  
  //      server.destroyCache();
     }
  
     public static class ViewListener implements CacheListener {
        List addrList_; // List of address
  
        public ViewListener(List addrList) {
           addrList_ = addrList;
        }
  
        public void viewChange(View new_view)  // might be MergeView after merging
        {
           Vector vector = new_view.getMembers();
           if(vector.size() > addrList_.size())
           {
              // We have more members than is specified!
              throw new RuntimeException("You have more members in the cluster group than specified in the config."
              + " size of members now: " +vector.size());
           }
           // Need to match individually but let's just assum the size matters then.
           if(vector.size() == addrList_.size())
           {
              // good to go
              isReady_ = true;
           }
        }
  
        public void cacheStarted(CacheSPI cache)
        {
        }
  
        public void cacheStopped(CacheSPI cache)
        {
        }
  
        public void nodeActivated(Fqn fqn, boolean pre)
        {
        }
  
        public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
        {
        }
  
        public void nodeEvicted(Fqn fqn, boolean pre, boolean isLocal)
        {
        }
  
        public void nodeLoaded(Fqn fqn, boolean pre, Map<Object, Object> data)
        {
        }
  
        public void nodeModified(Fqn fqn, boolean pre, boolean isLocal, ModificationType modType, Map<Object, Object> data)
        {
        }
  
        public void nodeMoved(Fqn from, Fqn to, boolean pre, boolean isLocal)
        {
        }
  
        public void nodePassivated(Fqn fqn, boolean pre)
        {
        }
  
        public void nodeRemoved(Fqn fqn, boolean pre, boolean isLocal, Map<Object, Object> data)
        {
        }
  
        public void nodeVisited(Fqn fqn, boolean pre)
        {
        }
     }
  
     public static class Loader extends Thread
     {
        int threadId;
        String localAddress;
        PojoCache cache_;
  
        TransactionManager tm_ = DummyTransactionManager.getInstance();
  
        public Loader(PojoCache cache, int i, String localAddress)
        {
           cache_ = cache;
           threadId = i;
           this.localAddress = localAddress;
  //         System.out.println("Local addres is: " +localAddress.toString());
        }
  
        public void run() {
           Object obj = null;
           Object obj1 = constructObject();
           Object obj2 = constructObject();
           String fqn = ROOT + localAddress + "/" + threadId;
  
           long start_time = System.currentTimeMillis();
           boolean isEven = true;
           for(int i=0; i < loops_; i++)
           {
              if(getSleepInterval() !=0)
                 sleep_(getSleepInterval());
              Transaction tx = null;
              try {
                 if(transaction_)
                 {
                    tm_.begin();
                    tx = tm_.getTransaction();
                 }
  
                 boolean update = true;
                 if(runPojoCache_ && updatePojoInterval_!= 1)
                 {
                    if((i%updatePojoInterval_) != 0 )
                    {
                       update = false;
                    }
                 }
  
                 /**
                  * Need this for PojoCache. Otherwise, it'd be cheating becuase another putObject of thee
                  * same pojo is fast.
                  */
                 if(update)
                 {
                    if(isEven)
                    {
                       obj = obj1;
                       isEven = false;
                    }
                    else
                    {
                       obj = obj2;
                       isEven = true;
                    }
                 }
  
                 doWork(fqn, localAddress, obj, update);
                 if(transaction_)
                 {
                    tx.commit();
                 }
              } catch (Exception e) {
                 exceptionCounts_++;
                 ex_ = e;
                 e.printStackTrace();
                 if(transaction_)
                 {
                    try {
                       tx.rollback();
                    } catch (SystemException e1) {
                       e1.printStackTrace();
                       ex_ = e1;
                    }
                 }
              } finally
              {
              }
  
  //            System.out.println("Processing at loop: " +i);
              if( (i%50) ==0 && threadId == 0)
              {
                 System.out.println("Processing at loop: " +i);
              }
           }
           long end_time = System.currentTimeMillis();
           long time = end_time - start_time;
           if(threadId == 0)
             System.out.println("Total time spent (ms) on thread id 0: " +time + " for "+ loops_ +" loops");
  
           countdown.countDown();
        }
  
        void doWork(String fqn, Object key, Object pojo, boolean update) throws CacheException {
           // Note that we still need to reconstruct object every loop to avoid cheating false identity problem.
           if(!runPojoCache_)
           {
              doPlainCacheWork(fqn, key, pojo);
           } else
           {
              if(update)
              {
  //               System.out.println("Running pojo cache update");
                 doPojoCacheWork(fqn, pojo);
              } else
              {
  //               System.out.println("Running field update");
                 doPojoCacheFieldWork(fqn, pojo);
              }
           }
        }
  
        void doPlainCacheWork(String fqn, Object key, Object pojo) throws CacheException {
           Fqn<String> realFqn = Fqn.fromString(fqn);
           if(operationType_ == 1)
           {
              Object obj1 = constructObject();
              plainCache.put(realFqn, key, obj1);
              Object obj = plainCache.get(realFqn, key);
              if(!((Student)obj).getName().equals("Joe"))
              {
                 throw new RuntimeException("Value returned not Joe");
              }
           } else
           {
              plainCache.remove(realFqn, key);
              plainCache.put(realFqn, key, pojo);
              Object obj = plainCache.get(realFqn, key);
              if(!((Student)obj).getName().equals("Joe"))
              {
                 throw new RuntimeException("Value returned not Joe");
              }
           }
        }
  
        void doPojoCacheWork(String fqn, Object pojo) throws CacheException {
           if(operationType_ == 1)
           {
              cache_.attach(fqn, pojo);
              Object obj = cache_.find(fqn);
              if(!((Student)obj).getName().equals("Joe"))
              {
                 throw new RuntimeException("Value returned not Joe");
              }
           } else
           {
              cache_.detach(fqn);
              cache_.attach(fqn, pojo);
              Object obj = cache_.find(fqn);
              if(!((Student)obj).getName().equals("Joe"))
              {
                 throw new RuntimeException("Value returned not Joe");
              }
           }
        }
  
        void doPojoCacheFieldWork(String fqn, Object pojo) throws CacheException {
           Object obj = cache_.find(fqn);
           List list = (List)((Student)obj).getCourses();
           ((Course)list.get(0)).setInstructor("Ben Wang");
  //         ((Student)obj).setSchool("Pingtung");
        }
  
        static Object constructObject()
        {
           Student joe = new Student();
           joe.setName("Joe");
  
           Address add = new Address();
           add.setZip(94086);
           add.setCity("Sunnyvale)");
           add.setStreet("Albertson");
           joe.setAddress(add);
  
           String str;
           for(int i=0; i < objectListSize_; i++)
           {
              Course course = new Course();
              str = RandomString.randomstring(10,20);
              course.setInstructor(str);
              str = RandomString.randomstring(10,20);
              course.setTitle(str);
              str = RandomString.randomstring(10,20);
              course.setRoom(str);
              joe.addCourse(course);
           }
  
           return joe;
        }
  
     }
  }
  
  
  
  1.1      date: 2007/04/12 17:25:13;  author: jgreene;  state: Exp;JBossCache/tests/perf/org/jboss/cache/test/perf/Student.java
  
  Index: Student.java
  ===================================================================
  package org.jboss.cache.test.perf;
  
  import java.util.*;
  
  import org.jboss.cache.pojo.annotation.Replicable;
  
  
  /**
   * @author Ben Wang
   */
  @Replicable
  public class Student extends Person
  {
     protected String school;
  //   protected Set   courses = new LinkedHashSet();
     protected List   courses = new ArrayList();
  
     public void setSchool(String school)
     {
        this.school = school;
     }
  
     public String getSchool()
     {
        return this.school;
     }
  
     public void addCourse(Course course)
     {
        courses.add(course);
     }
  
     public void removeCourse(Course course)
     {
        courses.remove(course);
     }
  
     public Collection getCourses()
     {
        return Collections.unmodifiableList(courses);
     }
  
     public String toString()
     {
        StringBuffer buf = new StringBuffer();
        buf.append("{Name = " +name).append(", School = " +school);
        if (address != null)
           buf.append(", Address = " + address.getSimpleAddress());
        buf.append("}\n");
        buf.append("Courses:\n");
        for (Iterator iter = getCourses().iterator(); iter.hasNext(); )
           buf.append(iter.next());
  
        return buf.toString();
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list