[jboss-cvs] JBoss Messaging SVN: r4195 - in trunk/examples/jms: etc and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed May 14 12:28:42 EDT 2008


Author: timfox
Date: 2008-05-14 12:28:42 -0400 (Wed, 14 May 2008)
New Revision: 4195

Added:
   trunk/examples/jms/src/org/jboss/jms/example/PerfTransactionExample.java
Modified:
   trunk/examples/jms/build.xml
   trunk/examples/jms/etc/jndi.properties
   trunk/examples/jms/src/org/jboss/jms/example/PerfExample.java
Log:
Added example


Modified: trunk/examples/jms/build.xml
===================================================================
--- trunk/examples/jms/build.xml	2008-05-14 16:17:20 UTC (rev 4194)
+++ trunk/examples/jms/build.xml	2008-05-14 16:28:42 UTC (rev 4195)
@@ -42,7 +42,7 @@
 
    <!--perf props-->
    <property name="message.count" value="200000"/>
-   <property name="delivery.mode" value="PERSISTENT"/>
+   <property name="delivery.mode" value="NON_PERSISTENT"/>
    <property name="sample.period" value="1"/>
 
    <path id="compile.classpath">
@@ -139,7 +139,7 @@
       	<jvmarg value="-XX:+UseParallelGC"/>
          <arg value="-s"/>
          <arg value="${message.count}"/>
-         <arg value="${delivery.mode}"/>
+         <arg value="PERSISTENT"/>
          <arg value="${sample.period}"/>
       </java>
    </target>

Modified: trunk/examples/jms/etc/jndi.properties
===================================================================
--- trunk/examples/jms/etc/jndi.properties	2008-05-14 16:17:20 UTC (rev 4194)
+++ trunk/examples/jms/etc/jndi.properties	2008-05-14 16:28:42 UTC (rev 4195)
@@ -1,3 +1,3 @@
 java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
-java.naming.provider.url=jnp://localhost:1099
-java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
\ No newline at end of file
+java.naming.provider.url=jnp://192.168.1.13:1099
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

Modified: trunk/examples/jms/src/org/jboss/jms/example/PerfExample.java
===================================================================
--- trunk/examples/jms/src/org/jboss/jms/example/PerfExample.java	2008-05-14 16:17:20 UTC (rev 4194)
+++ trunk/examples/jms/src/org/jboss/jms/example/PerfExample.java	2008-05-14 16:28:42 UTC (rev 4195)
@@ -33,6 +33,7 @@
  * a performance example that can be used to gather simple performance figures.
  * 
  * @author <a href="ataylor at redhat.com">Andy Taylor</a>
+ * @author <a href="tim.fox at jboss.com">Tim Fox</a>
  */
 public class PerfExample
 {
@@ -68,7 +69,7 @@
       queue = (Queue) initialContext.lookup("/queue/testPerfQueue");
       ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
       connection = cf.createConnection();
-      session = connection.createSession(true, Session.SESSION_TRANSACTED);
+      session = connection.createSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    }
    
    public void runSender(int noOfMessage, int deliveryMode, long samplePeriod)
@@ -91,25 +92,11 @@
          BytesMessage bytesMessage = session.createBytesMessage();
          byte[] payload = new byte[1024];
          bytesMessage.writeBytes(payload);
-         boolean committed = false;
          for (int i = 1; i <= noOfMessage; i++)
          {
             producer.send(bytesMessage);
-            messageCount++;
-            if (messageCount % 10000 == 0)
-            {
-               session.commit();
-               committed = true;
-            }
-            else
-            {
-               committed = false;
-            }
+            messageCount++;            
          }
-         if (!committed)
-         {
-            session.commit();
-         }
          scheduler.shutdownNow();
          log.info("average " +  command.getAverage() + " per " + (perfParams.getSamplePeriod()/1000) + " secs" );
       }
