You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/02/06 02:32:16 UTC

cvs commit: db-ojb/src/test/org/apache/ojb/broker OJBPerfTest.java

arminw      2003/02/05 17:32:16

  Added:       src/test/org/apache/ojb/broker OJBPerfTest.java
  Log:
  implementation classes for PB,ODMG api
  using the new performance "framework"
  
  Revision  Changes    Path
  1.1                  db-ojb/src/test/org/apache/ojb/broker/OJBPerfTest.java
  
  Index: OJBPerfTest.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import org.apache.ojb.broker.query.Criteria;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.query.QueryByCriteria;
  import org.apache.ojb.broker.util.ObjectModificationDefaultImpl;
  import org.apache.ojb.odmg.OJB;
  import org.apache.ojb.performance.PerfArticle;
  import org.apache.ojb.performance.PerfArticleImpl;
  import org.apache.ojb.performance.PerfHandle;
  import org.apache.ojb.performance.PerfTest;
  import org.odmg.Database;
  import org.odmg.Implementation;
  import org.odmg.ODMGException;
  import org.odmg.OQLQuery;
  import org.odmg.Transaction;
  
  import java.math.BigDecimal;
  import java.util.Collection;
  import java.util.List;
  
  /**
   * The performance test implementation classes for testing
   * the PB-api and ODMG-api of OJB.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: OJBPerfTest.java,v 1.1 2003/02/06 01:32:15 arminw Exp $
   */
  public class OJBPerfTest
  {
  
      // =====================================================================================
      // Inner class
      // =====================================================================================
      public static class PBPerfTest extends PerfTest
      {
          public PBPerfTest()
          {
          }
  
          public String testName()
          {
              return "PB";
          }
  
          public PerfHandle newPerfHandle(PerfTest test)
          {
              return new PBPerfHandle(test);
          }
  
          public int articleCount()
          {
              Criteria c = new Criteria();
              Query q = new QueryByCriteria(PerfArticleImpl.class, c);
              int count = 0;
              try
              {
                  PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  count = broker.getCount(q);
                  broker.close();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
              return count;
          }
      }
  
      // =====================================================================================
      // Inner class
      // =====================================================================================
      public static class PBPerfHandle extends PerfHandle
      {
          public PBPerfHandle(PerfTest test)
          {
              super(test);
          }
  
          public void init() throws Exception
          {
          }
  
          /**
           * factory method that creates an {@link org.apache.ojb.performance.PerfArticle}
           * @param articleName set the 'articleName'
           * @return the created PerfArticle object
           */
          public PerfArticle newPerfArticle(String articleName)
          {
              PerfArticle a = new PerfArticleImpl();
              a.setArticleName(articleName);
              a.setMinimumStock(100);
              a.setPrice(new BigDecimal(0.45));
              a.setProductGroupId(1);
              a.setStock(234);
              a.setSupplierId(4);
              a.setUnit("bottle");
              return a;
          }
  
          /**
           * A resource cumbering insert-method implementation,
           * use this to test implementation - set
           * {@link useStressMode} to <code>true</code>
           */
          public void insertNewArticlesStress(PerfArticle[] arr) throws Exception
          {
              for (int i = 0; i < arr.length; i++)
              {
                  PersistenceBroker broker = null;
                  try
                  {
                      broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                      broker.beginTransaction();
                      broker.store(arr[i]);
                      broker.commitTransaction();
                  }
                  finally
                  {
                      if (broker != null) broker.close();
                  }
  
              }
          }
  
          /**
           * A performance optimized insert-method implementation,
           * use this to test performance - set
           * {@link useStressMode} to <code>false</code>
           */
          public void insertNewArticles(PerfArticle[] arr) throws Exception
          {
              ObjectModificationDefaultImpl needsInsert = new ObjectModificationDefaultImpl();
              needsInsert.setNeedsInsert(true);
              PersistenceBroker broker = null;
              try
              {
                  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  broker.serviceConnectionManager().setBatchMode(true);
                  broker.beginTransaction();
                  for (int i = 0; i < arr.length; i++)
                  {
                      broker.store(arr[i], needsInsert);
                  }
                  broker.commitTransaction();
              }
              finally
              {
                  if (broker != null) broker.close();
              }
          }
  
          public Collection readArticlesByCursor(String articleName) throws Exception
          {
              Criteria c = new Criteria();
              c.addLike("articleName", articleName);
              Query q = new QueryByCriteria(PerfArticleImpl.class, c);
  
              Collection col = null;
              PersistenceBroker broker = null;
              try
              {
                  broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  col = broker.getCollectionByQuery(q);
              }
              finally
              {
                  if (broker != null) broker.close();
              }
              return col;
          }
  
          /**
           * A resource cumbering delete-method implementation,
           * use this to test implementation - set
           * {@link useStressMode} to <code>true</code>
           */
          public void deleteArticlesStress(PerfArticle[] arr) throws Exception
          {
              for (int i = 0; i < arr.length; i++)
              {
                  PersistenceBroker broker = null;
                  try
                  {
                      broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                      broker.beginTransaction();
                      broker.delete(arr[i]);
                      broker.commitTransaction();
                  }
                  finally
                  {
                      if (broker != null) broker.close();
                  }
              }
          }
  
          /**
           * A performance optimized delete-method implementation,
           * use this to test performance - set
           * {@link useStressMode} to <code>false</code>
           */
          public void deleteArticles(PerfArticle[] arr) throws Exception
          {
              PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
              try
              {
                  broker.serviceConnectionManager().setBatchMode(true);
                  broker.beginTransaction();
                  for (int i = 0; i < arr.length; i++)
                  {
                      broker.delete(arr[i]);
                  }
                  broker.commitTransaction();
              }
              finally
              {
                  if (broker != null) broker.close();
              }
          }
  
          public void tearDown() throws Exception
          {
          }
      }
  
      // =====================================================================================
      // Inner class
      // =====================================================================================
      public static class ODMGPerfTest extends PerfTest
      {
          public ODMGPerfTest()
          {
          }
  
          public String testName()
          {
              return "ODMG";
          }
  
          public PerfHandle newPerfHandle(PerfTest test)
          {
              return new ODMGPerfHandle(test);
          }
  
          public int articleCount()
          {
              Criteria c = new Criteria();
              Query q = new QueryByCriteria(PerfArticleImpl.class, c);
              int count = 0;
              try
              {
                  PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
                  count = broker.getCount(q);
                  broker.close();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
              return count;
          }
      }
  
      // =====================================================================================
      // Inner class
      // =====================================================================================
      public static class ODMGPerfHandle extends PerfHandle
      {
          private Implementation odmg;
          private Database db;
          private Transaction tx;
  
          public ODMGPerfHandle(PerfTest test)
          {
              super(test);
          }
  
          public void init()
          {
              try
              {
                  odmg = OJB.getInstance();
                  db = odmg.newDatabase();
                  db.open(TestHelper.DEF_DATABASE_NAME, Database.OPEN_READ_WRITE);
                  tx = odmg.newTransaction();
              }
              catch (ODMGException e)
              {
                  e.printStackTrace();
              }
          }
  
          public void tearDown() throws Exception
          {
              if(tx.isOpen()) tx.abort();
              db.close();
          }
  
          /**
           * A performance optimized insert-method implementation,
           * use this to test performance - set
           * {@link useStressMode} to <code>false</code>
           */
          public void insertNewArticles(PerfArticle[] arr) throws Exception
          {
              tx.begin();
              for (int i = 0; i < arr.length; i++)
              {
                  tx.lock(arr[i], Transaction.WRITE);
              }
              tx.commit();
          }
  
          /**
           * A resource cumbering insert-method implementation,
           * use this to test implementation - set
           * {@link useStressMode} to <code>true</code>
           */
          public void insertNewArticlesStress(PerfArticle[] arr) throws Exception
          {
              for (int i = 0; i < arr.length; i++)
              {
                  Transaction tx = odmg.newTransaction();
                  tx.begin();
                  tx.lock(arr[i], Transaction.WRITE);
                  tx.commit();
              }
          }
  
          public Collection readArticlesByCursor(String articleName) throws Exception
          {
              tx.begin();
              OQLQuery query = odmg.newOQLQuery();
              String sql = "select allArticles from " + PerfArticleImpl.class.getName() +
                      " where articleName like \"" + articleName + "\"";
              query.create(sql);
              List allProducts = (List) query.execute();
              tx.commit();
              return allProducts;
          }
  
          /**
           * A performance optimized delete-method implementation,
           * use this to test performance - set
           * {@link useStressMode} to <code>false</code>
           */
          public void deleteArticles(PerfArticle[] arr) throws Exception
          {
              tx.begin();
              for (int i = 0; i < arr.length; i++)
              {
                  db.deletePersistent(arr[i]);
              }
              tx.commit();
          }
  
          /**
           * A resource cumbering insert-method implementation,
           * use this to test implementation - set
           * {@link useStressMode} to <code>true</code>
           */
          public void deleteArticlesStress(PerfArticle[] arr) throws Exception
          {
              for (int i = 0; i < arr.length; i++)
              {
                  Transaction tx = odmg.newTransaction();
                  tx.begin();
                  db.deletePersistent(arr[i]);
                  tx.commit();
              }
          }
  
          /**
           * factory method that creates an PerfArticle
           * @param articleName set the 'articleName'
           * @return the created PerfArticle object
           */
          public PerfArticle newPerfArticle(String articleName)
          {
              PerfArticle a = new PerfArticleImpl();
              a.setArticleName(articleName);
              a.setMinimumStock(100);
              a.setPrice(new BigDecimal(0.45));
              a.setProductGroupId(1);
              a.setStock(234);
              a.setSupplierId(4);
              a.setUnit("bottle");
              return a;
          }
      }
  }