1   /*
2    * $Id: AbstractStreamingCapacityTestCase.java 11179 2008-03-05 13:46:23Z dfeist $
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.transport.tcp.integration;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleEventContext;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.tck.functional.EventCallback;
18  import org.mule.tck.functional.FunctionalStreamingTestComponent;
19  import org.mule.transport.DefaultMessageAdapter;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
22  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
23  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * IMPORTANT - DO NOT RUN THIS TEST IN AN IDE WITH LOG LEVEL OF DEBUG.  USE INFO TO SEE
30   * DIAGNOSTICS.  OTHERWISE THE CONSOLE OUTPUT WILL BE SIMILAR SIZE TO DATA TRANSFERRED,
31   * CAUSING CONFUSNG AND PROBABLY FATAL MEMORY USE.
32   */
33  public abstract class AbstractStreamingCapacityTestCase extends FunctionalTestCase
34  {
35  
36      public static final long ONE_KB = 1024;
37      public static final long ONE_MB = ONE_KB * ONE_KB;
38      public static final long ONE_GB = ONE_KB * ONE_MB;
39      public static final int MESSAGES = 21;
40  
41      protected final Log logger = LogFactory.getLog(getClass());
42      private long size;
43      private String endpoint;
44  
45      public AbstractStreamingCapacityTestCase(long size, String endpoint)
46      {
47          this.size = size;
48          this.endpoint = endpoint;
49      }
50  
51      public void testSend() throws Exception
52      {
53          final CountDownLatch latch = new CountDownLatch(1);
54          final AtomicReference message = new AtomicReference();
55  
56          EventCallback callback = new EventCallback()
57          {
58              public synchronized void eventReceived(MuleEventContext context, Object component)
59              {
60                  try
61                  {
62                      FunctionalStreamingTestComponent ftc = (FunctionalStreamingTestComponent) component;
63                      message.set(ftc.getSummary());
64                      latch.countDown();
65                  }
66                  catch (Exception e)
67                  {
68                      logger.error(e.getMessage(), e);
69                  }
70              }
71          };
72  
73          MuleClient client = new MuleClient();
74  
75          Object ftc = getComponent("testComponent");
76          assertTrue("FunctionalStreamingTestComponent expected", ftc instanceof FunctionalStreamingTestComponent);
77          assertNotNull(ftc);
78          //assertEquals(1, ftc.getNumber());
79  
80          ((FunctionalStreamingTestComponent) ftc).setEventCallback(callback, size);
81  
82          Runtime runtime = Runtime.getRuntime();
83          runtime.gc(); // i know, i know...
84          long freeStart = runtime.freeMemory();
85          long maxStart = runtime.maxMemory();
86          long timeStart = System.currentTimeMillis();
87  
88          BigInputStream stream = new BigInputStream(size, MESSAGES);
89          DefaultMessageAdapter adapter = new DefaultMessageAdapter(stream);
90          client.dispatch(endpoint, new DefaultMuleMessage(adapter));
91  
92          // if we assume 1MB/sec then we need at least...
93          long pause = Math.max(size / ONE_MB, 60 * 10) + 10;
94          logger.info("Waiting for up to " + pause + " seconds");
95  
96          latch.await(pause, TimeUnit.SECONDS);
97          assertEquals(stream.summary(), message.get());
98  
99          // neither of these memory tests are really reliable, but if we stay with 1.4 i don't
100         // know of anything better.
101         // if these fail in practice i guess we just remove them.
102 
103         long freeEnd = runtime.freeMemory();
104         long delta = freeStart - freeEnd;
105         long timeEnd = System.currentTimeMillis();
106         double speed = size / (double) (timeEnd - timeStart) * 1000 / ONE_MB;
107         logger.info("Transfer speed " + speed + " MB/s (" + size + " B in " + (timeEnd - timeStart) + " ms)");
108         double usePercent = 100.0 * delta / size;
109         logger.info("Memory delta " + delta + " B = " + usePercent + "%");
110         assertTrue("Memory used too high", usePercent < 10);
111 
112         long maxEnd = runtime.maxMemory();
113         assertEquals("Max memory shifted", 0,  maxEnd - maxStart);
114     }
115 
116 }