[jboss-cvs] JBossAS SVN: r111737 - in projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv: tests and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Jul 8 16:12:46 EDT 2011


Author: jesper.pedersen
Date: 2011-07-08 16:12:46 -0400 (Fri, 08 Jul 2011)
New Revision: 111737

Added:
   projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/tests/RampDatabaseTester.java
Modified:
   projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/TestRunner.java
Log:
Initial ramp database tester

Modified: projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/TestRunner.java
===================================================================
--- projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/TestRunner.java	2011-07-08 17:55:31 UTC (rev 111736)
+++ projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/TestRunner.java	2011-07-08 20:12:46 UTC (rev 111737)
@@ -96,6 +96,15 @@
             }
          }
 
+         if (testArgs != null)
+         {
+            log.info("Executing: " + testCase + Arrays.toString(testArgs));
+         }
+         else
+         {
+            log.info("Executing: " + testCase);
+         }
+
          return test.run(testArgs);
       }
       catch (Throwable t)

Added: projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/tests/RampDatabaseTester.java
===================================================================
--- projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/tests/RampDatabaseTester.java	                        (rev 0)
+++ projects/jboss-jca/branches/performance/perfenv/src/main/java/org/jboss/jca/performance/perfenv/tests/RampDatabaseTester.java	2011-07-08 20:12:46 UTC (rev 111737)
@@ -0,0 +1,341 @@
+ /*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2010, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+package org.jboss.jca.performance.perfenv.tests;
+
+import org.jboss.jca.performance.perfenv.Test;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.StringWriter;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+import javax.transaction.UserTransaction;
+
+import org.jboss.logging.Logger;
+
+/**
+ * A test application for database access that uses a ramp
+ * 
+ * @author <a href="jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+public class RampDatabaseTester implements Test
+{
+   /** The logger */
+   private static Logger log = Logger.getLogger("RampDatabaseTester");
+
+   /** User transaction JNDI name */
+   private static final String JNDI_USER_TRANSACTION = "java:/UserTransaction";
+
+   /** Ramp up */
+   private int rampUp;
+
+   /** Test period */
+   private int testPeriod;
+
+   /** Ramp down */
+   private int rampDown;
+
+   /** Multithreaded execution */
+   private boolean multiThreaded;
+
+   /** Max number of threads */
+   private int maxThreads;
+
+   /** The JNDI for the datasource */
+   private String dbJndiName;
+
+   /** The list of SQL statements to execute */
+   private List<String> sqls;
+
+   /**
+    * Constructor
+    */
+   public RampDatabaseTester()
+   {
+      this.rampUp = 0;
+      this.testPeriod = 0;
+      this.rampDown = 0;
+      this.dbJndiName = null;
+
+      this.multiThreaded = false;
+      this.maxThreads = 100;
+
+      this.sqls = new ArrayList<String>(1);
+      this.sqls.add("SELECT 1");
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   public Serializable run(Serializable[] args)
+   {
+      if (args == null || args.length < 4)
+         return new IllegalArgumentException("Unsupported argument list: " + Arrays.toString(args));
+
+      try
+      {
+         rampUp = Integer.valueOf((String)args[0]);
+         testPeriod = Integer.valueOf((String)args[1]);
+         rampDown = Integer.valueOf((String)args[2]);
+         dbJndiName = (String)args[3];
+
+         if (args.length > 4)
+            multiThreaded = Boolean.valueOf((String)args[4]);
+
+         if (args.length > 5)
+            maxThreads = Integer.valueOf((String)args[5]);
+
+         if (dbJndiName == null || dbJndiName.trim().equals(""))
+            throw new IllegalArgumentException("No datasource JNDI name defined");
+
+         Context context = new InitialContext();
+         UserTransaction userTransaction = (UserTransaction)context.lookup(JNDI_USER_TRANSACTION);
+         DataSource dataSource = (DataSource)context.lookup(dbJndiName);
+
+         long rampUpStart = System.currentTimeMillis();
+         execute(rampUp * 60000, dataSource, userTransaction);
+         long rampUpEnd = System.currentTimeMillis();
+
+         long testPeriodStart = System.currentTimeMillis();
+         int count = execute(testPeriod * 60000, dataSource, userTransaction);
+         long testPeriodEnd = System.currentTimeMillis();
+
+         long rampDownStart = System.currentTimeMillis();
+         execute(rampDown * 60000, dataSource, userTransaction);
+         long rampDownEnd = System.currentTimeMillis();
+
+         long elapsed = testPeriodEnd - testPeriodStart;
+         if (elapsed <= 0)
+            elapsed = 1;
+
+         log.info("Ramp up  : " + (rampUpEnd - rampUpStart) + " ms");
+         log.info("Test     : " + (testPeriodEnd - testPeriodStart) + " ms");
+         log.info("Ramp down: " + (rampDownEnd - rampDownStart) + " ms");
+         log.info("Unit of work/ms: " + (count / (double)elapsed));
+
+         context.close();      
+
+         return new Double((count * 1000L) / (double)elapsed);
+      }
+      catch (Throwable t)
+      {
+         StringWriter sw = new StringWriter();
+         sw.write(t.getMessage());
+         sw.write('\n');
+
+         t.printStackTrace(new PrintWriter(sw));
+
+         return new Exception(sw.toString());
+      }
+   }
+
+   /**
+    * Execute for a duration
+    * @param duration The duration of execution
+    * @param dataSource The data source
+    * @param userTransaction The user transaction
+    * @return The number of invocations
+    */
+   private int execute(long duration, DataSource dataSource, UserTransaction userTransaction)
+   {
+      AtomicInteger counter = new AtomicInteger(0);
+
+      if (multiThreaded)
+      {
+         try
+         {
+            BlockingQueue<Runnable> threadPoolQueue = new LinkedBlockingQueue<Runnable>();
+            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maxThreads, maxThreads,
+                                                                           60, TimeUnit.SECONDS,
+                                                                           threadPoolQueue);
+
+            threadPoolExecutor.allowCoreThreadTimeOut(true);
+            threadPoolExecutor.prestartAllCoreThreads();
+               
+            long start = System.currentTimeMillis();
+            long now = start;
+
+            while ((now - start) < duration)
+            {
+               if (threadPoolQueue.size() < maxThreads)
+               {
+                  TestCase t = new TestCase(dataSource, userTransaction, sqls, counter);
+                  threadPoolExecutor.submit(t);
+               }
+
+               now = System.currentTimeMillis();
+            }
+
+            threadPoolExecutor.shutdown();
+         }
+         catch (Throwable t)
+         {
+            // Ignore
+         }
+      }
+      else
+      {
+         TestCase t = new TestCase(dataSource, userTransaction, sqls);
+
+         long start = System.currentTimeMillis();
+         long now = start;
+
+         while ((now - start) < duration)
+         {
+            t.run();
+            counter.incrementAndGet();
+
+            now = System.currentTimeMillis();
+         }
+      }
+
+      return counter.get();
+   }
+
+
+   /**
+    * Test class
+    */
+   private static class TestCase implements Runnable
+   {
+      private DataSource dataSource;
+      private UserTransaction userTransaction;
+      private List<String> sqls;
+      private AtomicInteger counter;
+
+      TestCase(DataSource db, UserTransaction ut, List<String> sqls)
+      {
+         this(db, ut, sqls, null);
+      }
+
+      TestCase(DataSource db, UserTransaction ut, List<String> sqls, AtomicInteger counter)
+      {
+         this.dataSource = db;
+         this.userTransaction = ut;
+         this.sqls = sqls;
+         this.counter = counter;
+      }
+
+      public void run()
+      {
+         if (dataSource == null)
+            throw new IllegalArgumentException("No datasource defined");
+
+         if (userTransaction == null)
+            throw new IllegalArgumentException("UserTransaction is null");
+
+         Connection connection = null;
+
+         try
+         {
+            userTransaction.begin();
+
+            connection = dataSource.getConnection();
+
+            PreparedStatement ps = null;
+            ResultSet resultSet = null;
+
+            if (sqls != null)
+            {
+               for (String sqlStatement : sqls)
+               {
+                  log.debug("Executing: " + sqlStatement);
+
+                  try
+                  {
+                     ps = connection.prepareStatement(sqlStatement);
+                     resultSet = ps.executeQuery();
+                  }
+                  finally
+                  {
+                     try
+                     {
+                        if (resultSet != null)
+                           resultSet.close();
+                     }
+                     catch (SQLException se) 
+                     {
+                        // Ignore
+                     }
+                     try
+                     {
+                        if (ps != null)
+                           ps.close();
+                     }
+                     catch (SQLException se)
+                     {
+                        // Ignore
+                     }
+                  }
+               }
+            }
+            
+            userTransaction.commit();
+         }
+         catch (Throwable t)
+         {
+            try
+            {
+               if (userTransaction != null)
+                  userTransaction.rollback();
+            }
+            catch (Throwable inner)
+            {
+               // Ignore
+            }
+
+            log.error(t.getMessage(), t);
+         }
+         finally
+         {
+            try
+            {
+               if (connection != null)
+                  connection.close();
+            }
+            catch (SQLException se)
+            {
+               // Ignore
+            }
+
+            if (counter != null)
+               counter.incrementAndGet();
+         }
+      }
+   }
+}



More information about the jboss-cvs-commits mailing list