1   /*
2    * $Id: AbstractStreamingCapacityTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.providers.tcp.integration;
12  
13  import org.mule.MuleManager;
14  import org.mule.extras.client.MuleClient;
15  import org.mule.providers.streaming.StreamMessageAdapter;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalStreamingTestComponent;
19  import org.mule.umo.UMOEventContext;
20  import org.mule.umo.model.UMOModel;
21  import org.mule.umo.provider.UMOStreamMessageAdapter;
22  
23  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
24  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
25  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * IMPORTANT - DO NOT RUN THIS TEST IN AN IDE WITH LOG LEVEL OF DEBUG.  USE INFO TO SEE
31   * DIAGNOSTICS.  OTHERWISE THE CONSOLE OUTPUT WILL BE SIMILAR SIZE TO DATA TRANSFERRED,
32   * CAUSING CONFUSNG AND PROBABLY FATAL MEMORY USE.
33   */
34  public abstract class AbstractStreamingCapacityTestCase extends FunctionalTestCase
35  {
36  
37      public static final long ONE_KB = 1024;
38      public static final long ONE_MB = ONE_KB * ONE_KB;
39      public static final long ONE_GB = ONE_KB * ONE_MB;
40      public static final int MESSAGES = 21;
41  
42      protected final Log logger = LogFactory.getLog(getClass());
43      private long size;
44      private String endpoint;
45  
46      public AbstractStreamingCapacityTestCase(long size, String endpoint)
47      {
48          this.size = size;
49          this.endpoint = endpoint;
50          setDisposeManagerPerSuite(true);
51      }
52  
53      public void testSend() throws Exception
54      {
55          final CountDownLatch latch = new CountDownLatch(1);
56          final AtomicReference message = new AtomicReference();
57  
58          EventCallback callback = new EventCallback()
59          {
60              public synchronized void eventReceived(UMOEventContext context, Object component)
61              {
62                  try
63                  {
64                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
65                      message.set(ftc.getSummary());
66                      latch.countDown();
67                  }
68                  catch (Exception e)
69                  {
70                      logger.error(e.getMessage(), e);
71                  }
72              }
73          };
74  
75          MuleClient client = new MuleClient();
76  
77          UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("echo");
78          FunctionalStreamingTestComponent ftc =
79                  (FunctionalStreamingTestComponent) model.getComponent("testComponent").getInstance();
80  //        assertNotNull(ftc);
81  //        assertEquals(1, ftc.getNumber());
82  
83          ftc.setEventCallback(callback, size);
84  
85          Runtime runtime = Runtime.getRuntime();
86          runtime.gc(); // i know, i know...
87          long freeStart = runtime.freeMemory();
88          long maxStart = runtime.maxMemory();
89          long timeStart = System.currentTimeMillis();
90  
91          BigInputStream stream = new BigInputStream(size, MESSAGES);
92          UMOStreamMessageAdapter adapter = new StreamMessageAdapter(stream);
93          client.dispatchStream(endpoint, adapter);
94  
95          // if we assume 1MB/sec then we need at least...
96          int pause = (int) Math.max(size / ONE_MB, 10);
97          logger.info("Waiting for up to " + pause + " seconds");
98  
99          latch.await(pause, TimeUnit.SECONDS);
100         assertEquals(stream.summary(), message.get());
101 
102         // neither of these memory tests are really reliable, but if we stay with 1.4 i don't
103         // know of anything better.
104         // if these fail in practice i guess we just remove them.
105 
106         long freeEnd = runtime.freeMemory();
107         long delta = freeStart - freeEnd;
108         long timeEnd = System.currentTimeMillis();
109         double speed = size / (double) (timeEnd - timeStart) * 1000 / ONE_MB;
110         logger.info("Transfer speed " + speed + " MB/s (" + size + " B in " + (timeEnd - timeStart) + " ms)");
111         double usePercent = 100.0 * delta / ((double) size);
112         logger.info("Memory delta " + delta + " B = " + usePercent + "%");
113         assertTrue("Memory used too high", usePercent < 10);
114 
115         long maxEnd = runtime.maxMemory();
116         assertEquals("Max memory shifted", 0,  maxEnd - maxStart);
117     }
118 
119 }