test.http.HttpTestClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
/*
* Copyright (c) [2021] [Peking University]
* [BDWare DOIP SDK] is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package test.http;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class HttpTestClient {
static Logger LOGGER = LogManager.getLogger(HttpServerTest.class);
static int defaultThreadCount = 4;
static String longStr = HttpServerTest.getLongStr();
String serverAddr = "http://39.104.208.148:21044/";
public static void main(String[] args) throws Exception {
// tcp://39.104.208.148:21042 10 1000 "bdware.test/small"
if (args.length < 4) {
System.out.println("Usage:\n tcp://39.104.208.148:21042 threadCount requestCount bdware.test/small largeorempty");
}
if (args.length >= 5 && args[4].startsWith("large")) {
testClient(args[0], Integer.valueOf(args[1]), Integer.valueOf(args[2]), args[3], HttpServerTest.longStr);
System.out.println("Request: Large, Response:" + args[3]);
} else {
testClient(args[0], Integer.valueOf(args[1]), Integer.valueOf(args[2]), args[3], "");
System.out.println("Request: Small, Response:" + args[3]);
}
}
//=========From Client To Ali============
//[INFO ]17:51:00.700 http://127.0.0.1:21044/ Final Result:10000/10000 dur:2139 rps:4675.08 (HttpServerTest.java:135)
//[INFO ]23:58:36.152 http://39.104.208.148:21044/ Final Result:1000/1000 dur:8964 rps:111.56
//=========From Ali To Ali============
//[INFO ]00:06:06.550 http://39.104.208.148:21043/ Final Result:10000/10000 dur:1373 zdl:7283.32
//[INFO ]00:07:07.120 http://39.104.208.148:21043/ Final Result:10000/10000 dur:1298 zdl:7704.16
public void smallReqSmallResp() throws Exception {
testClient(serverAddr, defaultThreadCount, 1000, "bdware.test/small", "");
}
//[INFO ]17:51:36.493 http://127.0.0.1:21044/ Final Result:1000/1000 dur:21229 rps:47.11 (HttpServerTest.java:135)
//[INFO ]23:59:21.120 http://39.104.208.148:21044/ Final Result:100/100 dur:29175 rps:3.43
//=========From Ali To Ali============
//[INFO ]00:07:54.345 http://39.104.208.148:21043/ Final Result:1000/1000 dur:8412 zdl:118.88
//[INFO ]00:08:17.069 http://39.104.208.148:21043/ Final Result:1000/1000 dur:8438 zdl:118.51
public void smallReqLargeResp() throws Exception {
testClient(serverAddr, defaultThreadCount, 100, "bdware.test/large", "");
}
// [INFO]17:55:02.653 http://127.0.0.1:21044/ Final Result:1000/1000 dur:1401 rps:713.78 (HttpServerTest.java:135)
//[INFO ]00:00:00.780 http://39.104.208.148:21044/ Final Result:100/100 dur:22476 rps:4.45
//=========From Ali To Ali============
//[INFO ]00:08:37.819 http://39.104.208.148:21043/ Final Result:1000/1000 dur:6242 zdl:160.21
//[INFO ]00:09:00.098 http://39.104.208.148:21043/ Final Result:1000/1000 dur:6174 zdl:161.97
// Optimized by Zero-Copy
public void largeReqSmallResp() throws Exception {
testClient(serverAddr, defaultThreadCount, 100, "bdware.test/small", longStr);
}
//[INFO ]17:56:18.769 http://127.0.0.1:21044/ Final Result:1000/1000 dur:20680 rps:48.36 (HttpServerTest.java:136)
//[INFO ]00:01:13.328 http://39.104.208.148:21044/ Final Result:100/100 dur:36070 rps:2.77
//=========From Ali To Ali============
//[INFO ]00:09:29.003 http://39.104.208.148:21043/ Final Result:1000/1000 dur:10122 zdl:98.79
//[INFO ]00:09:47.582 http://39.104.208.148:21043/ Final Result:1000/1000 dur:9664 zdl:103.48
public void largeReqLargeResp() throws Exception {
testClient(serverAddr, defaultThreadCount, 100, "bdware.test/large", longStr);
}
public static void testClient(String urlStr, int threadCount, int totalCount, String doid, String body) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
AtomicInteger total = new AtomicInteger(0);
AtomicInteger correct = new AtomicInteger(0);
long start = System.currentTimeMillis();
for (int i = 0; i < totalCount; i++)
executorService.execute(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.addRequestProperty("doid", doid);
connection.setDoOutput(true);
connection.connect();
OutputStream out = connection.getOutputStream();
out.write(body.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
InputStream input = connection.getInputStream();
Scanner sc = new Scanner(input);
StringBuilder sb = new StringBuilder();
while (sc.hasNextLine()) {
sb.append(sc.nextLine());
}
sc.close();
String content = sb.toString();
if (content.startsWith("cold")) {
correct.incrementAndGet();
}
input.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
total.incrementAndGet();
}
}
});
int circle = 0;
for (; total.get() < totalCount; ) {
if (++circle % 100 == 0)
LOGGER.info(String.format("%d/%d", correct.get(), total.get()));
Thread.sleep(10);
}
int dur = (int) (System.currentTimeMillis() - start);
LOGGER.info(String.format("%s Final Result:%d/%d dur:%d rps:%.2f ", urlStr, correct.get(), total.get(),
dur, (correct.get() + 0.0D) * 1000.0D / (dur)));
}
}