JUnit负载测试数千个与服务器的并发套接字连接

时间:2013-10-19 13:25:49

标签: java sockets junit asyncsocket

我有大约100个JUnit测试,模拟客户端与服务器的套接字连接。他们看起来像这样:

@Test
public void testProtocolInACertainWay() throws Exception {
    Socket socket = _socketFactory.createSocket(_host, _port);  // SSLSocketFactory

    // Send payload
    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
    outputStream.write(/* test-specific payload */);
    outputStream.flush();

    // Receive response
    DataInputStream inputStream = new DataInputStream(socket.getInputStream());        
    socket.setSoTimeout(5000);
    byte[] buffer = new byte[512];
    int numBytesRead = inputStream.read(buffer);
    buffer = ArrayUtils.subarray(buffer, 0, numBytesRead);

    // Assert test-specific stuff on response
    Assert.assertTrue(buffer[0] == (byte)1);  // for example

    /* At this point, depending on the test, we either repeat similar steps with different payloads or end the test */
}

现在,我希望能够从服务器运行这些测试(或子集),一次150万。这意味着我想同时发送1.5M套接字写入,全部读取它们并在它们的响应上断言。

有没有办法可以做到这一点,而无需重写所有100个JUnit测试? (请说是,SO:))

谢谢!

1 个答案:

答案 0 :(得分:0)

经过大量研究,结果我真正想做的是使用Netty或vert.x.我将不得不重写所有测试以使用事件队列而不是阻止I / O.