Netty 3.8.0上的负载测试期间的数据损坏

时间:2014-04-25 17:37:30

标签: netty

有没有类似的经历? 简而言之,我创建了一个TCPServer,TCPClient,编码器和解码器。

在我的编码器和解码器上,我使用签名字节来检测任何数据损坏。 如果我运行单个TCPClient线程。它始终有效。但是当它增加到4个线程时,我开始出现随机错误。

TestClient的示例代码。

公共类TestClient {     private static Log logger = LogFactory.getLog(TestClient.class);

public static TCPClient createTCPClient(int times, String host, int port) {
    TCPClient client =  TCPClient.start(host, port);
    if (client.getFuture().awaitUninterruptibly(5000)) logger.info("wait ok");
    if ( client.getFuture().getChannel().isConnected() ) logger.info("connected");
    return client;

}

public static String createString(int size) {
    StringBuilder sb = new StringBuilder();
    for ( int i = 0 ; i < size ; i ++)
        sb.append("String createString, client send "+i);
    return sb.toString();
}

static class ClientThread implements Runnable {
    TCPClient client;
    int times;

    ClientThread(TCPClient client, int times) {
        this.client = client;
        this.times = times;
    }

    @Override
    public void run() {
        int error = 0;
        for ( int i = 0; i < times ; i ++) {
            logger.info("send "+i);
            try {
                Request request = new Request(new Header("test",i, (byte) 0), createString(i).getBytes());

                Response response = client.sendRequest( request);
                if ( response.isError() ) error ++;
                logger.info(response.toString());
            } catch (Throwable th) {
                error++;
                logger.error( th.getMessage());

            }
        }
        logger.info("ERROR # "+ error);
    }



}



public static void main(String[] args) throws Exception {
    String[] opts = new String[] {"-thread","-times","-host","-port"};
    String[] defaults = new String[] {"2", "10", "localhost", "7120" };
    String[] paras = getOpts( args, opts, defaults);
    int threads = Integer.valueOf(paras[0]);
    int times  = Integer.valueOf( paras[1]);
    String host = paras[2];
    int port = Integer.valueOf( paras[3]);
    for ( int i=0; i < threads; i++ ) {
        logger.info("start thread # "+i);
        TCPClient client = createTCPClient(times, host, port);
        new Thread( new ClientThread(client, times)).start();
    }

}

}

1 个答案:

答案 0 :(得分:0)

我最好的选择是你的Channel Downstream Handler或ChannelUp streamHandler实现中存在竞争条件。