1
2
3
4
5
6
7
8
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
30
31
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
79
80 ((FunctionalStreamingTestComponent) ftc).setEventCallback(callback, size);
81
82 Runtime runtime = Runtime.getRuntime();
83 runtime.gc();
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
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
100
101
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 }