summaryrefslogtreecommitdiffstats
path: root/simple/simple-transport/src/test/java/org/simpleframework/transport/TransportTest.java
blob: 1a14431708f75cf89c03ccf10f1642d6e9dc355d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package org.simpleframework.transport;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

import junit.framework.TestCase;

import org.simpleframework.transport.reactor.ExecutorReactor;
import org.simpleframework.transport.reactor.Reactor;

/**
 * Measure the performance of the transports to ensure that the perform 
 * well and that they send the correct sequence of bytes and that the
 * blocks sent are in the correct order. This also performs a comparison
 * with direct socket output streams to ensure there is a reasonable
 * performance difference.  
 * 
 * @author Niall Gallagher 
 */
public class TransportTest extends TestCase {

   private static final int REPEAT = 1000;

   public void testTransport() throws Exception {
      testTransport(REPEAT);
   }

   public void testTransport(int repeat) throws Exception {    
      for(int i = 1; i < 7; i++) { // just do some random sizes 
         testTransport(i, 100);
      }
      for(int i = 4092; i < 4102; i++) {
         testTransport(i, 100);
      }
      for(int i = 8190; i < 8200; i++) {
         testTransport(i, 100);
      }
      for(int i = 11282; i < 11284; i++) {
         testTransport(i, 1000);
      }
      for(int i = 204800; i < 204805; i++) {
         testTransport(i, 1000);
      }
      testTransport(16, repeat);
      testTransport(64, repeat);
      testTransport(256, repeat);
      testTransport(1024, repeat);
      testTransport(2048, repeat);
      testTransport(4096, repeat); 
      testTransport(4098, repeat);
      testTransport(8192, repeat);
      testTransport(8197, repeat);
   }

   // Test blocking transport        
   private void testTransport(int size, int repeat) throws Exception {
     // ThreadDumper dumper = new ThreadDumper();
      SocketConsumer consumer = new SocketConsumer(size, repeat);
      SocketAddress address = new InetSocketAddress("localhost", consumer.getPort());
      SocketChannel channel = SocketChannel.open();
      channel.configureBlocking(false); // underlying socket must be non-blocking
      channel.connect(address);

      while(!channel.finishConnect()) { // wait to finish connection
         Thread.sleep(10);
      };
      ExecutorService executor = Executors.newFixedThreadPool(20);
      Reactor reactor = new ExecutorReactor(executor);
     // Transport transport = new SocketTransport(channel, reactor, 2, 3);//XXX bug
      MockSocket pipeline = new MockSocket(channel);
      Transport transport = new SocketTransport(pipeline, reactor, 8192);
      OutputStream out = new TransportOutputStream(transport);

     // dumper.start();
      testOutputStream(consumer, out, size, repeat);

      out.close();
      executor.shutdown();
      channel.close();
      reactor.stop();
    //  dumper.kill();
      Thread.sleep(100);
   }

   public void s_testSocket() throws Exception {
      s_testSocket(REPEAT);
   }

   public void s_testSocket(int repeat) throws Exception {
      testSocket(16, repeat);
      testSocket(64, repeat);
      testSocket(256, repeat);
      testSocket(1024, repeat);
      testSocket(2048, repeat);
      testSocket(4098, repeat);
      testSocket(8192, repeat);
   }

   // Test blocking socket
   private void testSocket(int size, int repeat) throws Exception {
     // ThreadDumper dumper = new ThreadDumper();
      SocketConsumer consumer = new SocketConsumer(size, repeat);
      Socket socket = new Socket("localhost", consumer.getPort());
      OutputStream out = socket.getOutputStream();

      //dumper.start();
      testOutputStream(consumer, out, size, repeat);

      out.close();
      socket.close();
      //dumper.kill();
      Thread.sleep(100);
   }

   private class AlpahbetIterator {

      private byte[] alphabet = "abcdefghijklmnopqstuvwxyz".getBytes();

      private int off;

      public byte next() {
         if(off == alphabet.length) {
            off = 0;
         }
         return alphabet[off++];
      }

      public void reset() {
         off = 0;
      }
   }

   private void testOutputStream(SocketConsumer consumer, OutputStream out, int size, int repeat) throws Exception {
      byte[] block = new byte[size]; // write size
      AlpahbetIterator it = new AlpahbetIterator(); // write known data   

      for(int i = 1; i < block.length; i++) {
         block[i] = it.next();
      }
      AtomicLong count = new AtomicLong();
      PerformanceMonitor monitor = new PerformanceMonitor(consumer, count, out.getClass().getSimpleName(), size);

      for(int i = 0; i < repeat; i++) {
         block[0] = (byte) i; // mark the first byte in the block to be sure we get blocks in sequence
         //System.err.println("["+i+"]"+new String(block,"ISO-8859-1"));
         out.write(block);  // manipulation of the underlying buffer is taking place when the compact is invoked, this is causing major problems as the next packet will be out of sequence
         count.addAndGet(block.length);
      }
      Thread.sleep(2000); // wait for all bytes to flush through to consumer
      monitor.kill();
   }

   private class PerformanceMonitor extends Thread {
      private AtomicLong count;

      private volatile boolean dead;
      
      private SocketConsumer consumer;

      private String name;

      private int size;

      public PerformanceMonitor(SocketConsumer consumer, AtomicLong count, String name, int size) {
         this.consumer = consumer;
         this.count = count;
         this.name = name;
         this.size = size;
         this.start();
      }

