summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/socket/service/WebSocketPerformanceTest.java
blob: fbabeeb80b324b23fc915e443c4acb0bdc3e6c87 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package org.simpleframework.http.socket.service;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.SelectableChannel;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.simpleframework.common.buffer.Allocator;
import org.simpleframework.common.buffer.ArrayAllocator;
import org.simpleframework.common.thread.Daemon;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerTransportProcessor;
import org.simpleframework.http.core.StreamCursor;
import org.simpleframework.http.core.ThreadDumper;
import org.simpleframework.http.message.ReplyConsumer;
import org.simpleframework.http.socket.DataFrame;
import org.simpleframework.http.socket.Frame;
import org.simpleframework.http.socket.FrameListener;
import org.simpleframework.http.socket.FrameType;
import org.simpleframework.http.socket.Reason;
import org.simpleframework.http.socket.Session;
import org.simpleframework.http.socket.FrameChannel;
import org.simpleframework.http.socket.WebSocketAnalyzer;
import org.simpleframework.transport.TransportProcessor;
import org.simpleframework.transport.TransportSocketProcessor;
import org.simpleframework.transport.SocketProcessor;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
import org.simpleframework.transport.trace.TraceAnalyzer;
import org.simpleframework.transport.trace.Trace;

public class WebSocketPerformanceTest {

   public static class MessageCounter implements FrameListener {
      
      private final AtomicInteger counter;
      
      public MessageCounter(AtomicInteger counter) {
         this.counter = counter;
      }

      public void onFrame(Session socket, Frame frame) {
         counter.getAndIncrement();
      }

      public void onError(Session socket, Exception cause) {
         System.err.println("onError(");
         cause.printStackTrace();
         System.err.println(")");
      }

      public void onOpen(Session socket) {
         System.err.println("onOpen(" + socket +")");
      }

      public void onClose(Session session, Reason reason) {
         System.err.println("onClose(" + reason +")");
      }
   }
   
   public static class MessageGeneratorService extends Thread implements Service {     
     
      private static final String MESSAGE = 
      "{'product': {\r\n"+
      "  'id': '1234',\r\n"+
      "  'name': 'AU3TB00001256',\r\n"+
      "  'values': {\r\n"+
      "    'best': [\r\n"+
      "      {'bid': '13.344'},\r\n"+
      "      {'offer': '12.1'},\r\n"+
      "      {'volume': '100000'}\r\n"+
      "    ]\r\n"+
      "  }\r\n"+
      "}}";
      
      private final Set<FrameChannel> sockets;
      private final MessageCounter listener;
      private final AtomicInteger counter;
      private final AtomicBoolean begin;

      public MessageGeneratorService() {
         this.sockets = new CopyOnWriteArraySet<FrameChannel>();
         this.counter = new AtomicInteger();
         this.listener = new MessageCounter(counter);
         this.begin = new AtomicBoolean();
      }  
      
      public void begin() {
         if(begin.compareAndSet(false, true)) {
            start();
         }
      }
     
      public void connect(Session connection) {
         FrameChannel socket = connection.getChannel();   
         
         try {
            sockets.add(socket);
            socket.register(listener);
         } catch(Exception e) {
            e.printStackTrace();
         }         
      }

