Package perfs

Source Code of perfs.Producer

/*
* JORAM: Java(TM) Open Reliable Asynchronous Messaging
* Copyright (C) 2011 ScalAgent Distributed Technologies
*
* This library 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 any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package perfs;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.InitialContext;

import org.objectweb.joram.client.jms.Destination;

/**
* MessageProducer sending messages on queue or topic for performance statistics.
*/
public class Producer {
  static int Round = 50;
  static int NbMsgPerRound = 1000;
  static int MsgSize = 1000;
  static int mps = 10000;
 
  static Destination dest = null;
  static ConnectionFactory cf = null;

  static boolean MsgTransient = true;
  static boolean SwapAllowed = false;
  static boolean transacted = true;

  public static boolean getBoolean(String key, boolean def) {
    String value = System.getProperty(key, Boolean.toString(def));
    return Boolean.parseBoolean(value);
  }
 
  public static void main (String args[]) throws Exception {
    Round = Integer.getInteger("Round", Round).intValue();
    MsgSize = Integer.getInteger("MsgSize", MsgSize).intValue();

    MsgTransient = getBoolean("MsgTransient", MsgTransient);
    SwapAllowed = getBoolean("SwapAllowed", SwapAllowed);
    transacted = getBoolean("Transacted", transacted);
   
    InitialContext ictx = new InitialContext();
    Destination dest = (Destination) ictx.lookup(args[0]);
    ConnectionFactory cf = (ConnectionFactory) ictx.lookup("cf");
    ictx.close();

    System.out.println("Destination: " + (dest.isQueue()?"Queue":"Topic"));
    System.out.println("Message: MsgTransient=" + MsgTransient);
    System.out.println("Message: SwapAllowed=" + SwapAllowed);
    System.out.println("Transacted=" + transacted);
    System.out.println("NbMsg=" + (Round*NbMsgPerRound) + ", MsgSize=" + MsgSize);

    Connection cnx = cf.createConnection();
    Session session = cnx.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(dest);
    if (MsgTransient) {
      producer.setDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
    }

    byte[] content = new byte[MsgSize];
    for (int i = 0; i< MsgSize; i++)
      content[i] = (byte) (i & 0xFF);
   
    long dtx = 0;
    long start = System.currentTimeMillis();
    for (int i=0; i<(Round*NbMsgPerRound); i++) {
      BytesMessage msg = session.createBytesMessage();
      if (SwapAllowed) {
        msg.setBooleanProperty("JMS_JORAM_SWAPALLOWED", true);
      }
      msg.writeBytes(content);
      msg.setLongProperty("time", System.currentTimeMillis());
      msg.setIntProperty("index", i);
      producer.send(msg);
     
      if (transacted && ((i%10) == 9)) session.commit();
     
      if ((i%NbMsgPerRound) == (NbMsgPerRound-1)) {
        long dtx1 = (i * 1000L) / mps;
        long dtx2 = System.currentTimeMillis() - start;
        if (dtx1 > (dtx2 + 20)) {
          dtx += (dtx1 - dtx2);
          Thread.sleep(dtx1 - dtx2);
        }
      }
    }
    long end = System.currentTimeMillis();
    long dt = end - start;
   
    System.out.println("----------------------------------------------------");
    System.out.println("| sender dt=" ((dt *1000L)/(Round*NbMsgPerRound)) + "us -> " +
                        ((1000L * (Round*NbMsgPerRound)) / (dt)) + "msg/s");
    System.out.println("| sender wait=" + dtx + "ms");
   
    cnx.close();
  }

}
TOP

Related Classes of perfs.Producer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.