All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.websocket.TestClient Maven / Gradle / Ivy

There is a newer version: 11.0.0.beta1
Show newest version
package org.eclipse.jetty.websocket;

import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;

/**
 * @version $Revision$ $Date$
 * 
 * This is not a general purpose websocket client.
 * It's only for testing the websocket server and is hardwired to a specific draft version of the protocol.
 */
public class TestClient implements WebSocket.OnFrame
{
    private static WebSocketClientFactory __clientFactory = new WebSocketClientFactory();
    private static boolean _verbose=false;

    private static final Random __random = new Random();
    
    private final String _host;
    private final int _port;
    private final String _protocol;
    private final int _timeout;
    
    private static boolean __quiet;
    private static int __framesSent;
    private static int __messagesSent;
    private static AtomicInteger __framesReceived=new AtomicInteger();
    private static AtomicInteger __messagesReceived=new AtomicInteger();
    
    private static AtomicLong __totalTime=new AtomicLong();
    private static AtomicLong __minDuration=new AtomicLong(Long.MAX_VALUE);
    private static AtomicLong __maxDuration=new AtomicLong(Long.MIN_VALUE);
    private static long __start;
    private BlockingQueue _starts = new LinkedBlockingQueue();
    int _messageBytes;
    int _frames;
    byte _opcode=-1;
    private volatile WebSocket.FrameConnection _connection;
    private final CountDownLatch _handshook = new CountDownLatch(1);
    
    
    public void onOpen(Connection connection)
    {
    }

    public void onClose(int closeCode, String message)
    {
        _handshook.countDown();
    }

    public boolean onFrame(byte flags, byte opcode, byte[] data, int offset, int length)
    {
        try
        {
            if (_connection.isClose(opcode))
                return false;
            
            __framesReceived.incrementAndGet();
            _frames++;
            _messageBytes+=length;
            
            if (_opcode==-1)
                _opcode=opcode;
            
            if (_connection.isControl(opcode) || _connection.isMessageComplete(flags))
            {  
                int recv =__messagesReceived.incrementAndGet();
                Long start=_starts.poll();

                if (start!=null)
                {
                    long duration = System.nanoTime()-start.longValue();
                    long max=__maxDuration.get();
                    while(duration>max && !__maxDuration.compareAndSet(max,duration))
                        max=__maxDuration.get();
                    long min=__minDuration.get();
                    while(duration0&& len>fragment)
            len=fragment;
        __messagesSent++;
        while(offlen)
                len=data.length-off;
            if (fragment>0&& len>fragment)
                len=fragment;
        }
    }

    public void disconnect() throws Exception
    {
        if (_connection!=null)
            _connection.disconnect();
    }
    

    private static void usage(String[] args)
    {
        System.err.println("ERROR: "+Arrays.asList(args));
        System.err.println("USAGE: java -cp CLASSPATH "+TestClient.class+" [ OPTIONS ]");
        System.err.println("  -h|--host HOST  (default localhost)");
        System.err.println("  -p|--port PORT  (default 8080)");
        System.err.println("  -b|--binary");
        System.err.println("  -v|--verbose");
        System.err.println("  -c|--count n    (default 10)");
        System.err.println("  -s|--size n     (default 64)");
        System.err.println("  -f|--fragment n (default 4000) ");
        System.err.println("  -P|--protocol echo|echo-assemble|echo-fragment|echo-broadcast");
        System.err.println("  -C|--clients n  (default 1) ");
        System.err.println("  -d|--delay n    (default 1000ms) ");
        System.exit(1);
    }
    
    public static void main(String[] args) throws Exception
    {
        __clientFactory.start();

        String host="localhost";
        int port=8080;
        String protocol=null;
        int count=10;
        int size=64;
        int fragment=4000;
        boolean binary=false;
        int clients=1;
        int delay=1000;

        for (int i=0;i1?"s":"")+" ---");
            System.out.println(__framesSent+" frames transmitted, "+__framesReceived+" received, "+
                    __messagesSent+" messages transmitted, "+__messagesReceived+" received, "+
                    "time "+duration+"ms "+ (1000L*__messagesReceived.get()/duration)+" req/s");
            System.out.printf("rtt min/ave/max = %.3f/%.3f/%.3f ms\n",__minDuration.get()/1000000.0,__messagesReceived.get()==0?0.0:(__totalTime.get()/__messagesReceived.get()/1000000.0),__maxDuration.get()/1000000.0);
            
            __clientFactory.stop();
        }

    }
    
    
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy