KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > core > MessageLatencyTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.core;
5
6 import com.tc.bytes.TCByteBuffer;
7 import com.tc.bytes.TCByteBufferFactory;
8 import com.tc.net.TCSocketAddress;
9 import com.tc.net.protocol.GenericNetworkMessage;
10 import com.tc.net.protocol.GenericNetworkMessageSink;
11 import com.tc.net.protocol.GenericProtocolAdaptor;
12 import com.tc.util.runtime.Os;
13
14 import java.util.Arrays JavaDoc;
15
16 import junit.framework.TestCase;
17
18 /**
19  * @author teck
20  */

21 public class MessageLatencyTest extends TestCase {
22   private static final int NUM_MSGS = 10000;
23   private static final double PERCENTILE = 95;
24
25   private TCConnectionManager connMgr;
26   private SimpleServer server;
27   private final Object JavaDoc lock = new Object JavaDoc();
28   private final RecordTimeSink serversink = new RecordTimeSink(true);
29   private final RecordTimeSink clientsink = new RecordTimeSink(false);
30
31   protected void setUp() throws Exception JavaDoc {
32     connMgr = new TCConnectionManagerFactory().getInstance();
33     server = new SimpleServer(serversink);
34     server.start();
35   }
36
37   protected void tearDown() throws Exception JavaDoc {
38     connMgr.shutdown();
39     server.stop();
40   }
41
42   public static void main(String JavaDoc args[]) throws Exception JavaDoc {
43     MessageLatencyTest test = new MessageLatencyTest();
44     test.setUp();
45     test.testLatency();
46     test.tearDown();
47   }
48
49   public void testLatency() throws Exception JavaDoc {
50     final TCConnection conn = connMgr.createConnection(new GenericProtocolAdaptor(clientsink));
51     conn.connect(new TCSocketAddress(server.getServerAddr().getPort()), 3000);
52
53     for (int i = 0; i < NUM_MSGS; i++) {
54       TCByteBuffer data = TCByteBufferFactory.getInstance(false, 8);
55
56       final long sent = System.currentTimeMillis();
57       data.putLong(sent);
58       data.position(0);
59
60       final GenericNetworkMessage msg = new GenericNetworkMessage(conn, data);
61
62       synchronized (lock) {
63         conn.putMessage(msg);
64         lock.wait();
65       }
66     }
67
68     Arrays.sort(serversink.recvTimes);
69     Arrays.sort(clientsink.recvTimes);
70
71     final int withinPercentile = (int) (NUM_MSGS * (PERCENTILE / 100));
72
73     long serversum = 0;
74     for (int i = 0; i < withinPercentile; i++) {
75       serversum += serversink.recvTimes[i];
76     }
77
78     long clientsum = 0;
79     for (int i = 0; i < withinPercentile; i++) {
80       clientsum += clientsink.recvTimes[i];
81     }
82
83     // Account for System.currentTimeMillis() resolution suckage on windows
84
final double benchmark = Os.isWindows() ? 2.0 : 1.0;
85     final double serveravg = ((double) serversum / (double) withinPercentile);
86     final double clientavg = ((double) clientsum / (double) withinPercentile);
87
88     System.out.println("Client->Server Average Latency: " + serveravg + " milliseconds");
89     System.out.println("Server->Client Average Latency: " + clientavg + " milliseconds");
90
91     assertTrue("Server Avergage latency is not in acceptable range (" + serveravg + " > " + benchmark + ")",
92                (serveravg <= benchmark));
93     assertTrue("Client Avergage latency is not in acceptable range (" + clientavg + " > " + benchmark + ")",
94                (clientavg <= benchmark));
95   }
96
97   class RecordTimeSink implements GenericNetworkMessageSink {
98     final boolean respond;
99     int ctr = 0;
100     long recvTimes[] = new long[NUM_MSGS];
101
102     RecordTimeSink(boolean respond) {
103       this.respond = respond;
104     }
105
106     public void putMessage(GenericNetworkMessage msg) {
107       long recvTime = System.currentTimeMillis();
108       long sentTime = msg.getPayload()[0].getLong();
109
110       long diff = recvTime - sentTime;
111
112       if ((ctr % 100) == 0) {
113         System.out.println("Latency was " + diff + " milliseconds");
114       }
115
116       recvTimes[ctr++] = diff;
117
118       TCByteBuffer data = TCByteBufferFactory.getInstance(false, 8);
119
120       final long sent = System.currentTimeMillis();
121       data.putLong(sent);
122       data.position(0);
123
124       if (respond) {
125         final GenericNetworkMessage response = new GenericNetworkMessage(msg.getSource(), data);
126         msg.getSource().putMessage(response);
127       } else {
128         synchronized (lock) {
129           lock.notify();
130         }
131       }
132     }
133   }
134
135 }
Popular Tags