我有大约 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 测试? (请说是,所以:))
谢谢!
最佳答案
经过大量研究,我真正想做的是使用 Netty 或 vert.x。我必须重写所有测试才能使用事件队列而不是阻塞 I/O。
关于java - JUnit 负载测试数千个到服务器的并发套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466513/