From 126295cb2348d96c9bcd17c6b9e4aee015a47189 Mon Sep 17 00:00:00 2001 From: dingpei Date: Mon, 4 Mar 2024 17:00:38 +0800 Subject: [PATCH] =?UTF-8?q?[fix],=20=E4=BD=BF=E7=94=A8=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=9A=84list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/run/mone/mock/MockJsUtils.java | 17 +++++------- .../java/run/mone/mock/test/MockTest.java | 2 +- .../java/run/mone/sysFunc/SysFuncUtils.java | 27 +++++++++++++++++++ .../run/mone/sysFunc/test/SysFuncTest.java | 8 ++++++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java b/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java index 71679e14b..2b8ffcaf2 100644 --- a/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java +++ b/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java @@ -9,8 +9,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Supplier; import java.util.stream.IntStream; @@ -53,12 +53,14 @@ public static String mock(String template) { } public static List batchMock(String template, int number) { - return batchOperation(new ArrayList<>(), () -> mock(template), number); + return batchOperation(() -> mock(template), number); } - public static List batchOperation(List res, Supplier supplier, int number) { + public static List batchOperation(Supplier supplier, int number) { + List res = new CopyOnWriteArrayList<>(); + int batchNumber = 1000; - int n = ((number - res.size()) / batchNumber) + 1; + int n = (number / batchNumber) + 1; for (int j = 0; j <= n; j++) { int defaultNumber = batchNumber; @@ -71,12 +73,7 @@ public static List batchOperation(List res, Supplier sup }); } - if (res.size() >= number) { - List res1 = res.subList(0, number); - return res1; - } else { - return batchOperation(res, supplier, number); - } + return res.subList(0, number); } diff --git a/jcommon/mockjs/src/test/java/run/mone/mock/test/MockTest.java b/jcommon/mockjs/src/test/java/run/mone/mock/test/MockTest.java index cd5d8f90f..f6731eef4 100644 --- a/jcommon/mockjs/src/test/java/run/mone/mock/test/MockTest.java +++ b/jcommon/mockjs/src/test/java/run/mone/mock/test/MockTest.java @@ -44,7 +44,7 @@ public void mockjsTestRandom() { public void mockjsTestRandomBatch() { String input = "Mock.Random.title(3, 5)"; long begin = System.currentTimeMillis(); - List output = MockJsUtils.batchMock(input, 9999); + List output = MockJsUtils.batchMock(input, 99999); long costtime = System.currentTimeMillis() - begin; System.out.println(output); } diff --git a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java index 439a2103b..b833e1f49 100644 --- a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java +++ b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java @@ -5,8 +5,11 @@ import java.util.Arrays; import java.util.List; import java.util.UUID; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ThreadLocalRandom; +import java.util.function.Supplier; import java.util.stream.Collectors; +import java.util.stream.IntStream; import static run.mone.sysFunc.SysFuncConst.*; @@ -48,6 +51,30 @@ public static String gen(String funcDesc) { } } + public static List batchGen(String funcDesc, int number) { + return batchOperation(() -> gen(funcDesc), number); + } + + public static List batchOperation(Supplier supplier, int number) { + List res = new CopyOnWriteArrayList<>(); + + int batchNumber = 1000; + int n = (number / batchNumber) + 1; + + for (int j = 0; j <= n; j++) { + int defaultNumber = batchNumber; + IntStream.range(0, defaultNumber) + .parallel() // 将流转换为并行流 + .forEach(i -> { + // 这里是要并行执行的操作 + String mockData = supplier.get(); + res.add(mockData); + }); + } + + return res.subList(0, number); + } + private static String subString(List funcParamList, String defaultStr) { //substring(int beginIndex) diff --git a/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java b/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java index 97e44f580..9600d2ef4 100644 --- a/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java +++ b/jcommon/sysFunc/src/test/java/run/mone/sysFunc/test/SysFuncTest.java @@ -4,6 +4,8 @@ import org.junit.Test; import run.mone.sysFunc.SysFuncUtils; +import java.util.List; + @Ignore public class SysFuncTest { @@ -25,6 +27,12 @@ public void testRandomNumber() { System.out.println(res); } + @Test + public void testRandomNumberBatch() { + List res = SysFuncUtils.batchGen("${java.randomNumber(2,11)}", 6); + System.out.println(res); + } + }