diff --git a/example/example-app/example-app-cmd-domain/pom.xml b/example/example-app/example-app-cmd-domain/pom.xml index b2ed6e26..c0698a4c 100644 --- a/example/example-app/example-app-cmd-domain/pom.xml +++ b/example/example-app/example-app-cmd-domain/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-app/example-app-cmd-meta/pom.xml b/example/example-app/example-app-cmd-meta/pom.xml index 891ead1b..66f5138c 100644 --- a/example/example-app/example-app-cmd-meta/pom.xml +++ b/example/example-app/example-app-cmd-meta/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-app/example-app-query/pom.xml b/example/example-app/example-app-query/pom.xml index b76513f0..ac105c9b 100644 --- a/example/example-app/example-app-query/pom.xml +++ b/example/example-app/example-app-query/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-app/pom.xml b/example/example-app/pom.xml index bb03aa71..f0841b3e 100644 --- a/example/example-app/pom.xml +++ b/example/example-app/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.43 + 3.4.44 ../pom.xml pom diff --git a/example/example-domain/example-domain-leave/pom.xml b/example/example-domain/example-domain-leave/pom.xml index 9a7eb891..feca9e30 100644 --- a/example/example-domain/example-domain-leave/pom.xml +++ b/example/example-domain/example-domain-leave/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-domain - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-domain/example-domain-user/pom.xml b/example/example-domain/example-domain-user/pom.xml index 47ec402a..c03892c6 100644 --- a/example/example-domain/example-domain-user/pom.xml +++ b/example/example-domain/example-domain-user/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-domain - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index 7c688f3f..64b78ab4 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-example - 3.4.43 + 3.4.44 ../pom.xml 4.0.0 diff --git a/example/example-infra/example-infra-flow/pom.xml b/example/example-infra/example-infra-flow/pom.xml index 884ef1bc..7c6f1875 100644 --- a/example/example-infra/example-infra-flow/pom.xml +++ b/example/example-infra/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot example-infra - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-infra/example-infra-jpa/pom.xml b/example/example-infra/example-infra-jpa/pom.xml index 067db0c4..2d561e57 100644 --- a/example/example-infra/example-infra-jpa/pom.xml +++ b/example/example-infra/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot example-infra - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-infra/example-infra-security/pom.xml b/example/example-infra/example-infra-security/pom.xml index bfdad5a6..f1a1c3c2 100644 --- a/example/example-infra/example-infra-security/pom.xml +++ b/example/example-infra/example-infra-security/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-infra - 3.4.43 + 3.4.44 ../pom.xml diff --git a/example/example-infra/pom.xml b/example/example-infra/pom.xml index 36678a2c..36605591 100644 --- a/example/example-infra/pom.xml +++ b/example/example-infra/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.43 + 3.4.44 ../pom.xml pom diff --git a/example/example-interface/pom.xml b/example/example-interface/pom.xml index 2d1acfe1..5f23d1e0 100644 --- a/example/example-interface/pom.xml +++ b/example/example-interface/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.43 + 3.4.44 example-interface diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index 1bbbd6c1..e6a43cd7 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.4.43 + 3.4.44 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index 34595dcf..f73e7ee6 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -19,7 +19,7 @@ springboot-example - 3.4.43 + 3.4.44 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index 89da7a7a..2fc26ce5 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.4.43 + 3.4.44 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-authorization/pom.xml b/springboot-starter-data-authorization/pom.xml index 863ba9bc..d89f6d06 100644 --- a/springboot-starter-data-authorization/pom.xml +++ b/springboot-starter-data-authorization/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-parent - 3.4.43 + 3.4.44 springboot-starter-data-authorization diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 9d526806..9f44ba99 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.4.43 + 3.4.44 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index 0b0c96b3..ef49d453 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.4.43 + 3.4.44 springboot-starter-flow diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/domain/FlowButton.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/domain/FlowButton.java index 7dd5d8e5..533c55a4 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/domain/FlowButton.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/domain/FlowButton.java @@ -76,8 +76,7 @@ public MessageResult run(FlowRecord flowRecord, if (groovy != null) { //执行脚本 FlowSession session = new FlowSession(flowRecord, flowWork, flowNode, createOperator, currentOperator, bindData, opinion, historyRecords); - GroovyShellContext.ShellScript script = GroovyShellContext.getInstance().parse(groovy); - return (MessageResult) script.invokeMethod("run", session); + return GroovyShellContext.getInstance().run(groovy,MessageResult.class,session); } return null; } diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/error/ErrTrigger.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/error/ErrTrigger.java index fbdd6751..e59b9709 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/error/ErrTrigger.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/error/ErrTrigger.java @@ -13,15 +13,11 @@ public class ErrTrigger { @Getter private final String script; - private final GroovyShellContext.ShellScript runtime; - - public ErrTrigger(String script) { if (!StringUtils.hasLength(script)) { throw new IllegalArgumentException("script is empty"); } this.script = script; - this.runtime = GroovyShellContext.getInstance().parse(script); } /** @@ -30,7 +26,7 @@ public ErrTrigger(String script) { * @param flowSession 流程内容 */ public ErrorResult trigger(FlowSession flowSession) { - return (ErrorResult) runtime.invokeMethod("run", flowSession); + return GroovyShellContext.getInstance().run(script, ErrorResult.class, flowSession); } } diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/generator/TitleGenerator.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/generator/TitleGenerator.java index 2fa5f840..fd667f01 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/generator/TitleGenerator.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/generator/TitleGenerator.java @@ -13,14 +13,12 @@ public class TitleGenerator { @Getter private final String script; - private final GroovyShellContext.ShellScript runtime; public TitleGenerator(String script) { if (!StringUtils.hasLength(script)) { throw new IllegalArgumentException("script is empty"); } this.script = script; - this.runtime = GroovyShellContext.getInstance().parse(script); } @@ -41,7 +39,7 @@ public static TitleGenerator defaultTitleGenerator() { * @return 标题 */ public String generate(FlowSession flowSession) { - return (String) this.runtime.invokeMethod("run", flowSession); + return GroovyShellContext.getInstance().run(script,String.class,flowSession); } } diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/matcher/OperatorMatcher.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/matcher/OperatorMatcher.java index 522f5c8f..b8ecf59e 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/matcher/OperatorMatcher.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/matcher/OperatorMatcher.java @@ -20,8 +20,6 @@ public class OperatorMatcher { private final int state; - private final GroovyShellContext.ShellScript runtime; - // 指定用户 public static final int STATE_SPECIFY = 1; // 创建者 @@ -63,7 +61,6 @@ public OperatorMatcher(String script, int state) { } this.script = script; this.state = state; - this.runtime = GroovyShellContext.getInstance().parse(script); } /** @@ -114,7 +111,7 @@ public static OperatorMatcher creatorOperatorMatcher() { * @return 是否匹配 */ public List matcher(FlowSession flowSession) { - List values = (List) runtime.invokeMethod("run", flowSession); + List values = (List) GroovyShellContext.getInstance().run(script,List.class, flowSession); if (values == null) { return new ArrayList<>(); } diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java index 356fbb6e..8381bddc 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java @@ -1,87 +1,35 @@ package com.codingapi.springboot.flow.script; -import com.codingapi.springboot.flow.utils.Sha256Utils; -import groovy.lang.GroovyShell; -import groovy.lang.Script; +import com.codingapi.springboot.framework.script.GroovyScriptRunningContext; import lombok.Getter; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - public class GroovyShellContext { - @Getter - private final static GroovyShellContext instance = new GroovyShellContext(); - - private final static GroovyShell groovyShell = new GroovyShell(); - - private final Map cache = new HashMap<>(); - - private final static ExecutorService threadPool = Executors.newFixedThreadPool(10); - // 缓存最大值 private final static int MAX_CACHE_SIZE = 10000; + @Getter + private final static GroovyShellContext instance = new GroovyShellContext(); - private GroovyShellContext() { - } - public ShellScript parse(String script) { - String hash = Sha256Utils.generateSHA256(script); - if (cache.containsKey(hash)) { - return cache.get(hash); - } else { - if (cache.size() > MAX_CACHE_SIZE) { - cache.clear(); - } - ShellScript shellScript = new ShellScript(script); - threadPool.submit(shellScript); - cache.put(hash, shellScript); - return shellScript; + private GroovyShellContext() { + if(GroovyScriptRunningContext.getInstance().getMaxCacheSize()!=MAX_CACHE_SIZE) { + GroovyScriptRunningContext.getInstance().setMaxCacheSize(MAX_CACHE_SIZE); } } - public int size() { - return cache.size(); + public T run(String script, Class returnType, Object... args) { + return GroovyScriptRunningContext.getInstance().run("run", script, returnType, args); } - public static class ShellScript implements Runnable { - - @Getter - private final String script; - - private Script runtime; - - public ShellScript(String script) { - this.script = script; - } - - public Object invokeMethod(String run, Object params) { - synchronized (ShellScript.this) { - if (runtime == null) { - try { - ShellScript.this.wait(1000); - } catch (InterruptedException ignored) { - } - if (runtime == null) { - runtime = groovyShell.parse(script); - } - } - return runtime.invokeMethod(run, params); - } - } - @Override - public void run() { - runtime = groovyShell.parse(script); - synchronized (ShellScript.this) { - this.notifyAll(); - } - } + public int size(){ + return GroovyScriptRunningContext.getInstance().cacheSize(); } + public void clear(){ + GroovyScriptRunningContext.getInstance().clearCache(); + } } diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/trigger/OutTrigger.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/trigger/OutTrigger.java index b7281495..3883b5f2 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/trigger/OutTrigger.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/trigger/OutTrigger.java @@ -13,14 +13,12 @@ public class OutTrigger { @Getter private final String script; - private final GroovyShellContext.ShellScript runtime; public OutTrigger(String script) { if (!StringUtils.hasLength(script)) { throw new IllegalArgumentException("script is empty"); } this.script = script; - this.runtime = GroovyShellContext.getInstance().parse(script); } /** @@ -38,7 +36,7 @@ public static OutTrigger defaultOutTrigger() { * @return true 进入下一节点,false 则返回上一节点 */ public boolean trigger(FlowSession flowSession) { - return (Boolean) runtime.invokeMethod("run", flowSession); + return GroovyShellContext.getInstance().run(script, Boolean.class, flowSession); } } diff --git a/springboot-starter-flow/src/test/java/com/codingapi/springboot/flow/script/GroovyShellContextTest.java b/springboot-starter-flow/src/test/java/com/codingapi/springboot/flow/script/GroovyShellContextTest.java index 80f655fd..a0b0993c 100644 --- a/springboot-starter-flow/src/test/java/com/codingapi/springboot/flow/script/GroovyShellContextTest.java +++ b/springboot-starter-flow/src/test/java/com/codingapi/springboot/flow/script/GroovyShellContextTest.java @@ -10,21 +10,15 @@ class GroovyShellContextTest { void getInstance() { long t1 = System.currentTimeMillis(); int count = 12000; - GroovyShellContext.ShellScript[] scripts = new GroovyShellContext.ShellScript[count]; for (int i = 0; i < count; i++) { - scripts[i] = GroovyShellContext.getInstance().parse("def run(content){ return '" + i + "';}"); + String result = GroovyShellContext.getInstance().run("def run(content){ return '" + i + "';}",String.class,i); + assertEquals(result, String.valueOf(i)); } long t2 = System.currentTimeMillis(); - System.out.println("t2 time :" + (t2 - t1)); + System.out.println("time :" + (t2 - t1)); System.out.println("size:" + GroovyShellContext.getInstance().size()); - for (int i = 0; i < count; i++) { - assertEquals(scripts[i].invokeMethod("run", i), String.valueOf(i)); - } - - long t3 = System.currentTimeMillis(); - System.out.println("t3 time :" + (t3 - t2)); } } diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index e96320c3..761cdbac 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.4.43 + 3.4.44 springboot-starter-security diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index b0fe8b6d..8181bd0d 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.4.43 + 3.4.44 springboot-starter @@ -97,6 +97,22 @@ test + + org.apache.groovy + groovy + + + + org.apache.groovy + groovy-json + + + + org.apache.groovy + groovy-xml + + + diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/SHA256.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/SHA256.java new file mode 100644 index 00000000..7e653af3 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/SHA256.java @@ -0,0 +1,23 @@ +package com.codingapi.springboot.framework.crypto; + +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HexFormat; + +@Slf4j +public class SHA256 { + + public static String sha256(String data) { + try { + byte[] digest = MessageDigest.getInstance("SHA-256") + .digest(data.getBytes(StandardCharsets.UTF_8)); + return HexFormat.of().formatHex(digest); + } catch (NoSuchAlgorithmException exception) { + log.error("sha256 error:", exception); + return data; + } + } +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java new file mode 100644 index 00000000..f430dcb1 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java @@ -0,0 +1,140 @@ +package com.codingapi.springboot.framework.script; + +import com.codingapi.springboot.framework.crypto.SHA256; +import com.codingapi.springboot.framework.script.request.RuntimeBindObject; +import com.codingapi.springboot.framework.script.request.GroovyMethodScript; +import groovy.lang.GroovyClassLoader; +import groovy.lang.GroovyShell; +import groovy.lang.Script; +import lombok.Getter; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * groovy script 执行上下文对象 + */ +public class GroovyScriptRunningContext { + + private final GroovyShell shell; + + private final Map cache; + + @Getter + private final int maxCacheSize; + + public static final int MAX_CACHE_SIZE = 1000; + + @Getter + private static GroovyScriptRunningContext instance = new GroovyScriptRunningContext(MAX_CACHE_SIZE); + + private GroovyScriptRunningContext(int maxCacheSize) { + this.maxCacheSize = maxCacheSize; + GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader()); + this.shell = new GroovyShell(groovyClassLoader); + this.cache = new LinkedHashMap<>(16, 0.75F, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > maxCacheSize; + } + }; + } + + /** + * 更新最大的缓存大小 + */ + public void setMaxCacheSize(int maxCacheSize) { + instance = new GroovyScriptRunningContext(maxCacheSize); + } + + /** + * 清空缓存对象 + */ + public void clearCache() { + this.cache.clear(); + } + + /** + * 缓存大小 + */ + public int cacheSize(){ + return this.cache.size(); + } + + /** + * 编译脚本 + * + * @param script 脚本内容 + * @param cache 是否缓存 + */ + public void compile(String script, boolean cache) { + if (cache) { + String key = SHA256.sha256(script); + Script runtime = this.cache.get(key); + if (runtime == null) { + runtime = this.shell.parse(script); + this.cache.put(key, runtime); + } + } else { + this.shell.parse(script); + } + } + + + /** + * 编译脚本,非缓存模式 + * @param script 脚本内容 + */ + public void compile(String script) { + this.compile(script, false); + } + + /** + * 执行脚本 + * @param method 函数名称 + * @param script 脚本内容 + * @param returnType 返回类型 + * @param binds 绑定数据对象 + * @param args 函数参数 + * @return 返回数据 + */ + public T run(String method, String script, Class returnType, List binds, Object... args) { + String key = SHA256.sha256(script); + Script runtime = this.cache.get(key); + if (runtime == null) { + runtime = this.shell.parse(script); + this.cache.put(key, runtime); + } + + if (binds != null && !binds.isEmpty()) { + for (RuntimeBindObject runtimeBindObject : binds) { + runtime.setProperty(runtimeBindObject.getKey(), runtimeBindObject.getObject()); + } + } + return (T) runtime.invokeMethod(method, args); + } + + /** + * 运行脚本 + * @param method 函数名称 + * @param script 脚本内容 + * @param returnType 返回类型 + * @param args 函数参数 + * @return 返回数据 + */ + public T run(String method, String script, Class returnType, Object... args) { + return this.run(method,script,returnType,null,args); + } + + + /** + * 执行脚本 + * @param request 脚本参数 + * @return 返回数据 + */ + public T run(GroovyMethodScript request) { + return this.run(request.getMethod(),request.getScript(),request.getReturnType(),request.getBinds(),request.getParams()); + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java new file mode 100644 index 00000000..3c92807a --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java @@ -0,0 +1,59 @@ +package com.codingapi.springboot.framework.script.request; + +import lombok.Getter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 脚本执行对象 + * + * @param 返回格式 + */ +@Getter +public class GroovyMethodScript { + + private final String method; + + private final String script; + + private final Class returnType; + + private final List request; + + private final List binds; + + public GroovyMethodScript(String method, String script, Class returnType, Object... request) { + this.method = method; + this.script = script; + this.returnType = returnType; + this.request = new ArrayList<>(); + this.binds = new ArrayList<>(); + if (request != null) { + this.request.addAll(Arrays.asList(request)); + } + } + + public GroovyMethodScript(String script, Class returnType, Object... request) { + this("run", script, returnType, request); + } + + /** + * 获取参数 + */ + public Object[] getParams() { + return this.request.toArray(); + } + + /** + * 添加绑定对象 + * @param key 绑定key + * @param bind 绑定对象 + */ + public void addBindObject(String key, Object bind) { + this.binds.add(new RuntimeBindObject(key, bind)); + } + + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java new file mode 100644 index 00000000..8797b1ba --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java @@ -0,0 +1,15 @@ +package com.codingapi.springboot.framework.script.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 运行时绑定对象 + */ +@Getter +@AllArgsConstructor +public class RuntimeBindObject { + + private String key; + private Object object; +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/crypto/HmacSHA256Test.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/crypto/HmacSHA256Test.java index 404bab95..be274cf4 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/crypto/HmacSHA256Test.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/crypto/HmacSHA256Test.java @@ -12,7 +12,10 @@ class HmacSHA256Test { @Test void sha256() throws NoSuchAlgorithmException, InvalidKeyException { - String data = Base64.getEncoder().encodeToString(HmacSHA256.sha256("123456".getBytes(),"123456".getBytes())); - assertEquals("uK0Io6VH41gpuCG3U3AwHdjEsGvdd3H5tUGnWRQGhxg=",data); + long t1 = System.currentTimeMillis(); + String data = Base64.getEncoder().encodeToString(HmacSHA256.sha256("123456".getBytes(), "123456".getBytes())); + assertEquals("uK0Io6VH41gpuCG3U3AwHdjEsGvdd3H5tUGnWRQGhxg=", data); + long t2 = System.currentTimeMillis(); + System.out.println(t2 - t1); } } \ No newline at end of file diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java new file mode 100644 index 00000000..7745a60e --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java @@ -0,0 +1,63 @@ +package com.codingapi.springboot.framework.script; + +import com.codingapi.springboot.framework.script.request.GroovyMethodScript; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class GroovyScriptRunningContextTest { + + @Test + void voidRun() { + + String script = """ + def run(request){ + println($request.getRequest()) + } + """; + + GroovyScriptRunningContext.getInstance().compile(script); + + GroovyMethodScript request = new GroovyMethodScript<>(script, Void.class, 100); + request.addBindObject("$request", request); + + long t1 = System.currentTimeMillis(); + GroovyScriptRunningContext.getInstance().run(request); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + + } + + @Test + void run() { + + String script = """ + def run(request){ + println($request.getRequest()) + return request; + } + """; + + GroovyMethodScript request = new GroovyMethodScript<>(script, Integer.class, 100); + request.addBindObject("$request", request); + + long t1 = System.currentTimeMillis(); + int result = GroovyScriptRunningContext.getInstance().run(request); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + + assertEquals(100, result); + } + + @Test + void batchTest() { + int count = 100; + run(); + long t1 = System.currentTimeMillis(); + for (int i = 0; i < count; i++) { + run(); + } + long t2 = System.currentTimeMillis(); + System.out.println("total time:" + (t2 - t1)); + } +} \ No newline at end of file