db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/otm PerformanceTest.java
Date Sat, 13 Sep 2003 12:04:37 GMT
olegnitz    2003/09/13 05:04:37

  Modified:    src/test/org/apache/ojb/broker PerformanceTest2.java
  Added:       src/test/org/apache/ojb/otm PerformanceTest.java
  Log:
  OTM performance tests added
  
  Revision  Changes    Path
  1.20      +166 -5    db-ojb/src/test/org/apache/ojb/broker/PerformanceTest2.java
  
  Index: PerformanceTest2.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/PerformanceTest2.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- PerformanceTest2.java	30 Jul 2003 21:17:34 -0000	1.19
  +++ PerformanceTest2.java	13 Sep 2003 12:04:37 -0000	1.20
  @@ -9,6 +9,9 @@
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.util.ObjectModificationDefaultImpl;
   import org.apache.ojb.odmg.OJB;
  +import org.apache.ojb.otm.OTMConnection;
  +import org.apache.ojb.otm.TestKit;
  +import org.apache.ojb.otm.lock.LockType;
   import org.odmg.DList;
   import org.odmg.Database;
   import org.odmg.Implementation;
  @@ -25,6 +28,7 @@
    */
   public class PerformanceTest2 //extends TestCase
   {
  +    private TestKit _kit;
       private static int iterationsPerThread = 1000;
       private static int concurrentThreads = 10;
       // used for client generated ids
  @@ -50,6 +54,7 @@
       public PerformanceTest2()
       {
           this.threadCount = concurrentThreads;
  +        _kit = TestKit.getTestInstance();
       }
   
       /**
  @@ -139,7 +144,7 @@
           times = new long[4];
           threadCount = concurrentThreads;
   
  -        if (whichTest == 1 || whichTest == 3)
  +        if ((whichTest & 1) == 1)
           {
               PerformanceArticle art = createArticle(1000);
               PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  @@ -153,7 +158,7 @@
               broker.close();
           }
   
  -        if (whichTest == 2 || whichTest == 3)
  +        if ((whichTest & 2) == 2)
           {
               PerformanceArticle art2 = createArticle(1001);
               Implementation ojb = OJB.getInstance();
  @@ -169,6 +174,21 @@
               tx.commit();
               db.close();
           }
  +
  +        if ((whichTest & 4) == 4)
  +        {
  +            PerformanceArticle art3 = createArticle(1002);
  +            OTMConnection conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +            org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn);
  +            tx.begin();
  +            conn.makePersistent(art3);
  +            tx.commit();
  +
  +            tx = _kit.getTransaction(conn);
  +            tx.begin();
  +            conn.deletePersistent(art3);
  +            tx.commit();
  +        }
       }
   
       /**
  @@ -228,7 +248,7 @@
               int objectCount = 0;
               int objectCountAfter = 0;
   
  -            if (whichTest == 1 || whichTest == 3)
  +            if ((whichTest & 1) == 1)
               {
                   test.init();
                   objectCount = test.getArticleCount();
  @@ -241,7 +261,7 @@
               }
   
   
  -            if (whichTest == 2 || whichTest == 3)
  +            if ((whichTest & 2) == 2)
               {
                   test.init();
                   objectCount = test.getArticleCount();
  @@ -252,6 +272,18 @@
                   System.err.println("Test-Info:   Objects in DB after ODMG test: " + objectCountAfter);
                   System.err.println("Test-Info:   Stress test was successful? - " + (objectCount
== objectCountAfter) + " -");
               }
  +            
  +            if ((whichTest & 4) == 4)
  +            {
  +                test.init();
  +                objectCount = test.getArticleCount();
  +                test.testMultithreaded_OTM_api();
  +
  +                System.err.println("Test-Info:   Objects in DB before OTM test: " + objectCount);
  +                objectCountAfter = test.getArticleCount();
  +                System.err.println("Test-Info:   Objects in DB after OTM test: " + objectCountAfter);
  +                System.err.println("Test-Info:   Stress test was successful? - " + (objectCount
== objectCountAfter) + " -");
  +            }
           }
           catch (Exception e)
           {
  @@ -300,6 +332,26 @@
           System.out.println("++ End of performance test ODMG api ++" + sep);
       }
   
  +    public void testMultithreaded_OTM_api()
  +    {
  +        String sep = System.getProperty("line.separator");
  +        System.out.println("++ Start thread generation for OTM api test ++");
  +        System.out.println("Begin with performance test, " + concurrentThreads +
  +                " concurrent threads, handle " + iterationsPerThread + " articles per thread");
  +        PerfomanceTestClientOTM[] clientsOTM = new PerfomanceTestClientOTM[concurrentThreads];
  +        for (int i = 0; i < concurrentThreads; i++)
  +        {
  +            PerfomanceTestClientOTM obj = new PerfomanceTestClientOTM(this);
  +            clientsOTM[i] = obj;
  +        }
  +        System.out.println("");
  +        times[0] = System.currentTimeMillis();
  +        runTestClients(clientsOTM);
  +        times[0] = (long) (System.currentTimeMillis() - times[0]);
  +        System.out.println(buildTestSummary("OTM"));
  +        System.out.println("++ End of performance test OTM api ++" + sep);
  +    }
  +
       private String buildTestSummary(String key)
       {
           String sep = System.getProperty("line.separator");
  @@ -448,6 +500,115 @@
                   PerformanceArticle a = (PerformanceArticle) iter.next();
               }
               db.close();
  +            long stop = System.currentTimeMillis();
  +            times[2] = times[2] + (stop - start);
  +        }
  +    }
  +
  +    /**
  +     * OTM-api test class
  +     */
  +    class PerfomanceTestClientOTM extends TestClient
  +    {
  +        private static final String PRE_NAME = "B_";
  +        private PerformanceArticle[] arr;
  +        private String threadName;
  +        private PerformanceTest2 test;
  +
  +        public PerfomanceTestClientOTM(PerformanceTest2 test)
  +        {
  +            this.test = test;
  +        }
  +
  +        public void run()
  +        {
  +            threadName = Thread.currentThread().toString();
  +            arr = new PerformanceArticle[iterationsPerThread];
  +            for (int i = 0; i < iterationsPerThread; i++)
  +            {
  +                PerformanceArticle a = createArticle(i, threadName);
  +                arr[i] = a;
  +            }
  +
  +            try
  +            {
  +                OTMConnection conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
  +                insertNewArticles(conn);
  +                readArticlesByCursor(conn);
  +                deleteArticles(conn);
  +                conn.close();
  +            }
  +            catch (Throwable e)
  +            {
  +                System.out.println("Error in client " + this);
  +                e.printStackTrace();
  +                test.interruptThreads();
  +            }
  +        }
  +
  +        /**
  +         * factory method that createa an PerformanceArticle
  +         * @return the created PerformanceArticle object
  +         */
  +        private PerformanceArticle createArticle(int id, String name)
  +        {
  +            PerformanceArticle a = new PerformanceArticle();
  +            if (clientKeyGeneration) a.setArticleId(getId());
  +            a.setArticleName(PRE_NAME + name);
  +            a.setMinimumStock(100);
  +            a.setOrderedUnits(17);
  +            a.setPrice(0.45);
  +            a.setProductGroupId(1);
  +            a.setStock(234);
  +            a.setSupplierId(4);
  +            a.setUnit("bottle");
  +            return a;
  +        }
  +
  +        protected void deleteArticles(OTMConnection conn) throws Exception
  +        {
  +            long start = System.currentTimeMillis();
  +            org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn);
  +            tx.begin();
  +            for (int i = 0; i < arr.length; i++)
  +            {
  +                conn.deletePersistent(arr[i]);
  +            }
  +            tx.commit();
  +            long stop = System.currentTimeMillis();
  +            times[3] = times[3] + (stop - start);
  +        }
  +
  +        protected void insertNewArticles(OTMConnection conn) throws Exception
  +        {
  +            long start = System.currentTimeMillis();
  +            org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn);
  +            tx.begin();
  +            for (int i = 0; i < arr.length; i++)
  +            {
  +                conn.makePersistent(arr[i]);
  +            }
  +            tx.commit();
  +            long stop = System.currentTimeMillis();
  +            times[1] = times[1] + (stop - start);
  +        }
  +
  +        protected void readArticlesByCursor(OTMConnection conn) throws Exception
  +        {
  +            long start = System.currentTimeMillis();
  +            int artId = arr[0].articleId;
  +            org.apache.ojb.otm.core.Transaction tx = _kit.getTransaction(conn);
  +            OQLQuery query = conn.newOQLQuery(LockType.NO_LOCK);
  +            String sql = "select allArticles from " + PerformanceArticle.class.getName()
+
  +                    " where articleName = \"" + PRE_NAME + threadName + "\"";
  +            query.create(sql);
  +            Iterator iter = conn.getIteratorByOQLQuery(query);
  +            int fetchCount = 0;
  +            while (iter.hasNext())
  +            {
  +                fetchCount++;
  +                PerformanceArticle a = (PerformanceArticle) iter.next();
  +            }
               long stop = System.currentTimeMillis();
               times[2] = times[2] + (stop - start);
           }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/otm/PerformanceTest.java
  
  Index: PerformanceTest.java
  ===================================================================
  package org.apache.ojb.otm;
  
  import java.util.Iterator;
  
  import junit.framework.TestCase;
  import org.apache.ojb.otm.core.Transaction;
  import org.apache.ojb.otm.core.TransactionException;
  import org.apache.ojb.otm.lock.LockType;
  import org.apache.ojb.otm.lock.LockingException;
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.PerformanceArticle;
  import org.apache.ojb.broker.PersistenceBrokerFactory;
  import org.apache.ojb.broker.util.logging.Logger;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  import org.odmg.OQLQuery;
  
  /**
   * This TestCase contains the OJB performance benchmarks for the
   * OTM API.
   * @author Oleg Nitz, Matthew Baird, borrowing heavily from Thomas Mahler
   */
  public class PerformanceTest extends TestCase
  {
      private Logger logger = LoggerFactory.getLogger("performance");
  
      private TestKit _kit;
  
      private OTMConnection _conn;
  
      private PerformanceArticle[] arr;
      /**
       * the number of PerformanceArticle objects to work with.
       */
      static int articleCount = 10000;
      /**
       * the number of iterations to perform.
       */
      static int iterations = 2;
      /**
       * the offset value for PerformanceArticle primary keys
       */
      int offsetId = 123456;
  
      public PerformanceTest(String name)
      {
          super(name);
      }
  
      /**
       * launches the TestCase.
       * The number of Objects to work with and the number of iterations
       * to be performed can be adjusted by setting them as commandline parameters.
       * @param args the String[] holding the commandline parameters.
       */
      public static void main(String[] args)
      {
          if (args.length > 0)
          {
              articleCount = Integer.parseInt(args[0]);
          }
          if (args.length > 1)
          {
              iterations = Integer.parseInt(args[1]);
          }
          String[] arr = {PerformanceTest.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      public void setUp()
      {
          _kit = TestKit.getTestInstance();
          _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
          arr = new PerformanceArticle[articleCount];
          for (int i = 0; i < articleCount; i++)
          {
              PerformanceArticle a = createArticle(offsetId + i);
              arr[i] = a;
          }
      }
  
      public void tearDown()
      {
          _conn.close();
          _conn = null;
      }
  
      /**
       * factory method that createa an PerformanceArticle with a given id.
       * @return the created PerformanceArticle object
       * @param id the primary key value for the new object
       */
      private PerformanceArticle createArticle(int id)
      {
          PerformanceArticle a = new PerformanceArticle();
          a.setArticleId(id);
          a.setArticleName("New Performance Article " + id);
          a.setMinimumStock(100);
          a.setOrderedUnits(17);
          a.setPrice(0.45);
          a.setProductGroupId(1);
          a.setStock(234);
          a.setSupplierId(4);
          a.setUnit("bottle");
          return a;
      }
  
      /**
       * deletes all PerformanceArticle created by <code>insertNewArticles</code>.
       */
      protected void deleteArticles() throws Exception
      {
          long start = System.currentTimeMillis();
          Transaction tx = _kit.getTransaction(_conn);
          tx.begin();
          for (int i = 0; i < articleCount; i++)
          {
              _conn.deletePersistent(arr[i]);
          }
          tx.commit();
          long stop = System.currentTimeMillis();
          logger.info("deleting " + articleCount + " Objects: " + (stop - start) + " msec");
      }
  
      /**
  
       * create new PerformanceArticle objects and insert them into the RDBMS.
  
       * The number of objects to create is defined by <code>articleCount</code>.
  
       */
      protected void insertNewArticles() throws Exception
      {
          long start = System.currentTimeMillis();
          Transaction tx = _kit.getTransaction(_conn);
          tx.begin();
          for (int i = 0; i < articleCount; i++)
          {
              _conn.makePersistent(arr[i]);
          }
          tx.commit();
          long stop = System.currentTimeMillis();
          logger.info("inserting " + articleCount + " Objects: " + (stop - start) + " msec");
      }
  
      /**
       * read in all the PerformanceArticles from the RDBMS that have
       * been inserted by <code>insertNewArticles()</code>.
       * The lookup is done one by one, that is: a primary key based lookup is used.
       */
      protected void readArticles() throws Exception
      {
          long start = System.currentTimeMillis();
          Transaction tx = _kit.getTransaction(_conn);
          tx.begin();
          for (int i = 0; i < articleCount; i++)
          {
              Object[] pks = {new Integer(offsetId + i)};
              Identity oid = new Identity(PerformanceArticle.class, PerformanceArticle.class,pks);
              PerformanceArticle a = (PerformanceArticle) _conn.getObjectByIdentity(oid, LockType.NO_LOCK);
          }
          tx.commit();
          long stop = System.currentTimeMillis();
          logger.info("querying " + articleCount + " Objects: " + (stop - start) + " msec");
      }
  
      /**
       * read in all the PerformanceArticles from the RDBMS that have
       * been inserted by <code>insertNewArticles()</code>.
       * The lookup is done with a cursor fetch,
       * that is: a between Statement is used to select all inserted PerformanceArticles
       * and Objects are read in by fetching from the cursor (JDBC ResultSet).
       */
      protected void readArticlesByCursor() throws Exception
      {
          // clear the cache
          _conn.invalidateAll();
  
          Transaction tx = _kit.getTransaction(_conn);
          OQLQuery query = _conn.newOQLQuery(LockType.NO_LOCK);
          String sql = "select allArticles from " + PerformanceArticle.class.getName() + "
where articleId between " + new Integer(offsetId) + " and " + new Integer(offsetId + articleCount);
          query.create(sql);
          long start = System.currentTimeMillis();
          Iterator iter = _conn.getIteratorByOQLQuery(query);
          int fetchCount = 0;
          while (iter.hasNext())
          {
              fetchCount++;
              PerformanceArticle a = (PerformanceArticle) iter.next();
          }
          long stop = System.currentTimeMillis();
          logger.info("fetching " + fetchCount + " Objects: " + (stop - start) + " msec");
      }
  
      /**
       * updates all PerformanceArticles inserted by <code>insertNewArticles()</code>.
       * All objects are modified and changes are written to the RDBMS with an UPDATE.
       */
      protected void updateExistingArticles() throws Exception
      {
          long start = System.currentTimeMillis();
          Transaction tx = _kit.getTransaction(_conn);
          tx.begin();
          // update all objects
          for (int i = 0; i < articleCount; i++)
          {
              _conn.lockForWrite(arr[i]);
              arr[i].setPrice(arr[i].getPrice() * 1.95583);
          }
          tx.commit();
          long stop = System.currentTimeMillis();
          logger.info("updating " + articleCount + " Objects: " + (stop - start) + " msec");
      }
  
      /**
       * this method is the driver for the complete Benchmark.
       * It performs the following steps:
       *
       * 1.) n objects are created and inserted to the RDBMS.
       * 2.) the created objects are modified. Modifications are written to the RDBMS with
updates.
       * 3.) All objects created in 1.) are read in by primary key based SELECT statements.
       * 4.) Step 3.) is repeated to test caching facilities.
       * 5.) All objects created in 1.) are read by iterating over a ResultSet.
       * 6.) All objects created in 1.) are deleted with n separate DELETE Statements.
       */
      public void testBenchmark()
      {
          try
          {
              logger.info("Test for OTM-api");
              for (int i = 0; i < iterations; i++)
              {
                  logger.info("");
                  // store all Article objects
                  insertNewArticles();
                  // update all objects
                  updateExistingArticles();
                  // querying with empty cache
                  _conn.invalidateAll();
  
                  readArticles();
                  // querying with hot cache
                  readArticles();
                  // fetching through cursor
                  readArticlesByCursor();
                  // delete all objects
                  deleteArticles();
              }
          }
          catch (Throwable t)
          {
              logger.error(t);
              fail(t.getMessage());
          }
      }
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message