我在服务器套接字上有一个java服务器。客户端连接到此服务器套接字,并通过输入/输出对象流进行交换。现在我需要测试应用程序的可伸缩性。这意味着我需要运行相同的测试请求,并测试服务器是否能够处理来自随机客户端的请求。
以下基于junit的测试用例是否是测试随机连接/请求的正确方法。我感觉下面的代码是按顺序测试客户端的。
我读过的一些链接,但对我来说不起作用
Creating a JUnit testsuite with multiple instances of a Parameterized test
public class ScalePostiveTestCases {
SendQueue sendQueue;
Socket clientSocket = null;
public static void main(String[] args) throws Throwable {
testSearching() ;
}
@SuppressWarnings("unchecked")
private static void testSearching() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
TestSuite tests = new TestSuite();
for (int i = 0; i < 99; i++) {
Class<SingleSearchTest> singleSearchTest = (Class<SingleSearchTest>) ClassLoader
.getSystemClassLoader().loadClass(
"SingleSearchTest");
SingleSearchTest singleSearch = singleSearchTest.newInstance();
tests.addTest(singleSearch);
}
TestRunner.run(tests);
}
public class SingleSearchTest extends TestCase {
static SingleSearchTest singleSearch = null;
private String device, connection, user;
private ClientSession clientSession;
private SendQueue sendQueue;
public static SingleSearchTest main(String args[]) {
singleSearch = new SingleSearchTest();
return singleSearch;
}
public SingleSearchTest() {
super("testSingleSearch");
Random rand = new Random();
}
}
答案 0 :(得分:2)
您正在创建许多测试,每个测试都有一个客户端。
您需要做的是进行一次简单测试,创建许多同时执行的客户端。
在我的单元测试中;
答案 1 :(得分:0)
刚想通了我正在使用threadExecutor.isTerminated和threadExecutor.showdown,而不是使用threadExecutor.isShutDown和threadExecutor.shutdownNow。我也尝试过threadExecutor.awaitTermination。工作。基本上两个测试都是并行运行的。
public static void testLoading() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleClientTest clientTest = new SingleClientTest();
randomStringList.add(clientTest.getRandomString());
threadExecutor.execute(clientTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}
public static void testSearching() {
threadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 50; i++) {
SingleSearchTest searchTest = new SingleSearchTest(randomStringList);
threadExecutor.execute(searchTest);
}
threadExecutor.shutdownNow();
try {
threadExecutor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// while (!threadExecutor.isShutdown()) {
// }
}