      public void run() {
         int second = 0;
         while(!dead) {
            try {
               long octets = count.longValue();
               System.out.printf("%s,%s,%s,%s,%s%n", name, size, second++, octets, consumer.getWindow());
               Thread.sleep(1000);
            } catch(Exception e) {
               e.printStackTrace();
            }
         }
      }

      public void kill() throws Exception {
         dead = true;
      }
   }

   private class SocketConsumer extends Thread {

      private ServerSocket server;

      private Window window;
      
      private long repeat;

      private long size;

      public SocketConsumer(int size, int repeat) throws Exception {
         this.window = new Window(20);
         this.server = getSocket();
         this.repeat = repeat;
         this.size = size;
         this.start();
      }
      
      public int getPort() {
         return server.getLocalPort();
      }
      
      public String getWindow() {
         return window.toString();
      }
      
      private ServerSocket getSocket() throws Exception {
         // Scan the ephemeral port range
         for(int i = 2000; i < 10000; i++) { // keep trying to grab the socket 
            try {
               ServerSocket socket = new ServerSocket(i);
               System.out.println("port=["+socket.getLocalPort()+"]");
               return socket;
            } catch(Exception e) {
               Thread.sleep(200);
            }
         }
         // Scan a second time for good measure, maybe something got freed up
         for(int i = 2000; i < 10000; i++) { // keep trying to grab the socket 
            try {
               ServerSocket socket = new ServerSocket(i);
               System.out.println("port=["+socket.getLocalPort()+"]");
               return socket;
            } catch(Exception e) {
               Thread.sleep(200);
            }
         }
         throw new IOException("Could not create a client socket");
      }

      public void run() {
         long count = 0;
         int windowOctet = 0;
         int expectWindowOctet = 0;

         try {
            Socket socket = server.accept();
            InputStream in = socket.getInputStream();
            InputStream source = new BufferedInputStream(in);
            AlpahbetIterator it = new AlpahbetIterator();

            scan: for(int i = 0; i < repeat; i++) {
               int octet = source.read(); // check first byte in the block to make sure its correct in sequence
               
               if(octet == -1) {
                  break scan;
               }
               count++; // we have read another byte
               windowOctet = octet & 0x000000ff;
               expectWindowOctet = i & 0x000000ff;
               
               if((byte) octet != (byte) i) {
                  throw new Exception("Wrong sequence of blocks sent, was "
                        + (byte)octet + " should have been " + (byte)i + " count is "+count+" window is "+window+" compare "+explore(it, source, 5));
               }
               window.recieved(octet);
               
               for(int j = 1, k = 0; j < size; j++, k++) {
                  octet = source.read();

                  if(octet == -1) {
                     break scan;
                  }
                  byte next = it.next();
                   
                  if((byte) octet != next) {
                     throw new Exception("Invalid data received expected "+((byte)octet)+"("+((char)octet)+
                           ") but was "+next+"("+((char)next)+") total count is "+count+" block count is "+k+" window is expected "+
                           expectWindowOctet+"("+((char)expectWindowOctet)+")("+((byte)expectWindowOctet)+") got "+windowOctet+"("+
                           ((char)windowOctet)+")("+((byte)windowOctet)+") "+window+" compare "+explore(it, source, 5));
                  }
                  count++;
               }
               it.reset();
            }
         } catch(Throwable e) {
            e.printStackTrace();
         }
         if(count != size * repeat) {
            new Exception("Invalid number of bytes read, was " + count
                  + " should have been " + (size * repeat)).printStackTrace();
         }      
         try {
           // server.close();         
         }catch(Exception e) {
            e.printStackTrace();
         }      
      }
      
      private String explore(AlpahbetIterator it, InputStream source, int count) throws IOException {
         StringBuffer buf = new StringBuffer();
         buf.append("expected (");
         for(int i = 0; i < count; i++) {
            buf.append( (char)it.next() );
         }
         buf.append(") is (");
         for(int i = 0; i < count; i++) {
            buf.append(  (char)source.read()  );
         }
         buf.append(")");
         return buf.toString();
      }
   }
   

   private static class TransportOutputStream extends OutputStream {

      private Transport transport;

      public TransportOutputStream(Transport transport) {
         this.transport = transport;
      }

      public void write(int octet) throws IOException {
         byte[] data = new byte[] { (byte) octet };
         write(data);
      }

      public void write(byte[] data, int off, int len) throws IOException {
         try {
            ByteBuffer buffer = ByteBuffer.wrap(data, off, len);
            ByteBuffer safe = buffer.asReadOnlyBuffer();
            
            if(len > 0) {
               transport.write(safe);
            }
         } catch(Exception e) {
            e.printStackTrace();
            throw new IOException("Write failed");
         }
      }

      public void flush() throws IOException {
         try {
            transport.flush();
         } catch(Exception e) {
            e.printStackTrace();
            throw new IOException("Flush failed");
         }
      }

      public void close() throws IOException {
         try {
            transport.close();
         } catch(Exception e) {
            e.printStackTrace();
            throw new IOException("Close failed");
         }
      }

   }
   
   private static class Window {
      
      private final LinkedList<String> window;
      private final int size;
      
      public Window(int size) {
         this.window = new LinkedList<String>();
         this.size = size;
      }
      
      public synchronized void recieved(int sequence) {
         window.addLast(String.valueOf(sequence));
         
         if(window.size() > size) {
            window.removeFirst();
         }
      }
      
      public synchronized String toString() {
         StringBuilder builder = new StringBuilder("[");
         String delim = "";
         for(String b : window) {
            builder.append(delim).append(b);
            delim=", ";
         }
         builder.append("]");
         return builder.toString();
      }
   }

}