Selaa lähdekoodia

feat: 完成并发测试

weijianghai 2 vuotta sitten
vanhempi
commit
aaa18f7f3f

+ 7 - 0
src/test/java/com/nokia/domainb/capability/PkgValueTest.java

@@ -38,4 +38,11 @@ public class PkgValueTest {
             e.printStackTrace();
         }
     }
+
+    @Test
+    void test() {
+        PkgValue pkgValue = new PkgValue();
+        COMPConnectionContext result = pkgValue.query("18515104360", "11", "0010", false);
+        System.out.println(result.getResponseContext());
+    }
 }

+ 136 - 32
src/test/java/com/nokia/domainb/capability/controller/DomainControllerTest.java

@@ -2,9 +2,10 @@ package com.nokia.domainb.capability.controller;
 
 import com.alibaba.fastjson2.JSON;
 import com.nokia.domainb.DomainBApplication;
-import com.nokia.domainb.capability.dto.RouteDTO;
-import com.nokia.domainb.capability.dto.SaDTO;
-import com.nokia.domainb.common.http.R;
+import com.nokia.domainb.dto.RouteDTO;
+import com.nokia.domainb.dto.SaAndVolteDTO;
+import com.nokia.domainb.dto.SaDTO;
+import com.nokia.common.http.R;
 import com.nokia.domainb.util.Utils;
 import de.siegmar.fastcsv.reader.CsvReader;
 import de.siegmar.fastcsv.reader.CsvRow;
