1 /*------------------------------------------------------------------------------
  2 Name:      RamTest.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Load test for xmlBlaster
  6 Version:   $Id: RamTest.java 14846 2006-03-07 17:14:22Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.test.stress;
  9 
 10 import java.util.logging.Logger;
 11 import org.xmlBlaster.util.Global;
 12 import org.xmlBlaster.util.StopWatch;
 13 
 14 import org.xmlBlaster.util.XmlBlasterException;
 15 import org.xmlBlaster.util.MsgUnit;
 16 import org.xmlBlaster.client.qos.ConnectQos;
 17 import org.xmlBlaster.client.qos.PublishReturnQos;
 18 import org.xmlBlaster.client.qos.EraseReturnQos;
 19 import org.xmlBlaster.client.I_XmlBlasterAccess;
 20 
 21 import junit.framework.*;
 22 
 23 
 24 /**
 25  * This client publishes 1000 different messages to measure RAM consumption/message.
 26  * <br />
 27  * The RAM consumption in kByte/Message is logged to the console.
 28  * <br />
 29  * Note that this is the net RAM consumption, without any content and a very small XmlKey.
 30  * You may see this as the internal memory overhead in xmlBlaster for each published message.
 31  * <br />
 32  * This client may be invoked multiple time on the same xmlBlaster server,
 33  * as it cleans up everything after his tests are done.
 34  * <p>
 35  * Invoke examples:<br />
 36  * <pre>
 37  *    java junit.textui.TestRunner org.xmlBlaster.test.stress.RamTest
 38  *    java junit.swingui.TestRunner org.xmlBlaster.test.stress.RamTest
 39  * </pre>
 40  */
 41 public class RamTest extends TestCase
 42 {
 43    private final Global glob;
 44    private static Logger log = Logger.getLogger(RamTest.class.getName());
 45    private StopWatch stopWatch = null;
 46 
 47    private I_XmlBlasterAccess senderConnection;
 48    private String senderName;
 49    private String senderContent;
 50 
 51    private final int numPublish;        // 100;
 52    private final String contentMime = "text/plain";
 53    private final String contentMimeExtended = "1.0";
 54 
 55    /** Constructor for Junit */
 56    public RamTest() {
 57       this(new Global(), "RamTest", "RamTest", 1000);
 58    }
 59 
 60    /**
 61     * Constructs the RamTest object.
 62     * <p />
 63     * @param testName  The name used in the test suite
 64     * @param loginName The name to login to the xmlBlaster
 65     * @param numPublish The number of messages to send
 66     */
 67    public RamTest(Global glob, String testName, String loginName, int numPublish)
 68    {
 69       super(testName);
 70       this.glob = glob;
 71 
 72       this.senderName = loginName;
 73       this.numPublish = numPublish;
 74    }
 75 
 76 
 77    /**
 78     * Sets up the fixture.
 79     * <p />
 80     * Connect to xmlBlaster and login
 81     */
 82    protected void setUp()
 83    {
 84       try {
 85          senderConnection = glob.getXmlBlasterAccess(); // Find orb
 86          String passwd = "secret";
 87          ConnectQos connectQos = new ConnectQos(glob, senderName, passwd);
 88          senderConnection.connect(connectQos, null); // Login to xmlBlaster without Callback
 89       }
 90       catch (Exception e) {
 91           log.severe(e.toString());
 92           e.printStackTrace();
 93       }
 94 
 95    }
 96 
 97 
 98    /**
 99     * Tears down the fixture.
100     * <p />
101     * cleaning up .... erase() the previous message OID and logout
102     */
103    protected void tearDown()
104    {
105       log.info("tearDown() ...");
106       stopWatch = new StopWatch();
107 
108       for (int ii=0; ii<numPublish; ii++) {
109          String xmlKey = "<key oid='RamTest-" + (ii+1) + "'>\n" +
110                          "</key>";
111          String qos = "<qos></qos>";
112          try {
113             EraseReturnQos[] arr = senderConnection.erase(xmlKey, qos);
114             assertTrue("returned erased oid array == null", null != arr);
115             assertEquals("num erased messages is wrong", 1, arr.length);
116          } catch(XmlBlasterException e) { log.severe("XmlBlasterException: " + e.getMessage()); }
117       }
118 
119       long avg = 0;
120       double elapsed = stopWatch.elapsed();
121       if (elapsed > 0.)
122          avg = (long)(1000.0 * numPublish / elapsed);
123       log.info("Success: Erasing done, " + numPublish + " messages erased, average messages/second = " + avg);
124 
125       senderConnection.disconnect(null);
126    }
127 
128 
129    /**
130     * TEST: Construct a message and publish it.
131     * <p />
132     * The returned publishOid is checked
133     */
134    public void doPublish()
135    {
136       log.info("Publishing " + numPublish + " messages ...");
137 
138       long usedMemBefore = 0L;
139 
140       MsgUnit[] msgUnitArr = new MsgUnit[numPublish];
141 
142       try {
143          for (int ii=0; ii<numPublish; ii++) {
144             String xmlKey = "<key oid='RamTest-" + (ii+1) + "' contentMime='" + contentMime + "' contentMimeExtended='" + contentMimeExtended + "'>\n" +
145                             "   <RamTest-AGENT id='192.168.124.10' subId='1' type='generic'>" +
146                             "      <RamTest-DRIVER id='FileProof' pollingFreq='10'>" +
147                             "      </RamTest-DRIVER>"+
148                             "   </RamTest-AGENT>" +
149                             "</key>";
150             senderContent = "" + (ii+1);
151             MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), "<qos></qos>");
152             msgUnitArr[ii] = msgUnit;
153          }
154 
155 
156          // 1. Query the current memory allocated in xmlBlaster
157          String xmlKey = "<key oid='__cmd:?usedMem' queryType='EXACT'></key>";
158          String qos = "<qos></qos>";
159          MsgUnit[] msgArr = senderConnection.get(xmlKey, qos);
160 
161          assertTrue("returned msgArr == null", null != msgArr);
162          assertEquals("msgArr.length!=1", 1, msgArr.length);
163          assertTrue("returned msgArr[0].msgUnit == null", null != msgArr[0]);
164          assertTrue("returned msgArr[0].msgUnit.content == null", null != msgArr[0].getContent());
165          assertTrue("returned msgArr[0].msgUnit.content.length == 0", 0 != msgArr[0].getContent().length);
166          String mem = new String(msgArr[0].getContent());
167          usedMemBefore = new Long(mem).longValue();
168          log.info("xmlBlaster used allocated memory before publishing = " + Global.byteString(usedMemBefore));
169 
170 
171          stopWatch = new StopWatch();
172          // 2. publish all the messages
173          PublishReturnQos[] publishOidArr = senderConnection.publishArr(msgUnitArr);
174 
175          long avg = 0;
176          double elapsed = stopWatch.elapsed();
177          if (elapsed > 0.)
178             avg = (long)(1000.0 * numPublish / elapsed);
179          log.info("Success: Publishing done, " + numPublish + " messages sent, average messages/second = " + avg);
180 
181          assertTrue("returned publishOidArr == null", null != publishOidArr);
182          assertEquals("numPublished is wrong", numPublish, publishOidArr.length);
183 
184 
185          // 3. Query the memory allocated in xmlBlaster after publishing all the messages
186          msgArr = senderConnection.get(xmlKey, qos);
187          long usedMemAfter = new Long(new String(msgArr[0].getContent())).longValue();
188          log.info("xmlBlaster used allocated memory after publishing = " + Global.byteString(usedMemAfter));
189          log.info("Consumed memory for each message = " + Global.byteString((usedMemAfter-usedMemBefore)/numPublish));
190 
191       } catch(XmlBlasterException e) {
192          log.warning("XmlBlasterException: " + e.getMessage());
193          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
194       } catch(Exception e) {
195          log.warning("Exception: " + e.toString());
196          e.printStackTrace();
197          assertTrue("get or publish - Exception: " + e.toString(), false);
198       }
199    }
200 
201 
202    /**
203     * TEST: Construct 1000 messages and publish it.
204     */
205    public void testManyPublish()
206    {
207       doPublish();
208    }
209 
210 
211    /**
212     * Method is used by TestRunner to load these tests
213     */
214    public static Test suite()
215    {
216        TestSuite suite= new TestSuite();
217        String loginName = "Tim";
218        int numMsg = 100;
219        suite.addTest(new RamTest(new Global(), "testManyPublish", loginName, numMsg));
220        return suite;
221    }
222 
223 
224    /**
225     * Invoke: java org.xmlBlaster.test.stress.RamTest
226     * <br />
227     * You can use the command line option -numPublish 1000 to change the number of messages sent.
228     * <br />
229     * @deprecated Use the TestRunner from the testsuite to run it:<p />
230     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.stress.RamTest</pre>
231     */
232    public static void main(String args[])
233    {
234       Global glob = new Global(args);
235       int numPublish = glob.getProperty().get("numPublish", 1000);
236       RamTest testSub = new RamTest(glob, "RamTest", "Tim", numPublish);
237       testSub.setUp();
238       testSub.testManyPublish();
239       testSub.tearDown();
240    }
241 }


syntax highlighted by Code2HTML, v. 0.9.1