@@ -212,13 +199,8 @@
             {
                BytesMessage bm = (BytesMessage) message;
                messageCount++;      
-               if (messageCount % 10000 == 0)
-               {
-                  session.commit();
-               }
                if (messageCount == params.getNoOfMessagesToSend())
                {
-                  session.commit();
                   countDownLatch.countDown();
                   scheduler.shutdownNow();
                   log.info("average " +  command.getAverage() + " per " + (params.getSamplePeriod()/1000) + " secs" );

Added: trunk/examples/jms/src/org/jboss/jms/example/PerfTransactionExample.java
===================================================================
--- trunk/examples/jms/src/org/jboss/jms/example/PerfTransactionExample.java	                        (rev 0)
+++ trunk/examples/jms/src/org/jboss/jms/example/PerfTransactionExample.java	2008-05-14 16:28:42 UTC (rev 4195)
@@ -0,0 +1,272 @@
+/*
+   * JBoss, Home of Professional Open Source
+   * Copyright 2005, 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.jms.example;
+
+import org.jboss.messaging.core.logging.Logger;
+import org.jboss.jms.util.PerfParams;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.jms.*;
+import java.util.concurrent.*;
+
+/**
+ * a performance example that can be used to gather simple performance figures.
+ * 
+ * @author <a href="ataylor at redhat.com">Andy Taylor</a>
+ * @author <a href="tim.fox at jboss.com">Tim Fox</a>
+ */
+public class PerfTransactionExample
+{
+   private static Logger log = Logger.getLogger(PerfExample.class);
+   private Queue queue;
+   private Connection connection;
+   private int messageCount = 0;
+   private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
+   private Session session;
+   private Sampler command = new Sampler();
+   
+   private static final int transactionSize = 5000;
+
+   public static void main(String[] args)
+   {
+      PerfTransactionExample perfExample = new PerfTransactionExample();
+      if (args[0].equalsIgnoreCase("-l"))
+      {
+         perfExample.runListener();
+      }
+      else
+      {
+         int noOfMessages = Integer.parseInt(args[1]);
+         int deliveryMode = args[2].equalsIgnoreCase("persistent")? DeliveryMode.PERSISTENT: DeliveryMode.NON_PERSISTENT;
+         long samplePeriod = Long.parseLong(args[3]);
+         perfExample.runSender(noOfMessages, deliveryMode, samplePeriod);
+      }
+
+   }
+
+   private void init()
+           throws NamingException, JMSException
+   {
+      InitialContext initialContext = new InitialContext();
+      queue = (Queue) initialContext.lookup("/queue/testPerfQueue");
+      ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
+      connection = cf.createConnection();
+      session = connection.createSession(true, Session.SESSION_TRANSACTED);
+   }
+   
+   public void runSender(int noOfMessage, int deliveryMode, long samplePeriod)
+   {
+      try
+      {
+         init();
+         MessageProducer producer = session.createProducer(queue);
+         producer.setDisableMessageID(true);
+         producer.setDisableMessageTimestamp(true);
+         producer.setDeliveryMode(deliveryMode);
+         ObjectMessage m = session.createObjectMessage();
+         PerfParams perfParams = new PerfParams();
+         perfParams.setNoOfMessagesToSend(noOfMessage);
+         perfParams.setDeliveryMode(deliveryMode);
+         perfParams.setSamplePeriod(samplePeriod);
+         m.setObject(perfParams);
+         producer.send(m);
+         scheduler.scheduleAtFixedRate(command, perfParams.getSamplePeriod(), perfParams.getSamplePeriod(), TimeUnit.SECONDS);
+         BytesMessage bytesMessage = session.createBytesMessage();
+         byte[] payload = new byte[1024];
+         bytesMessage.writeBytes(payload);
+         boolean committed = false;
+         for (int i = 1; i <= noOfMessage; i++)
+         {
+            producer.send(bytesMessage);
+            messageCount++;
+            if (messageCount % transactionSize == 0)
+            {
+               session.commit();
+               committed = true;
+            }
+            else
+            {
+               committed = false;
+            }
+         }
+         if (!committed)
+         {
+            session.commit();
+         }
+         scheduler.shutdownNow();
+         log.info("average " +  command.getAverage() + " per " + (perfParams.getSamplePeriod()/1000) + " secs" );
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+      }
+      finally
+      {
+         if (connection != null)
+            try
+            {
+               connection.close();
+            }
+            catch (JMSException e)
+            {
+               e.printStackTrace();
+            }
+      }
+   }
+
+   public void runListener()
+   {
+      try
+      {
+         init();
+         MessageConsumer messageConsumer = session.createConsumer(queue);
+         CountDownLatch countDownLatch = new CountDownLatch(1);
+         connection.start();
+         log.info("emptying queue");
+         while (true)
+         {
+            Message m = messageConsumer.receive(500);
+            if (m == null)
+            {
+               break;
+            }
+         }
+         messageConsumer.setMessageListener(new PerfListener(countDownLatch));
+         log.info("READY!!!");
+         countDownLatch.await();
+      }
+      catch (Exception e)
+      {
+         e.printStackTrace();
+      }
+      finally
+      {
+         if (connection != null)
+            try
+            {
+               connection.close();
+            }
+            catch (JMSException e)
+            {
+               e.printStackTrace();
+            }
+      }
+   }
+
+
+
+   /**
+    * a message listener
+    */
+   class PerfListener implements MessageListener
+   {
+      private boolean started = false;
+      private PerfParams params = null;
+      int count = 0;
+      private CountDownLatch countDownLatch;
+
+
+      public PerfListener(CountDownLatch countDownLatch)
+      {
+         this.countDownLatch = countDownLatch;
+      }
+
+      public void onMessage(Message message)
+      {
+         if (!started)
+         {
+            started = true;
+            ObjectMessage m = (ObjectMessage) message;
+            try
+            {
+               params = (PerfParams) m.getObject();
+            }
+            catch (JMSException e)
+            {
+               params = new PerfParams();
+            }
+            log.info("params = " + params);
+            scheduler.scheduleAtFixedRate(command, params.getSamplePeriod(), params.getSamplePeriod(), TimeUnit.SECONDS);
+         }
+         else
+         {
+            try
+            {
+               boolean committed = false;
+               BytesMessage bm = (BytesMessage) message;
+               messageCount++;      
+               if (messageCount % transactionSize == 0)
+               {
+                  session.commit();
+                  committed = true;
+               }
+               if (messageCount == params.getNoOfMessagesToSend())
+               {
+                  if (!committed)
+                  {
+                     session.commit();
+                  }
+                  countDownLatch.countDown();
+                  scheduler.shutdownNow();
+                  log.info("average " +  command.getAverage() + " per " + (params.getSamplePeriod()/1000) + " secs" );
+               }
+            }
+            catch (Exception e)
+            {
+               e.printStackTrace();
+            }
+         }
+      }
+   }
+
+   /**
+    * simple class to gather performance figures
+    */
+   class Sampler implements Runnable
+   {
+      int sampleCount = 0;
+
+      long startTime = 0;
+
+      long samplesTaken = 0;
+
+      public void run()
+      {
+         if(startTime == 0)
+         {
+            startTime = System.currentTimeMillis();
+         }
+         long elapsed = System.currentTimeMillis() - startTime;
+         int lastCount = sampleCount;
+         sampleCount = messageCount;
+         samplesTaken++;
+         log.info(" time elapsed " + (elapsed / 1000) + " secs, message count " + (sampleCount) + " : this period " + (sampleCount - lastCount));
+      }
+
+      public long getAverage()
+      {
+         return sampleCount/samplesTaken;
+      }
+
+   }
+}




More information about the jboss-cvs-commits mailing list