From a776c23b7e52b9708e85c497c172d89f84964014 Mon Sep 17 00:00:00 2001 From: dingpei Date: Thu, 29 Feb 2024 19:53:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[fix],=20=E5=BE=AA=E7=8E=AF=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=EF=BC=8C=E4=BF=9D=E8=AF=81=E6=8C=89=E8=A6=81=E6=B1=82?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/run/mone/mock/MockJsUtils.java | 16 +++++++--------- .../test/java/run/mone/mock/test/MockTest.java | 18 ++++++++++++------ .../java/run/mone/sysFunc/SysFuncEnum.java | 2 +- 3 files changed, 20 insertions(+), 16 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 7d4fee550..89b43c09e 100644 --- a/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java +++ b/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java @@ -53,14 +53,12 @@ public static String mock(String template) { } public static List batchMock(String template, int number) { - return batchOperation(() -> mock(template), number); + return batchOperation(new ArrayList<>(), () -> mock(template), number); } - public static List batchOperation(Supplier supplier, int number) { + public static List batchOperation(List res, Supplier supplier, int number) { int batchNumber = 1000; - List res = new ArrayList<>(); - - int n = (number / batchNumber) + 1; + int n = ((number - res.size()) / batchNumber) + 1; for (int j = 0; j <= n; j++) { int defaultNumber = batchNumber; @@ -73,12 +71,12 @@ public static List batchOperation(Supplier supplier, int number) }); } - if (res.size() > number) { + if (res.size() >= number) { List res1 = res.subList(0, number); return res1; + } else { + return batchOperation(res, supplier, number); } - - return res; } public static String random(String template) { @@ -95,7 +93,7 @@ public static String random(String template) { } public static List batchRandom(String template, int number) { - return batchOperation(() -> random(template), number); + return batchOperation(new ArrayList<>(), () -> random(template), number); } } \ No newline at end of file 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 7903b8523..17718de88 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 @@ -1,11 +1,9 @@ package run.mone.mock.test; -import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import run.mone.mock.MockJsUtils; -import javax.script.ScriptEngine; import java.util.List; @Ignore @@ -14,32 +12,40 @@ public class MockTest { @Test public void mockjsTest() { String input = "{\n" + - " \"boolean|1\": true\n" + + " \"string|1-10\": \"★\"\n" + "}"; + long begin = System.currentTimeMillis(); String output = MockJsUtils.mock(input); - System.out.println(output); + long costtime = System.currentTimeMillis() - begin; + System.out.println(output + " ====== cost time " + costtime); } @Test public void mockjsTestBatch() { String input = "{\n" + - " \"boolean|1\": true\n" + + " \"string|1-10\": \"★\"\n" + "}"; - List output = MockJsUtils.batchMock(input, 10000); + long begin = System.currentTimeMillis(); + List output = MockJsUtils.batchMock(input, 50000); + long costtime = System.currentTimeMillis() - begin; System.out.println(output); } @Test public void mockjsTestRandom() { String input = "Random.title(3, 5)"; + long begin = System.currentTimeMillis(); String output = MockJsUtils.random(input); + long costtime = System.currentTimeMillis() - begin; System.out.println(output); } @Test public void mockjsTestRandomBatch() { String input = "Random.title(3, 5)"; + long begin = System.currentTimeMillis(); List output = MockJsUtils.batchRandom(input, 9999); + long costtime = System.currentTimeMillis() - begin; System.out.println(output); } diff --git a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java index 98dd2495e..e31566eb0 100644 --- a/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java +++ b/jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java @@ -7,7 +7,7 @@ public enum SysFuncEnum { //报警级别 SUBSTRING(FUNC_NAME_SUBSTRING, "common", "截取字符串", "${java.substring(ceshi, 1, 3)}", "返回从指定开始索引到结束索引之间的子字符串,但不包括结束索引位置的字符"), - UUID(FUNC_NAME_UUID, "common", "唯一标识符", "{java.uuid()}", + UUID(FUNC_NAME_UUID, "common", "唯一标识符", "${java.uuid()}", "标准化的唯一标识符"), RANDOM_NUMBER(FUNC_NAME_RANDOM_NUMBER, "common", "随机数", "${java.randomNumber(2, 11)}", "生成指定范围的整型数字"); From 4107845edc2f85cf8fbcc861874674a564e4d966 Mon Sep 17 00:00:00 2001 From: dingpei Date: Fri, 1 Mar 2024 17:11:20 +0800 Subject: [PATCH 2/2] update --- .../main/java/run/mone/mock/MockJsUtils.java | 18 +---------------- .../java/run/mone/mock/test/MockTest.java | 20 +++++++++---------- 2 files changed, 11 insertions(+), 27 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 89b43c09e..71679e14b 100644 --- a/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java +++ b/jcommon/mockjs/src/main/java/run/mone/mock/MockJsUtils.java @@ -43,7 +43,7 @@ public static String mock(String template) { template = StringUtils.trimToEmpty(template); try { - String result = MOCK_JS_ENGINE.eval("JSON.stringify(Mock.mock(" + template + "))").toString(); + String result = MOCK_JS_ENGINE.eval("JSON.stringify(" + template + ")").toString(); return result; } catch (Throwable e) { log.error("执行Mock.mock错误", e); @@ -79,21 +79,5 @@ public static List batchOperation(List res, Supplier sup } } - public static String random(String template) { - template = StringUtils.trimToEmpty(template); - - try { - String result = MOCK_JS_ENGINE.eval("JSON.stringify(Mock." + template + ")").toString(); - return result; - } catch (Throwable e) { - log.error("执行Mock.mock错误", e); - } - - return null; - } - - public static List batchRandom(String template, int number) { - return batchOperation(new ArrayList<>(), () -> random(template), number); - } } \ No newline at end of file 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 17718de88..cd5d8f90f 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 @@ -11,9 +11,9 @@ public class MockTest { @Test public void mockjsTest() { - String input = "{\n" + - " \"string|1-10\": \"★\"\n" + - "}"; + String input = "Mock.mock({\n" + + " \"number|1-100.1-10\": 1\n" + + "})"; long begin = System.currentTimeMillis(); String output = MockJsUtils.mock(input); long costtime = System.currentTimeMillis() - begin; @@ -22,9 +22,9 @@ public void mockjsTest() { @Test public void mockjsTestBatch() { - String input = "{\n" + - " \"string|1-10\": \"★\"\n" + - "}"; + String input = "Mock.mock({\n" + + " \"number|1-100.1-10\": 1\n" + + "})"; long begin = System.currentTimeMillis(); List output = MockJsUtils.batchMock(input, 50000); long costtime = System.currentTimeMillis() - begin; @@ -33,18 +33,18 @@ public void mockjsTestBatch() { @Test public void mockjsTestRandom() { - String input = "Random.title(3, 5)"; + String input = "Mock.Random.title(3, 5)"; long begin = System.currentTimeMillis(); - String output = MockJsUtils.random(input); + String output = MockJsUtils.mock(input); long costtime = System.currentTimeMillis() - begin; System.out.println(output); } @Test public void mockjsTestRandomBatch() { - String input = "Random.title(3, 5)"; + String input = "Mock.Random.title(3, 5)"; long begin = System.currentTimeMillis(); - List output = MockJsUtils.batchRandom(input, 9999); + List output = MockJsUtils.batchMock(input, 9999); long costtime = System.currentTimeMillis() - begin; System.out.println(output); }