增加benchmark的工程测试

This commit is contained in:
everywhere.z
2024-09-30 17:42:06 +08:00
parent 857a6d6eac
commit 4f0d983c84
9 changed files with 222 additions and 48 deletions

View File

@@ -12,4 +12,13 @@
<artifactId>liteflow-benchmark-common</artifactId> <artifactId>liteflow-benchmark-common</artifactId>
<dependencies>
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>liteflow-script-javax</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View File

@@ -0,0 +1,67 @@
package com.yomahub.liteflow.benchmark;
import cn.hutool.core.io.resource.ResourceUtil;
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.FlowBus;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@EnableAutoConfiguration
@PropertySource(value = "classpath:application.properties")
@ComponentScan("com.yomahub.liteflow.benchmark.cmp")
public class CommonBenchmark {
private ConfigurableApplicationContext applicationContext;
private FlowExecutor flowExecutor;
@Setup
public void setup() {
applicationContext = SpringApplication.run(CommonBenchmark.class);
flowExecutor = applicationContext.getBean(FlowExecutor.class);
}
@TearDown
public void tearDown() {
applicationContext.close();
}
@Benchmark
public void test1(){
flowExecutor.execute2Resp("chain1");
}
@Benchmark
public void test2(){
flowExecutor.execute2Resp("chain2");
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(CommonBenchmark.class.getSimpleName())
.mode(Mode.Throughput)
.warmupIterations(1)//预热次数
.measurementIterations(3)//执行次数
.measurementTime(new TimeValue(10, TimeUnit.SECONDS))//每次执行多少时间
.threads(100)//多少个线程
.forks(1)//多少个进程
.timeUnit(TimeUnit.SECONDS)
.build();
new Runner(opt).run();
}
}

View File

@@ -0,0 +1,20 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.benchmark.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
}
}

View File

@@ -0,0 +1,20 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.benchmark.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
}
}

View File

@@ -0,0 +1,20 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.benchmark.cmp;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
}
}

View File

@@ -0,0 +1,28 @@
package com.yomahub.liteflow.benchmark.cmp;
public class Person {
private String name;
private Integer salary;
public Person(String name, Integer salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
}

View File

@@ -0,0 +1,36 @@
package com.yomahub.liteflow.benchmark.test;
import cn.hutool.core.io.resource.ResourceUtil;
import com.yomahub.liteflow.builder.LiteFlowNodeBuilder;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.FlowBus;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.yomahub.liteflow.slot.DefaultContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
@ExtendWith(SpringExtension.class)
@TestPropertySource(value = "classpath:application.properties")
@SpringBootTest(classes = CommonTest.class)
@EnableAutoConfiguration
@ComponentScan({ "com.yomahub.liteflow.benchmark.cmp" })
public class CommonTest {
@Resource
private FlowExecutor flowExecutor;
@Test
public void test1() {
flowExecutor.execute2Resp("chain1");
}
}

View File

@@ -2,54 +2,32 @@
<!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd"> <!DOCTYPE flow PUBLIC "liteflow" "liteflow.dtd">
<flow> <flow>
<nodes> <nodes>
<node id="s1" name="普通脚本1" type="script" language="groovy"> <node id="s1" name="普通脚本1" type="script" language="java">
<![CDATA[ <![CDATA[
import cn.hutool.core.collection.ListUtil import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil import com.yomahub.liteflow.benchmark.cmp.Person;
import com.yomahub.liteflow.script.body.CommonScriptBody;
import com.yomahub.liteflow.slot.DefaultContext;
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
import com.yomahub.liteflow.script.ScriptExecuteWrap;
import java.util.function.Consumer import java.util.List;
import java.util.function.Function import java.util.function.ToIntFunction;
import java.util.stream.Collectors
def date = DateUtil.parse("2022-10-17 13:31:43") public class Demo implements CommonScriptBody {
defaultContext.setData("demoDate", date) public Void body(ScriptExecuteWrap wrap) {
return null;
List<String> list = ListUtil.toList("a", "b", "c") }
List<String> resultList = list.stream().map(s -> "hello," + s).collect(Collectors.toList())
defaultContext.setData("resultList", resultList)
class Student {
int studentID
String studentName
} }
Student student = new Student()
student.studentID = 100301
student.studentName = "张三"
defaultContext.setData("student", student)
def a = 3
def b = 2
defaultContext.setData("s1", a * b)
]]>
</node>
<node id="s2" name="循环脚本1" type="for_script" language="groovy">
<![CDATA[
return 2
]]>
</node>
<node id="s3" name="选择脚本" type="switch_script" language="groovy">
<![CDATA[
return "b"
]]> ]]>
</node> </node>
</nodes> </nodes>
<chain name="chain1"> <chain name="chain1">
THEN(FOR(s2).DO(THEN(a, b, c, s1)), SWITCH(s3).TO(a,b)); THEN(a,b,c);
</chain>
<chain name="chain2">
THEN(a,b,s1);
</chain> </chain>
</flow> </flow>

View File

@@ -79,14 +79,10 @@ public class JavaxExecutor extends ScriptExecutor {
@Override @Override
public Object compile(String script) throws Exception { public Object compile(String script) throws Exception {
try{ CodeSpec codeSpec = new CodeSpec(convertScript(script))
CodeSpec codeSpec = new CodeSpec(convertScript(script)) .returnType(Object.class)
.returnType(Object.class) .parameters(new ParamSpec("_meta", ScriptExecuteWrap.class)).cached(isCache);
.parameters(new ParamSpec("_meta", ScriptExecuteWrap.class)).cached(isCache); return Scripts.compile(codeSpec);
return Scripts.compile(codeSpec);
}catch (Exception e){
return e;
}
} }
private String convertScript(String script){ private String convertScript(String script){