Ver Fonte

test: 完成一键解封接口并发测试

weijianghai há 2 anos atrás
pai
commit
4ff23979fe

+ 2 - 1
sms_blk_api/.gitignore

@@ -3,4 +3,5 @@ target/
 # vscode项目配置文件路径
 .vscode
 # office 临时文件
-~$*
+~$*
+test/result

+ 12 - 0
sms_blk_api/pom.xml

@@ -28,6 +28,18 @@
             <artifactId>lombok</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/de.siegmar/fastcsv -->
+        <dependency>
+            <groupId>de.siegmar</groupId>
+            <artifactId>fastcsv</artifactId>
+            <version>2.2.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

+ 159 - 0
sms_blk_api/src/test/java/com/nokia/sms/ConcurrentTest.java

@@ -0,0 +1,159 @@
+package com.nokia.sms;
+
+import com.alibaba.fastjson2.JSON;
+import com.nokia.common.http.R;
+import com.nokia.sms.vo.RequestParams;
+import de.siegmar.fastcsv.reader.CsvReader;
+import de.siegmar.fastcsv.reader.CsvRow;
+import de.siegmar.fastcsv.writer.CsvWriter;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Import;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+import org.springframework.web.client.RestTemplate;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * 并发测试
+ */
+@Slf4j
+@SpringBootTest
+@Import(ConcurrentTestConfig.class)
+class ConcurrentTest {
+    /**
+     * 测试结果保存文件夹
+     */
+    public static final String TEST_DIR = "test/result/";
+    /**
+     * 手机号文件
+     */
+    public static final String PHONES_FILE = "test/1000.txt";
+    @Autowired
+    private RestTemplate restTemplate;
+    @Autowired
+    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
+
+    @Test
+    void testRemove() {
+        // 每个请求结果保存的路径
+        String singlePath = TEST_DIR + "remove/single/";
+        // 合并结果保存的路径
+        String resultPath = TEST_DIR + "remove/";
+        // 创建文件夹
+        try {
+            Files.createDirectories(Paths.get(singlePath));
+            Files.createDirectories(Paths.get(resultPath));
+        } catch (IOException e) {
+            e.printStackTrace();
+            return;
+        }
+        resultPath = resultPath + "一键解封接口" + threadPoolTaskScheduler.getPoolSize() + "个线程1000条数据测试结果.csv";
+        // 接口地址
+        String url = "http://133.96.94.108:12120/sms/blacklist/api/remove/";
+        Set<String> phones = readPhones();
+        for (String t : phones) {
+            RequestParams dto = new RequestParams();
+            dto.setPhone(t);
+            dto.setFromSystem("test");
+            dto.setOperator("test");
+            threadPoolTaskScheduler.submit(new Task(restTemplate, t, 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) {
+        try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(resultPath)),
+                StandardCharsets.UTF_8);
+             CsvWriter csvWriter = CsvWriter.builder().build(osw);) {
+            csvWriter.writeRow("入参", "时间", "调用时长", "结果");
+            for (String tt : phones) {
+                Path path = Paths.get(singlePath + tt + ".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));
+                    }
+                } catch (IOException e) {
+                    log.error("{} -> {}", tt, e.getMessage(), e);
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    @AllArgsConstructor
+    class Task implements Runnable {
+        private RestTemplate restTemplate;
+        private String phoneNumber;
+        private String url;
+        private Object dto;
+        private String singlePath;
+
+        @Override
+        public void run() {
+            Object res;
+            String filePath = singlePath + phoneNumber + ".csv";
+            LocalDateTime startTime = LocalDateTime.now();
+            long startTimestamp = System.currentTimeMillis();
+            try {
+                res = restTemplate.postForObject(url, dto, R.class);
+            } catch (Exception e) {
+                res = e.getMessage();
+                log.error("{} -> {}", phoneNumber, e.getMessage(), e);
+            }
+            long endTimestamp = System.currentTimeMillis();
+            long cost = endTimestamp - startTimestamp;
+            // 记录请求结果
+            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(res));
+            } catch (IOException e) {
+                log.error("{} cost {} -> {}", phoneNumber, cost, e.getMessage(), e);
+            }
+        }
+    }
+
+    /**
+     * 读取手机号放入set去重
+     */
+    Set<String> readPhones() {
+        Set<String> result = new HashSet<>();
+        try (InputStreamReader isr = new InputStreamReader(Files.newInputStream(Paths.get(PHONES_FILE)),
+                StandardCharsets.UTF_8);
+             BufferedReader br = new BufferedReader(isr);) {
+            String line;
+            while ((line = br.readLine()) != null) {
+                result.add(line);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return result;
+    }
+}

+ 30 - 0
sms_blk_api/src/test/java/com/nokia/sms/ConcurrentTestConfig.java

@@ -0,0 +1,30 @@
+package com.nokia.sms;
+
+
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * 并发测试配置
+ */
+@TestConfiguration
+public class ConcurrentTestConfig {
+    @Bean
+    public RestTemplate restTemplate() {
+        return new RestTemplate();
+    }
+
+    @Bean
+    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
+        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
+        scheduler.setPoolSize(40);
+        scheduler.setWaitForTasksToCompleteOnShutdown(true);
+//        scheduler.setThreadNamePrefix("");
+//        scheduler.setAwaitTerminationSeconds(60);
+//        scheduler.setErrorHandler(e -> log.error(">>> {}", e.getMessage(), e));
+        scheduler.initialize();
+        return scheduler;
+    }
+}

+ 1000 - 0
sms_blk_api/test/1000.txt

@@ -0,0 +1,1000 @@
+18503369666
+18632450346
+15633553235
+18632157917
+16603112238
+15557153750
+13206786656
+18331524020
+13131680581
+18603271888
+13131221221
+13111683050
+18622305641
+15511310932
+15690237898
+13091131129
+15510416665
+15530375352
+13931598728
+16632722819
+13273661508
+13040049000
+13223275860
+15532531065
+13031410034
+15503388534
+13081859611
+18503231698
+13171998053
+13190795236
+15603275117
+15531220675
+13284360056
+13273636560
+15632266832
+18633054444
+15533071403
+18630003374
+16603192388
+17713146572
+17614317358
+18631162617
+13292406126
+15512941676
+15633670896
+18632475627
+15503238898
+15613482606
+18630240806
+13283235851
+13180031341
+18611334493
+18630605703
+17531390978
+18531566123
+15530639577
+13091221187
+15612749206
+15612474341
+15613885837
+13011568765
+18531019416
+13141188868
+13180307530
+18533267257
+13292043885
+13223109228
+15530008680
+13031500072
+18630243080
+18630002021
+13131136089
+18632120668
+13166545565
+13082175560
+15632632723
+13283427871
+15614451335
+17558818518
+18631584994
+18623847650
+13111731269
+18533611782
+15630780157
+13292084388
+13082366257
+18630294607
+15603175790
+19912374869
+15613835508
+15633109812
+15530123667
+18617879579
+17732135772
+13246577577
+18610332003
+15731766644
+15030080148
+13111494949
+15631108897
+15531798837
+18617651537
+15531880088
+16682051555
+15600580093
+17603355999
+17633013880
+18513855267
+16632776660
+18630405208
+15531550373
+13273601889
+18631532086
+13643126070
+17600718766
+13069302121
+13133516266
+13031449956
+17631150559
+13131067807
+17526582274
+17631551145
+13273662873
+13230163130
+18637255554
+17631150555
+15503319106
+15633955101
+13131542888
+15511944044
+13180243828
+15530219823
+15614880826
+18633987816
+15530299939
+15612571508
+18561532393
+18601022739
+15630293096
+15632101219
+17692130802
+13230581947
+13102913830
+18632589633
+18631136301
+18630192680
+15612108961
+13043125605
+13127368139
+13230327929
+18511823136
+13520579329
+15633013698
+18333875152
+15532165130
+18631770890
+16632498176
+13091444319
+13223159555
+18611720832
+13229350665
+18503379028
+18632071198
+18633367653
+13070509777
+15530525558
+16630203689
+18633379653
+13223238820
+16632328061
+13133541276
+18633450150
+15932505320
+18634067589
+13013233859
+17692758111
+15532082298
+13293271375
+18531248491
+18632445838
+13288718889
+18633962589
+18632698807
+17631030717
+15511908380
+15632831928
+18631053466
+13283290606
+13082330258
+18632732388
+15630119789
+16631775554
+15503340509
+13833035109
+15230297707
+13031418834
+15532985897
+13293079653
+13165545796
+15633068819
+18632584826
+18633337796
+18531752011
+13092801999
+17659733836
+15631337853
+15533770904
+15831143523
+16630916111
+15532742220
+15612911150
+13288322770
+13131251026
+13082378215
+15533305828
+18631463878
+15613630229
+18617760730
+13230581980
+17507083317
+16630708835
+15511454776
+15512217186
+15503184597
+15511609853
+15530231518
+15613702393
+16630334214
+13284476666
+17692370586
+13290618927
+15613120477
+18501038982
+17633086827
+15503026360
+13102582327
+15130695101
+15614967262
+15531088630
+13292810372
+13223441203
+15633025627
+18518511792
+18633762085
+18631289988
+13111373797
+17503298721
+13400310031
+17600159541
+18533605935
+15533647496
+18631220221
+13171608633
+16633203165
+18631686942
+13068754444
+18632605345
+15556188617
+13180411825
+15336177878
+13483372487
+13012058838
+15613359196
+15512015760
+15512268765
+13253100411
+15511408024
+13111572986
+13102911229
+13021838619
+18631895535
+15512226952
+13011910660
+13103161198
+16630509692
+15630842951
+13292808080
+15076592663
+15690381339
+16630336818
+15612852202
+13833407205
+13012020250
+13131392653
+16631156690
+17633359013
+15531927270
+13223117780
+15530495239
+15612145957
+13230951395
+13001860036
+18633182791
+18631418333
+15632222409
+18731447929
+13276012328
+17633210371
+13292350815
+13273219060
+15533210956
+17610189261
+17631701362
+15631234506
+18617734444
+13013221899
+15632223729
+15532571902
+13933923772
+18632798230
+13131126648
+17692608414
+17631541723
+18632251157
+15632409861
+18633449138
+15532276995
+13111373750
+13292681957
+18603251352
+15532269806
+15613361405
+15512856411
+13131872611
+15731000068
+15633181126
+17692767917
+18630135262
+18631246891
+18632606633
+15503345371
+13020820627
+15603219095
+13292011365
+15632425065
+13191660989
+13273443197
+15531124888
+17600780788
+15532966747
+13031898147
+18630630680
+18631233573
+16630930392
+15530266129
+15530988342
+17659992658
+17610201371
+15690520850
+18603213068
+15511671717
+17631962801
+15633042163
+15503140008
+13013212080
+13131427916
+13111309177
+16630864455
+13131911055
+13103258527
+17633084141
+15511268889
+17631545319
+15630622048
+15632208136
+15511519895
+17631534662
+15511761888
+18532224322
+13293306621
+13016149569
+13103066605
+18533538550
+15613323112
+18631736101
+18531056488
+15512741212
+17603251284
+15631708598
+15614925304
+13110011892
+13111677277
+13293259663
+16631090320
+13021855111
+15694936791
+13785494209
+15512213675
+15033034666
+18518095064
+13292265678
+18633703651
+17631659256
+17610377719
+15530695739
+15612052072
+15632829591
+16607025403
+17631282430
+13231429817
+13229892770
+13290508521
+13131001993
+18833355969
+15614561706
+18631461106
+15632109959
+13273163828
+17533634404
+18633756118
+15632981606
+15632776630
+15511632131
+18531800196
+13111572908
+13293167101
+15512593625
+15612718184
+15533636755
+13171613143
+16630059596
+18531447530
+17603335593
+13127375333
+18518235859
+17547059161
+17633170715
+13273400014
+18595414034
+17631776965
+13831642376
+18500566766
+13032035849
+15631822350
+17692538922
+13131438600
+16630736035
+18633930044
+17692207912
+18333688025
+13294093828
+18532567222
+13011445088
+18632947146
+13102826102
+13784217759
+13230230530
+15530806309
+18630791118
+15613194802
+18630568278
+18630391882
+13512818083
+15175634535
+18631587219
+15031709052
+15503230166
+15031083858
+17631558663
+16630807005
+15613084234
+13231482006
+15530724647
+13103027007
+15544888288
+13013287605
+13111559957
+15631698697
+15630258668
+18630137870
+15690027960
+16630558755
+18503121222
+13231939561
+13172190691
+15533802166
+13249337333
+15631767070
+13293060106
+18631576530
+13103079980
+15533838110
+13082188912
+13230061169
+15690517268
+15530069577
+18634020816
+13107274796
+17614012577
+13283398867
+13930583099
+13131090190
+15531792778
+15612775579
+18532015780
+15803311515
+13132499330
+13273179381
+18503170251
+15603398818
+13231459628
+18503315750
+13131046000
+15511116600
+13035885856
+18533245869
+17631008003
+17692660968
+13091209580
+18617855677
+18633630213
+15511320324
+18631083322
+13910546321
+18631835869
+13191681182
+18631601958
+17631290900
+13103210535
+18531761888
+18633864135
+17645856791
+15603421807
+15631779033
+18630150801
+13001801573
+15533155516
+13223278183
+15633913303
+18632528631
+17692122396
+18630240777
+13231941830
+18630519312
+17531995611
+15632645996
+18631451715
+18034357180
+18632295659
+17692619063
+15512803999
+13223112865
+13284368834
+13111661768
+15679258312
+18532212362
+15632897698
+15511007388
+18631251409
+15666406568
+17631786389
+15503077985
+15533315128
+13020807690
+17631440995
+16630342709
+17531188170
+15690587975
+15532713796
+13191987131
+15630792023
+18617531652
+13223110218
+13082335080
+13081091581
+18640105268
+16603253833
+18533791234
+15530025143
+17631972272
+13231292594
+15531226735
+17631149537
+17631277991
+13191921365
+15613323104
+15512231501
+13124728759
+13131086907
+13111588436
+15530337160
+17631452978
+18630692991
+13013239972
+18634051970
+13191792886
+13230519992
+15544703839
+16663108150
+15613888188
+17633113314
+15613511655
+18531249840
+13273508333
+13273202171
+15633586981
+13040033580
+18732585370
+13102762276
+13290455365
+18630038100
+18531423519
+13180319531
+13273522610
+13131234590
+15533538306
+13175053708
+18693115066
+18632895296
+15533425708
+18515365670
+18532200346
+17631635263
+16630981969
+15630625980
+15613522301
+17531729663
+13091362717
+13032047889
+13133517612
+17532588988
+17600377856
+18617639919
+13103366564
+18610134296
+18631100250
+15503387337
+15531837362
+15503247032
+16631234521
+17631551219
+18630128179
+15516999919
+13131552399
+18630100605
+13231566059
+15630667657
+18631972116
+15632265638
+13081079772
+13230392000
+13180110598
+15512777068
+18634138333
+13082060320
+18531376626
+18531211020
+18603155657
+13131727632
+13191700618
+18633661455
+15530160770
+13102509568
+13091233263
+13028657977
+15703376903
+15530086085
+15631030650
+13131018958
+15533288895
+16631475750
+15632265621
+13103354576
+18532398068
+13223159659
+13070557555
+15613529915
+13191613071
+15544781221
+15511514885
+15612875013
+16632418168
+18531908273
+13028656679
+15511098129
+13131681785
+18533077933
+17692293911
+13193719795
+15533815656
+13293013732
+17603316204
+15530855155
+15632001739
+18631426928
+13230139578
+15630996258
+18630669026
+13011594997
+18631911197
+17631577813
+15632239306
+15531269802
+15598887368
+18531466668
+15612841662
+15512711208
+13040013141
+15630576316
+18632408950
+18631596879
+13111202277
+13180081507
+17661313550
+18630787732
+15603392665
+18032531575
+15633670753
+13273335259
+13030057736
+15512229031
+18633093979
+13231615751
+15631091611
+13231237872
+15631606707
+13231921253
+18603130320
+15614125265
+13103227311
+15603215377
+13102096467
+16683243505
+16631156511
+13091031676
+13171817133
+13082324332
+13223273118
+17691767977
+15533931777
+15632573752
+15694900621
+13131066602
+15690272108
+13131448090
+17692173888
+13081049915
+15632443000
+17631755002
+15533299581
+18631000852
+13230631118
+18633912167
+16631750317
+15630033094
+18533206171
+15544757572
+15630578995
+18733343058
+13273576491
+18617739155
+13292558573
+13111762299
+13171958988
+17531352787
+13171537929
+15632707227
+18631199831
+18631101558
+15630889715
+13102695111
+13082124025
+13273572901
+13231712903
+15632722836
+13047705888
+18631088066
+13303370090
+15633957880
+15633602675
+15613883321
+18603179699
+15611896789
+18531844550
+13290615177
+15631027061
+13133517467
+18603298188
+13284350790
+18630545629
+15033328500
+13180535111
+15531797630
+15632103762
+15511033696
+17633373285
+15530663322
+13011987048
+15532293353
+15614490829
+13073151660
+15613418117
+13131475698
+18603397736
+18633295565
+15533426871
+15613151546
+15632037774
+15631024815
+18633253777
+18633378200
+15603173058
+18134246504
+15531785678
+18631444776
+13014375071
+18511056789
+13171711573
+18632023573
+15694931722
+13171848224
+15632986375
+18633745666
+13165546995
+13091102537
+18632872576
+15531617912
+17734141117
+18630628535
+15632113176
+17603138888
+13283151777
+13290546869
+18630444786
+15630883507
+18600898237
+13184929553
+13293180136
+13040040719
+15532032129
+18632756210
+15512888868
+18631066671
+18633368899
+15512001269
+13012130627
+13292009228
+15512153555
+13231415035
+13230791687
+17631612752
+17631829888
+15511388820
+15511500271
+18832864605
+18532160410
+13084597496
+17631595694
+13082368776
+13231442629
+18532657187
+18631649899
+17631781382
+16632095062
+15690511269
+13230789358
+13165545689
+18611549001
+13131437327
+18631883699
+15631968621
+18631959191
+15630088184
+18631246908
+16633811468
+15694888483
+16630852290
+15631788667
+15532602044
+13191796260
+15633820929
+13131612361
+15512513550
+13229323149
+15633951698
+15631192361
+15632182555
+18503164239
+16603199998
+18568837753
+15543467253
+15614387885
+13171579790
+13292563088
+13103269109
+15531208883
+15613629178
+13012034992
+15612751614
+15511932155
+13021418880
+13012058960
+13131464960
+13131079993
+13230383978
+15632590250
+18531007579
+17631991467
+18532628606
+17503199016
+18603142396
+18600786850
+17631515551
+13011467926
+17612213160
+18875772444
+18631986790
+18632745520
+15503362003
+13001806581
+15910757528
+15613046160
+16631438797
+15511031092
+18531088999
+15544510615
+15633835115
+13290520296
+13131066679
+15583933703
+13084598799
+16630511881
+15530228044
+18632150545
+18603101865
+15532336990
+13103316246
+15612300812
+15613911000
+16603119885
+13513333264
+15613469758
+13082143029
+13091072126
+13230367069
+13102526003
+13191993022
+18630298222
+13070598058
+13022826600
+13933268774
+18617748963
+13171789341
+18713881918
+15530938028
+17633109974
+13091059160
+18634053005
+13180043224
+13121519078
+18503281898
+18564419332
+13292789971
+18230049766
+13231201831
+15690044680
+13180201813
+13230069997
+17603290804
+13231255776
+13111456755
+18931629092
+13141101309
+13103259753
+18630359550
+17631526226
+18603390323
+17701245298
+15630990052
+18033785752
+17531115032
+18617805463
+18630311628
+17633186162
+15508739732
+15511610666
+18610315424
+13091379678
+18630792301
+15633812424
+13103247767
+18531409198
+15512033728
+13273538203
+13230882997
+18203398688
+17531873122
+13284415778
+17631433750
+18610570988
+15633229635
+13102481488
+17559505204
+18631599123
+17631142146
+18730782488
+13242633126
+18531902055
+17631556115
+13131278911