|
@@ -8,13 +8,17 @@ import de.siegmar.fastcsv.reader.CsvRow;
|
|
|
import de.siegmar.fastcsv.writer.CsvWriter;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.ConnectionPool;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
import org.springframework.boot.test.context.TestConfiguration;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
-import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
+import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
|
|
+import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
@@ -28,6 +32,9 @@ import java.nio.file.Paths;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* 并发测试
|
|
@@ -44,29 +51,28 @@ class ConcurrentTest {
|
|
|
/**
|
|
|
* 手机号文件
|
|
|
*/
|
|
|
- public static final String PHONES_FILE = "test/1000.txt";
|
|
|
+ public static final String PHONES_FILE = "test/10000.txt";
|
|
|
+
|
|
|
@Autowired
|
|
|
- private RestTemplate restTemplate;
|
|
|
+ private RestTemplate template;
|
|
|
@Autowired
|
|
|
- private ThreadPoolTaskScheduler threadPoolTaskScheduler;
|
|
|
+ private ThreadPoolExecutor executor;
|
|
|
|
|
|
@TestConfiguration
|
|
|
static class ConcurrentTestConfig {
|
|
|
@Bean
|
|
|
- public RestTemplate restTemplate() {
|
|
|
- return new RestTemplate();
|
|
|
+ public RestTemplate template() {
|
|
|
+ return new RestTemplate(new OkHttp3ClientHttpRequestFactory(new OkHttpClient().newBuilder()
|
|
|
+ .connectionPool(new ConnectionPool(1000, 10, TimeUnit.MINUTES))
|
|
|
+ .build()));
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
- public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
|
|
|
- ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
|
|
- scheduler.setPoolSize(poolSize);
|
|
|
- scheduler.setWaitForTasksToCompleteOnShutdown(true);
|
|
|
-// scheduler.setThreadNamePrefix("");
|
|
|
-// scheduler.setAwaitTerminationSeconds(60);
|
|
|
-// scheduler.setErrorHandler(e -> log.error(">>> {}", e.getMessage(), e));
|
|
|
- scheduler.initialize();
|
|
|
- return scheduler;
|
|
|
+ public ThreadPoolExecutor executor() {
|
|
|
+ ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS,
|
|
|
+ new LinkedBlockingQueue<>(), new CustomizableThreadFactory("test-"));
|
|
|
+ executor.prestartAllCoreThreads();
|
|
|
+ return executor;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -84,19 +90,19 @@ class ConcurrentTest {
|
|
|
e.printStackTrace();
|
|
|
return;
|
|
|
}
|
|
|
- resultPath = resultPath + "黑名单查询接口" + poolSize + "个线程1000条数据测试结果.csv";
|
|
|
+ resultPath = resultPath + "黑名单查询接口" + poolSize + "个线程10000条数据测试结果.csv";
|
|
|
// 接口地址
|
|
|
- String url = "http://192.168.50.3:12120/sms/blacklist/api/query/";
|
|
|
-// String url = "http://133.96.94.108:12120/sms/blacklist/api/query/";
|
|
|
+// String url = "http://192.168.50.3:12120/sms/blacklist/api/query/";
|
|
|
+ String url = "http://133.96.94.108:12120/sms/blacklist/api/query/";
|
|
|
Set<String> phones = readPhones();
|
|
|
for (String t : phones) {
|
|
|
RequestParams dto = new RequestParams();
|
|
|
dto.setPhone(t);
|
|
|
dto.setFromSystem("test");
|
|
|
- threadPoolTaskScheduler.submit(new Task(restTemplate, t, url, dto, singlePath));
|
|
|
+ executor.submit(new Task(template, t, url, dto, singlePath));
|
|
|
}
|
|
|
// 等待所有任务执行结束
|
|
|
- while (threadPoolTaskScheduler.getActiveCount() > 0) {
|
|
|
+ while (executor.getActiveCount() > 0) {
|
|
|
}
|
|
|
mergeResult(phones, resultPath, singlePath);
|
|
|
}
|
|
@@ -124,10 +130,10 @@ class ConcurrentTest {
|
|
|
dto.setPhone(t);
|
|
|
dto.setFromSystem("test");
|
|
|
dto.setOperator("test");
|
|
|
- threadPoolTaskScheduler.submit(new Task(restTemplate, t, url, dto, singlePath));
|
|
|
+ executor.submit(new Task(template, t, url, dto, singlePath));
|
|
|
}
|
|
|
// 等待所有任务执行结束
|
|
|
- while (threadPoolTaskScheduler.getActiveCount() > 0) {
|
|
|
+ while (executor.getActiveCount() > 0) {
|
|
|
}
|
|
|
mergeResult(phones, resultPath, singlePath);
|
|
|
}
|
|
@@ -162,7 +168,7 @@ class ConcurrentTest {
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
class Task implements Runnable {
|
|
|
- private RestTemplate restTemplate;
|
|
|
+ private RestTemplate template;
|
|
|
private String phoneNumber;
|
|
|
private String url;
|
|
|
private Object dto;
|
|
@@ -175,7 +181,7 @@ class ConcurrentTest {
|
|
|
LocalDateTime startTime = LocalDateTime.now();
|
|
|
long startTimestamp = System.currentTimeMillis();
|
|
|
try {
|
|
|
- res = restTemplate.postForObject(url, dto, R.class);
|
|
|
+ res = template.postForObject(url, dto, R.class);
|
|
|
} catch (Exception e) {
|
|
|
res = e.getMessage();
|
|
|
log.error("{} -> {}", phoneNumber, e.getMessage(), e);
|
|
@@ -203,7 +209,7 @@ class ConcurrentTest {
|
|
|
BufferedReader br = new BufferedReader(isr);) {
|
|
|
String line;
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
- result.add(line);
|
|
|
+ result.add(StringUtils.delete(line, "\""));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|