      public void distribute(Frame frame) {
         try {         
            for(FrameChannel socket : sockets) {
               try {                  
                  socket.send(frame);
               } catch(Exception e){
                  e.printStackTrace();
                  sockets.remove(socket);
                  socket.close();
               }
            }
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
      
      public void run() {
         try {
            for(int i = 0; i < 10000000; i++) {
               distribute(new DataFrame(FrameType.TEXT, System.currentTimeMillis() + ":" + MESSAGE));
            }
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
   
   public static class MessageGeneratorContainer implements Container {   

      private final RouterContainer container;
      private final SocketAddress address;
      private final Connection connection;
      private final Allocator allocator;
      private final TransportProcessor processor;
      private final Router negotiator;
      private final SocketProcessor server;
      
      public MessageGeneratorContainer(MessageGeneratorService service, TraceAnalyzer agent, int port) throws Exception {
         this.negotiator = new DirectRouter(service);
         this.container = new RouterContainer(this, negotiator, 10, 100000);
         this.allocator = new ArrayAllocator();
         this.processor = new ContainerTransportProcessor(container, allocator, 10);
         this.server = new TransportSocketProcessor(processor, 10, 8192*10);
         this.connection = new SocketConnection(server, agent);
         this.address = new InetSocketAddress(port);
      }
      
      public void connect() throws Exception {    
         connection.connect(address);
      }
      
      public void handle(Request req, Response resp) {
         long time = System.currentTimeMillis();
         
         System.err.println(req);
            
         try {
            PrintStream out = resp.getPrintStream();
            
            resp.setDate("Date", time);
            resp.setValue("Server", "MessageGeneratorContainer/1.0");
            resp.setContentType("text/plain");            
            resp.setDate("Date", time);
            resp.setValue("Server", "MessageGeneratorContainer/1.0");
            resp.setContentType("text/html");            
    
            out.println("Your request is invalid as this is a websocket test!!");
            out.close();
         }catch(Exception e) {
            e.printStackTrace();
         }
      }
   }  
   
   public static class MessageGeneratorClient extends Thread {
      
      private final MessageGeneratorService service;
      private final AtomicInteger counter;
      private final AtomicLong duration;
      private final int port;
      private final boolean debug;
      
      public MessageGeneratorClient(MessageGeneratorService service, AtomicInteger counter, AtomicLong duration, int port, boolean debug) {
         this.duration = duration;
         this.counter = counter;
         this.service = service;
         this.debug = debug;
         this.port = port;
      }
      
      public void run() {
         try {
            Socket socket = new Socket("localhost", port);
            StreamCursor cursor = new StreamCursor(socket.getInputStream());
            FrameConsumer consumer = new FrameConsumer();
            ReplyConsumer response = new ReplyConsumer();
            
            byte[] request = ("GET /chat HTTP/1.1\r\n"+
               "Host: server.example.com\r\n"+
               "Upgrade: websocket\r\n"+
               "Connection: Upgrade\r\n"+
               "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"+
               "Sec-WebSocket-Protocol: chat, superchat\r\n"+
               "Sec-WebSocket-Version: 13\r\n"+
               "Origin: http://example.com\r\n" +
               "\r\n").getBytes("ISO-8859-1");            
            
            socket.getOutputStream().write(request);
            
            while(cursor.isOpen()) {
               response.consume(cursor);
               
               if(response.isFinished()) {
                  System.err.println(response);
                  break;
               }
            }
            service.begin();            
            
            while(cursor.isOpen()) {
               consumer.consume(cursor);
               
               if(consumer.isFinished()) {
                  Frame frame = consumer.getFrame();
                  
                  if(frame != null) {
                     validate(frame);
                  }
                  consumer.clear();
               }
               
               if(!cursor.isReady()) { // wait for it to fill                  
                  Thread.sleep(1);
               }               
            }
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
      
      public void validate(Frame frame) throws Exception {
         FrameType type = frame.getType();
         
         if(type == FrameType.TEXT) {
            String text = frame.getText();
            int index = text.indexOf(':');
            String time = text.substring(0, index);            
            long sendTime = Long.parseLong(time);
            long timeElapsed = System.currentTimeMillis() - sendTime;
         
            duration.getAndAdd(timeElapsed);
            counter.getAndIncrement();

            if(debug) {                                    
               System.err.println("count=" + counter + " text="+text + " time="+duration);            
            }            
         }
      }
   }
   
   public static class MessageTimer extends Thread {
      
      private final AtomicLong duration;
      private final AtomicInteger counter;
      
      public MessageTimer(AtomicInteger counter, AtomicLong duration) {
         this.duration = duration;
         this.counter = counter;
      }
      
      public void run() {
         while(true) {
            try {
               Thread.sleep(1000);
               int count = counter.getAndSet(0);
               long total = duration.getAndSet(0);              
               long average = (total > 0 ? total : 1) / (count > 0 ? count : 1);               
               
               System.err.println("framesPerSecond="+count+" millisPerFrame="+average);
            } catch(Exception e) {
               e.printStackTrace();
            }
         }
      }
   }

   public static class ConsoleAnalyzer extends Daemon implements TraceAnalyzer {
      
      private final Queue<TraceRecord> queue;
      private final AtomicLong count;
      private final String filter;
      
      public ConsoleAnalyzer() {
         this(null);
      }
      
      public ConsoleAnalyzer(String filter) {
         this.queue = new ConcurrentLinkedQueue<TraceRecord>();
         this.count = new AtomicLong();
         this.filter = filter;
      } 
      
      public Trace attach(SelectableChannel channel) {     
         return new TraceFeeder(channel);
      }
      
      public void run() {
         try {
            while(isActive()) {
               Thread.sleep(1000);
               
               while(!queue.isEmpty()) {
                  TraceRecord record = queue.poll();
                  
                  if(filter != null) {
                     Object event = record.event;
                     Class type = event.getClass();
                     String name = type.getName();
                     
                     if(name.contains(filter)) {
                        System.err.println(record);
                     }
                  } else {               
                     System.err.println(record);
                  }
               }        
            }
         } catch(Exception e) {
            e.printStackTrace();
         }
         
      }
      
      private class TraceFeeder implements Trace {
         
         private final SelectableChannel channel;
         private final long sequence;
         
         public TraceFeeder(SelectableChannel channel) {
            this.sequence = count.getAndIncrement();
            this.channel = channel;
         }
         
         public void trace(Object event) {
            trace(event, null);
         }

         public void trace(Object event, Object value) {
            TraceRecord record = new TraceRecord(channel, event, value, sequence);
            
            if(isActive()) {
               queue.offer(record);
            }
         }
         
      }
      
      private class TraceRecord {
         
         private final SelectableChannel channel;
         private final String thread;
         private final Object event;
         private final Object value;
         private final long sequence;
         
         public TraceRecord(SelectableChannel channel, Object event, Object value, long sequence) {
            this.thread = Thread.currentThread().getName();
            this.sequence = sequence;
            this.channel = channel;
            this.event = event;
            this.value = value;
         }
         
         public String toString() {
            StringWriter builder = new StringWriter();
            PrintWriter writer = new PrintWriter(builder);
            
            writer.print(sequence);         
            writer.print(" ["); 
            writer.print(channel);
            writer.print("]");
            writer.print(" ");
            writer.print(thread);
            writer.print(": ");
            writer.print(event);
            
            if(value != null) {
               if(value instanceof Throwable) {
                  ((Throwable)value).printStackTrace(writer);
               } else {
                  writer.print(" -> ");
                  writer.print(value);
               }
            }
            writer.close();
            return builder.toString();
         }
      }

   }
   
   
   public static void main(String[] list) throws Exception {
      ThreadDumper dumper = new ThreadDumper();
      ConsoleAnalyzer agent = new ConsoleAnalyzer();
      AtomicLong duration = new AtomicLong();
      AtomicInteger counter = new AtomicInteger();
      MessageGeneratorService service = new MessageGeneratorService();
      MessageGeneratorContainer container = new MessageGeneratorContainer(service, agent, 7070);
      MessageTimer timer = new MessageTimer(counter, duration);
      
      //agent.start();
      dumper.start();
      timer.start();
      container.connect();
      
      for(int i = 0; i < 100; i++) {
         MessageGeneratorClient client = new MessageGeneratorClient(service, counter, duration, 7070, false);
         client.start();
      }
   }
}