From 3c0c559a9b38156bc190f3409d3891bc6fd7b8ec Mon Sep 17 00:00:00 2001 From: Alawn Date: Tue, 6 Jul 2021 18:25:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8E=8B=E6=B5=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ctg/tool/DataFactory.java | 34 ++++---- src/test/java/hro/DiskApplyRequestTest.java | 4 +- src/test/java/hro/DiskApplyTest.java | 4 +- src/test/java/hro/MixedTest.java | 4 +- src/test/java/hro/StressTest.java | 92 +++++++-------------- src/test/java/hro/UatTest.java | 4 +- src/test/resources/prop.setting.stress | 1 + 7 files changed, 57 insertions(+), 86 deletions(-) diff --git a/src/main/java/ctg/tool/DataFactory.java b/src/main/java/ctg/tool/DataFactory.java index b1a7b07..8dc286e 100644 --- a/src/main/java/ctg/tool/DataFactory.java +++ b/src/main/java/ctg/tool/DataFactory.java @@ -37,7 +37,7 @@ public class DataFactory { ArrayList list = new ArrayList(totalCount); - String[] amounts = newAmounts(totalCount,totalAmount.multiply(new BigDecimal("100.00")).toBigInteger().intValue(),1); + String[] amounts = randomAmounts(totalCount,totalAmount.multiply(new BigDecimal("100.00")).toBigInteger().intValue(),1); for(int i=0;i card = pickCmbCardRandomly(); - list.add(RequestDiskRow.builder().acctName(card.getValue()).acctNo(card.getKey()).amount(amounts[i]).refId(pickRefIdRandomly()).build()); + Pair card = randomCmbCard(); + list.add(RequestDiskRow.builder().acctName(card.getValue()).acctNo(card.getKey()).amount(amounts[i]).refId(randomRefId()).build()); break; } /** 工行测试数据 */ case 1: { - Pair card = pickIcbcCardRandomly(); - list.add(RequestDiskRow.builder().acctName(card.getValue()).acctNo(card.getKey()).amount(amounts[i]).refId(pickRefIdRandomly()).build()); + Pair card = randomIcbcCard(); + list.add(RequestDiskRow.builder().acctName(card.getValue()).acctNo(card.getKey()).amount(amounts[i]).refId(randomRefId()).build()); break; } /** 假数据 */ default: { - list.add(RequestDiskRow.builder().acctName(newCardName()).acctNo(pickCardNoRandomly()).amount(amounts[i]).refId(pickRefIdRandomly()).build()); + list.add(RequestDiskRow.builder().acctName(randomCardName()).acctNo(randomCardNo()).amount(amounts[i]).refId(randomRefId()).build()); break; } } @@ -72,11 +72,11 @@ public class DataFactory { * 生成工行 & 招行测试用的卡号 + 姓名 * @return */ - public static final Pair pickCmbCardRandomly(){ + public static final Pair randomCmbCard(){ return cmbCards.get(RandomUtil.randomInt(0,cmbCards.size())); } - public static final Pair pickIcbcCardRandomly(){ + public static final Pair randomIcbcCard(){ return icbcCards.get(RandomUtil.randomInt(0,icbcCards.size())); } @@ -97,16 +97,16 @@ public class DataFactory { add(new Pair<>("6214831150131511","吴极客")); }}; - public static final String pickCardNoRandomly() { + public static final String randomCardNo() { int index = random.nextInt(cards.length - 1); return cards[index]; } - public static String pickRefIdRandomly() { + public static String randomRefId() { return String.valueOf(IdUtil.getSnowflake(1,1).nextIdStr()); } - public static String newAmount() + public static String randomAmount() { return String.valueOf(random.nextInt(500000000)); } @@ -116,10 +116,10 @@ public class DataFactory { return amount; } - public static String pickPostscriptRandomly + public static String randomPostscript () { return postscripts[random.nextInt(postscripts.length - 1)]; } - public static String pickCustomerIdRandomly() throws SQLException { + public static String randomCustomerId() throws SQLException { String customerId = ""; List rs = Db.use().query("SELECT CUSTOMER_ID FROM CAPCUSINF WHERE ENABLED = 'Y'",new HashMap()); @@ -135,7 +135,7 @@ public class DataFactory { } - public static String pickTitleIdRandomly() throws SQLException { + public static String randomTitleId() throws SQLException { String titleId = ""; List rs = null; @@ -279,7 +279,7 @@ public class DataFactory { private final static String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘"; - private static final String newCardName() + private static final String randomCardName() { int index = random.nextInt(Surname.length - 1); String name = Surname[index]; //获得一个随机的姓氏 @@ -309,7 +309,7 @@ public class DataFactory { return name; } - public static String[] newAmounts(int count, BigDecimal totalAmount) + public static String[] randomAmounts(int count, BigDecimal totalAmount) { String[] amounts = new String[count]; @@ -325,7 +325,7 @@ public class DataFactory { * @param minMoney 最小金额 * @return */ - private static String[] newAmounts(int num,long totalMoney,long minMoney){ + private static String[] randomAmounts(int num, long totalMoney, long minMoney){ String[] amounts = new String[num]; long[] each = new long[num]; for(int i=0;i request = new Api<>("diskApply",body); diff --git a/src/test/java/hro/MixedTest.java b/src/test/java/hro/MixedTest.java index 4978b79..a52c12f 100644 --- a/src/test/java/hro/MixedTest.java +++ b/src/test/java/hro/MixedTest.java @@ -95,8 +95,8 @@ public class MixedTest { public DiskInfo diskApplySimpleTest() throws Exception { DiskApplyRequest request = DiskApplyRequest.builder() .applyDate(DateUtil.format(new Date(),"YYYYMMdd")) - .customerId(DataFactory.pickCustomerIdRandomly()) - .titleId(DataFactory.pickTitleIdRandomly()) + .customerId(DataFactory.randomCustomerId()) + .titleId(DataFactory.randomTitleId()) .diskDesc("代发工资").build(); int totalCount = RandomUtil.randomInt(0,3000); diff --git a/src/test/java/hro/StressTest.java b/src/test/java/hro/StressTest.java index bbc45c9..733aecf 100644 --- a/src/test/java/hro/StressTest.java +++ b/src/test/java/hro/StressTest.java @@ -2,9 +2,9 @@ package hro; import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.TimeInterval; -import cn.hutool.core.lang.Console; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.XmlUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.http.HttpUtil; @@ -24,6 +24,7 @@ import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.AesKeyStrength; import net.lingala.zip4j.model.enums.EncryptionMethod; +import org.junit.Assert; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; @@ -49,8 +50,7 @@ public class StressTest { // private final static String propFile = "prop.setting.uat"; // private final static String propFile = "prop.setting.sit"; private final static String propFile = "prop.setting.stress"; -// private final TimeInterval timer = DateUtil.timer(); - + private final TimeInterval timer = DateUtil.timer(); @BeforeAll static void initProp() { @@ -60,48 +60,19 @@ public class StressTest { zipPassword = setting.getStr("zipPassword"); } - - @DisplayName("自定义名称重复测试") - @RepeatedTest(value = 3, name = "{displayName} 第 {currentRepetition} 次") - public void testJunit() throws InterruptedException { - - ThreadUtil.execAsync(new Runnable() { - @SneakyThrows - @Override - public void run() { - System.out.println(Thread.currentThread().getName() + " is started"); -// String threadName = Thread.currentThread().getName(); -// timer.start(threadName); -// Console.log("{} is started",threadName); - log.info("{} is started",Thread.currentThread().getName()); - ThreadUtil.safeSleep(1000); - log.info("{} is end",Thread.currentThread().getName()); -// Console.log("{} is end",threadName); -// System.out.println(threadName + "is end"); -// System.out.println("is end"); -// System.out.println(Thread.currentThread().getName() + "is end"); - } - }); - } - - - @JunitPerfConfig(threads = 5, warmUp = 1000, duration = 2000) - public void perfTest() { - log.info("{} is started",Thread.currentThread().getName()); - ThreadUtil.safeSleep(1000); - log.info("{} is end",Thread.currentThread().getName()); - } - - + /** 压测入口 */ @Test @JunitPerfConfig(threads = 2, warmUp = 1000, duration = 2000) - public void overAllTest() throws Exception { + public void stressTest() { + String threadName = Thread.currentThread().getName(); + log.info("{} started",threadName); + timer.start(threadName); DiskInfo disk; try{ /** 生成测试批次 */ - String customerId = DataFactory.pickCustomerIdRandomly(); - String titleId = DataFactory.pickTitleIdRandomly(); - int totalCount = RandomUtil.randomInt(0,3000); + String customerId = DataFactory.randomCustomerId(); + String titleId = DataFactory.randomTitleId(); + int totalCount = RandomUtil.randomInt(1,1000); BigDecimal totalAmount = RandomUtil.randomBigDecimal(new BigDecimal(1),new BigDecimal(1000000)); /** 新建文件 */ File file = DiskTool.newSimpleDiskFile(customerId,titleId,"代发工资",totalCount,totalAmount); @@ -111,18 +82,15 @@ public class StressTest { log.error(e.getMessage()); return; } - log.info("disk:{}",disk); - log.info("等待30秒后查询结果..."); - ThreadUtil.sleep(30000); - try { - diskResult(disk.getApplyDate(),disk.getDiskNo()); - } - catch (Exception e) { - log.error("error:{}",e.getMessage()); - return; - } - log.info("disk:{}",disk); + DiskInfo result = null; + + /* 批次为空(表示还在入库中),批次未终态(U:预计 0:成功 1:处理中) */ + while(result == null || StrUtil.equalsAny(result.getDiskSts(),"U","0","1")) { + ThreadUtil.sleep(3); + result = diskResult(disk.getApplyDate(),disk.getDiskNo()); + } + log.info("{} is completed, elapsed time:{}, disk:{}",threadName,timer.intervalSecond(threadName),result); } @Test @@ -138,6 +106,7 @@ public class StressTest { log.info("disk:{}",disk); } + /* 结果查询 */ @SneakyThrows public DiskInfo diskResult(String applyDate,String diskNo) { DiskResultRequest request = DiskResultRequest.builder().applyDate(applyDate).diskNo(diskNo).build(); @@ -165,26 +134,18 @@ public class StressTest { return disk; } else { - throw new Exception(respMessage); + return null; } } + /* 送盘 */ public DiskInfo diskApply(String customerId,String titleId,File file) throws Exception { -// DiskApplyRequest request = DiskApplyRequest.builder() -// .applyDate(DateUtil.format(new Date(),"YYYYMMdd")) -// .customerId(DataFactory.newCustomerId()) -// .titleId(DataFactory.newTitleId()) -// .diskDesc("代发工资").build(); DiskApplyRequest request = DiskApplyRequest.builder() .applyDate(DateUtil.format(new Date(),"YYYYMMdd")) .customerId(customerId) .titleId(titleId) .diskDesc("代发工资").build(); - -// int totalCount = RandomUtil.randomInt(0,3000); -// BigDecimal totalAmount = RandomUtil.randomBigDecimal(new BigDecimal(1),new BigDecimal(1000000)); -// File file = DiskTool.newSimpleDiskFile(request.getCustomerId(),request.getTitleId(),"代发工资",totalCount,totalAmount); request.setDiskNo(file.getName()); log.info("disk:{}", file.getAbsolutePath()); @@ -232,4 +193,13 @@ public class StressTest { } return DiskInfo.builder().diskNo(request.getDiskNo()).applyDate(request.getApplyDate()).build(); } + + @JunitPerfConfig(threads = 100, duration = 6000) + public void perfTest() { + String threadName = Thread.currentThread().getName(); + timer.start(threadName); + log.info("{} is started, start time:{}",threadName,DateUtil.now()); + ThreadUtil.safeSleep(3000); + log.info("{} is end, elapsed time:{}",threadName,timer.intervalSecond(threadName)); + } } diff --git a/src/test/java/hro/UatTest.java b/src/test/java/hro/UatTest.java index c9de958..8d76cf1 100644 --- a/src/test/java/hro/UatTest.java +++ b/src/test/java/hro/UatTest.java @@ -56,8 +56,8 @@ public class UatTest { DiskInfo disk; try{ /** 生成测试批次 */ - String customerId = DataFactory.pickCustomerIdRandomly(); - String titleId = DataFactory.pickTitleIdRandomly(); + String customerId = DataFactory.randomCustomerId(); + String titleId = DataFactory.randomTitleId(); int totalCount = RandomUtil.randomInt(0,3000); BigDecimal totalAmount = RandomUtil.randomBigDecimal(new BigDecimal(1),new BigDecimal(1000000)); /** 新建文件 */ diff --git a/src/test/resources/prop.setting.stress b/src/test/resources/prop.setting.stress index d790895..c239f66 100644 --- a/src/test/resources/prop.setting.stress +++ b/src/test/resources/prop.setting.stress @@ -1,3 +1,4 @@ baseURL= http://172.17.20.91:9998/api secretKey = HelloWorld zipPassword = 1234567 +