使用JMH压测条件表达式性能

对于解析json表达式 groovy+spring el+aviator+mvel2压测性能比较,最终选择aviator

下面只保留aviator,主要是学会jmh的使用

最终结论

el各项性能远小于其他表达式,不予考虑

入参map的复杂度对groovy影响较大,map简单时,groovytaviator、mel快,入参较多时(实际生产场累),由于groovy解析入参占用时间较长,明显就比aviator,mel慢了

aviator, mvel难分伯仲,在实际场景用的较多的“==”运算场景和substring+'=='"运算场景,awiator略微更快些,并发方面aviator略微高于mvel;在contains函数处理方面mvel并发高于aviator

        
            org.openjdk.jmh
            jmh-core
            1.36
        
        
            org.openjdk.jmh
            jmh-generator-annprocess
            1.36
        
        
            com.googlecode.aviator
            aviator
            5.3.3
        

单线程

package com.ruoyi.common.utils;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.results.format.ResultFormatType;
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 java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5,time = 5)
@Threads(1)
@Fork(1)
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class StringConnectTestSingleThread {

    private Expression aviatorCompile;

    @Param(value = "string.substring(a.b.d) == '5'")
    private String aviatorExpression;

    private Map env;

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(StringConnectTestSingleThread.class.getSimpleName())
                .result("result.json")
                .resultFormat(ResultFormatType.JSON).build();
        new Runner(opt).run();
    }

    @Setup
    public void setup() throws InstantiationException,IllegalAccessException{
        aviatorCompile = AviatorEvaluator.compile(aviatorExpression);
        env = new HashMap<>();
        Map env1 = new HashMap<>();
        Map env2 = new HashMap<>();
        env.put("a",1);
        env.put("b",env1);
        env.put("s","hello world");
        env1.put("c",env2);
        env1.put("d",5);
        env2.put("e",4);
        System.out.println("env=="+env);
    }

    @Benchmark
    public void testAviatorFunction(Blackhole blackhole){
        Object result = aviatorCompile.execute(env);
        blackhole.consume(result);
    }



}

多线程

package com.ruoyi.common.utils;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.results.format.ResultFormatType;
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 java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5,time = 5)
@Threads(16)
@Fork(1)
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
public class StringConnectTestSingleThread {

    private Expression aviatorCompile;

    @Param(value = "string.substring(a.b.d) == '5'")
    private String aviatorExpression;

    private Map env;

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(StringConnectTestSingleThread.class.getSimpleName())
                .result("result.json")
                .resultFormat(ResultFormatType.JSON).build();
        new Runner(opt).run();
    }

    @Setup
    public void setup() throws InstantiationException,IllegalAccessException{
        aviatorCompile = AviatorEvaluator.compile(aviatorExpression);
        env = new HashMap<>();
        Map env1 = new HashMap<>();
        Map env2 = new HashMap<>();
        env.put("a",1);
        env.put("b",env1);
        env.put("s","hello world");
        env1.put("c",env2);
        env1.put("d",5);
        env2.put("e",4);
        System.out.println("env=="+env);
    }

    @Benchmark
    public void testAviatorFunction(Blackhole blackhole){
        Object result = aviatorCompile.execute(env);
        blackhole.consume(result);
    }



}

你可能感兴趣的:(其它,压力测试,java)