@@ -34,21 +35,120 @@ class DomainControllerTest {
     @Autowired
     ThreadPoolTaskScheduler threadPoolTaskScheduler;
 
+    /**
+     * 路由查询并发测试
+     */
     @Test
     void testRoute() {
+        // 每个请求结果保存的路径
+        String singlePath = "z:/route/single/";
+        // 合并结果保存的路径
+        String resultPath = "z:/route/";
+        // 创建文件夹
+        try {
+            Files.createDirectories(Paths.get(singlePath));
+            Files.createDirectories(Paths.get(resultPath));
+        } catch (IOException e) {
+            e.printStackTrace();
+            return;
+        }
+        resultPath = resultPath + "路由查询10个线程10000条数据测试结果.csv";
+//        resultPath = resultPath + "路由查询10个线程1000条数据测试结果.csv";
+//        resultPath = resultPath + "路由查询10个线程100条数据测试结果.csv";
+        // 接口地址
         String url = "http://133.96.94.108:12091/domainB/api/route/";
         Set<String> phones = Utils.readPhones();
         for (String t : phones) {
             String[] a = t.split(",");
-            String phone = a[0];
-            threadPoolTaskScheduler.submit(new RouteTask(restTemplate, phone, url));
+            String phoneNumber = a[0];
+            RouteDTO dto = new RouteDTO("test", phoneNumber);
+            threadPoolTaskScheduler.submit(new Task(restTemplate, phoneNumber, url, dto, singlePath));
         }
+        // 等待所有任务执行结束
         while (threadPoolTaskScheduler.getActiveCount() > 0) {
         }
-        String resultPath = "z:/route/路由查询10个线程10000条数据测试结果.csv";
-//        String resultPath = "z:/route/路由查询10个线程1000条数据测试结果.csv";
-//        String resultPath = "z:/route/result.csv";
-        OutputStreamWriter osw = null;
+        mergeResult(phones, resultPath, singlePath);
+    }
+
+    /**
+     * 5GSA查询并发测试
+     */
+    @Test
+    void testSa() {
+        // 每个请求结果保存的路径
+        String singlePath = "z:/sa/single/";
+        // 合并结果保存的路径
+        String resultPath = "z:/sa/";
+        // 创建文件夹
+        try {
+            Files.createDirectories(Paths.get(singlePath));
+            Files.createDirectories(Paths.get(resultPath));
+        } catch (IOException e) {
+            e.printStackTrace();
+            return;
+        }
+        resultPath = resultPath + "5GSA查询10个线程10000条数据测试结果.csv";
+//        resultPath = resultPath + "5GSA查询10个线程1000条数据测试结果.csv";
+//        resultPath = resultPath + "5GSA查询10个线程100条数据测试结果.csv";
+        // 接口地址
+        String url = "http://133.96.94.108:12091/domainB/api/sa/";
+        Set<String> phones = Utils.readPhones();
+        for (String t : phones) {
+            String[] a = t.split(",");
+            String phoneNumber = a[0];
+            SaDTO dto = new SaDTO("test", phoneNumber);
+            threadPoolTaskScheduler.submit(new Task(restTemplate, phoneNumber, url, dto, singlePath));
+        }
+        // 等待所有任务执行结束
+        while (threadPoolTaskScheduler.getActiveCount() > 0) {
+        }
+        mergeResult(phones, resultPath, singlePath);
+    }
+
+    /**
+     * 功能查询并发测试
+     */
+    @Test
+    void testSaAndVolte() {
+        // 每个请求结果保存的路径
+        String singlePath = "z:/saAndVolte/single/";
+        // 合并结果保存的路径
+        String resultPath = "z:/saAndVolte/";
+        // 创建文件夹
+        try {
+            Files.createDirectories(Paths.get(singlePath));
+            Files.createDirectories(Paths.get(resultPath));
+        } catch (IOException e) {
+            e.printStackTrace();
+            return;
+        }
+        resultPath = resultPath + "功能查询10个线程10000条数据测试结果.csv";
+//        resultPath = resultPath + "功能查询10个线程1000条数据测试结果.csv";
+//        resultPath = resultPath + "功能查询10个线程100条数据测试结果.csv";
+        // 接口地址
+        String url = "http://133.96.94.108:12091/domainB/api/product/saAndVolte/";
+        Set<String> phones = Utils.readPhones();
+        for (String t : phones) {
+            String[] a = t.split(",");
+            String phoneNumber = a[0];
+            SaAndVolteDTO dto = new SaAndVolteDTO("test", phoneNumber);
+            threadPoolTaskScheduler.submit(new Task(restTemplate, phoneNumber, url, dto, singlePath));
+        }
+        // 等待所有任务执行结束
+        while (threadPoolTaskScheduler.getActiveCount() > 0) {
+        }
+        mergeResult(phones, resultPath, singlePath);
+    }
+
+    /**
+     * 合并结果
+     *
+     * @param phones     手机列表
+     * @param resultPath 合并结果保存路径
+     * @param singlePath 单个结果保存路径
+     */
+    private void mergeResult(Set<String> phones, String resultPath, String singlePath) {
+        OutputStreamWriter osw;
         try {
             osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(resultPath)), "GBK");
         } catch (IOException e) {
@@ -60,14 +160,14 @@ class DomainControllerTest {
         for (String tt : phones) {
             String[] a = tt.split(",");
             String phone = a[0];
-            String singlePath = "z:/route/single/" + phone + ".csv";
-            Path path = Paths.get(singlePath);
+            Path path = Paths.get(singlePath + phone + ".csv");
             try (CsvReader csvReader = CsvReader.builder().build(path, StandardCharsets.UTF_8)) {
                 for (CsvRow row : csvReader) {
-                    csvWriter.writeRow(row.getField(0), row.getField(1), row.getField(2), row.getField(3));
+                    csvWriter.writeRow(row.getField(0), row.getField(1), row.getField(2),
+                            row.getField(3));
                 }
             } catch (IOException e) {
-                e.printStackTrace();
+                log.error("{} -> {}", phone, e.getMessage(), e);
             }
         }
         try {
@@ -77,41 +177,45 @@ class DomainControllerTest {
         }
     }
 
-    @Test
-    void testSa() {
-        String url = "http://133.96.94.108:12091/domainB/api/sa/";
-        R r = restTemplate.postForObject(url, new SaDTO("test", "18503369666"), R.class);
-        System.out.println(r);
-    }
-//
-//    @Test
-//    void testSaAndVolte() {
-//        String url = "http://133.96.94.108:12091/domainB/api/product/saAndVolte/";
-//        R r = restTemplate.postForObject(url, new SaAndVolteDTO("test", "18503369666"), R.class);
-//        System.out.println(r);
-//    }
-
     @AllArgsConstructor
-    class RouteTask implements Runnable {
+    class Task implements Runnable {
         private RestTemplate restTemplate;
         private String phoneNumber;
         private String url;
+        private Object dto;
+        private String singlePath;
 
         @Override
         public void run() {
-            RouteDTO dto = new RouteDTO("test", phoneNumber);
+            String filePath = singlePath + phoneNumber + ".csv";
             LocalDateTime startTime = LocalDateTime.now();
             long startTimestamp = System.currentTimeMillis();
-            R r = restTemplate.postForObject(url, dto, R.class);
+            R r;
+            try {
+                r = restTemplate.postForObject(url, dto, R.class);
+            } catch (Exception e) {
+                long endTimestamp = System.currentTimeMillis();
+                long cost = endTimestamp - startTimestamp;
+                log.error("{} cost {} -> {}", phoneNumber, cost, e.getMessage(), e);
+                try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(filePath)),
+                        StandardCharsets.UTF_8);
+                     CsvWriter csv = CsvWriter.builder().build(osw);) {
+                    csv.writeRow(JSON.toJSONString(dto), startTime.toString(), String.valueOf(cost),
+                            JSON.toJSONString(e.getMessage()));
+                    return;
+                } catch (IOException ioException) {
+                    log.error("{} cost {} -> {}", phoneNumber, cost, ioException.getMessage(), ioException);
+                    return;
+                }
+            }
             long endTimestamp = System.currentTimeMillis();
             long cost = endTimestamp - startTimestamp;
-            String filePath = "z:/route/single/" + phoneNumber + ".csv";
             try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(filePath)),
                     StandardCharsets.UTF_8);
                  CsvWriter csv = CsvWriter.builder().build(osw);) {
                 csv.writeRow(JSON.toJSONString(dto), startTime.toString(), String.valueOf(cost), JSON.toJSONString(r));
             } catch (IOException e) {
-                e.printStackTrace();
+                log.error("{} cost {} -> {}", phoneNumber, cost, e.getMessage(), e);
             }
         }
     }