• jmh 微基准测试


    选择依据:对某段代码的性能测试。

    1.运行方法

    mvn clean install
     java -jar target/benchmarks.jar JMHSample_02 -f 1

    2.maven 配置

    <dependency>
                <groupId>org.openjdk.jmh</groupId>
                <artifactId>jmh-core</artifactId>
                <version>${jmh.version}</version>
            </dependency>
            <dependency>
                <groupId>org.openjdk.jmh</groupId>
                <artifactId>jmh-generator-annprocess</artifactId>
                <version>${jmh.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.7</version>
            </dependency>
            <!-- logback -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.0.11</version>
            </dependency>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <!-- JMH version to use with this project. -->
            <jmh.version>1.19</jmh.version>
    
            <!-- Java source/target to use for compilation. -->
            <javac.target>1.8</javac.target>
    
            <!-- Name of the benchmark Uber-JAR to generate. -->
            <uberjar.name>benchmarks</uberjar.name>
        </properties>

    3.实例类

    public class MyBenchmark {
        private static final Logger logger = LoggerFactory.getLogger(MyBenchmark.class);
        
        @Fork(value=2)
        @Benchmark
        public void testConcatenatingStrings() {
            String x = "", y = "", z = "";
    
            for (int i = 0; i < 100; i++) {
                x += i;
                y += i;
                z += i;
                logger.info("Concatenating strings " + x + y + z);
            }
        }
        @Fork(value=2)
        @Benchmark
        public void testVariableArguments() {
    
            String x = "", y = "", z = "";
    
            for (int i = 0; i < 100; i++) {
                x += i;
                y += i;
                z += i;
    
                logger.info("Concatenating strings {} {} {}", x, y, z);
            }
        }
        @Fork(value=2)
        @Benchmark
        public void testIfDebugEnabled() {
    
            String x = "", y = "", z = "";
    
            for (int i = 0; i < 100; i++) {
                x += i;
                y += i;
                z += i;
    
                if (logger.isInfoEnabled())
                    logger.info("Concatenating strings  {} {} {}", x, y, z);
            }
        }
    
    }

    实例2

    package org.agoncal.sample.jmh;
    
    import java.util.Date;
    import java.util.Random;
    
    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.BenchmarkMode;
    import org.openjdk.jmh.annotations.Fork;
    import org.openjdk.jmh.annotations.Mode;
    import org.openjdk.jmh.annotations.Param;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.Setup;
    import org.openjdk.jmh.annotations.State;
    import org.openjdk.jmh.infra.Blackhole;
    
    
    public class TestBenchMarks {
        public enum ChannelState {
            CONNECTED, DISCONNECTED, SENT, RECEIVED, CAUGHT
        }
    
        @State(Scope.Benchmark)
        public static class ExecutionPlan {
            @Param({ "1000000" })
            public int size;
            public ChannelState[] states = null;
    
            @Setup
            public void setUp() {
                ChannelState[] values = ChannelState.values();
                states = new ChannelState[size];
                Random random = new Random(new Date().getTime());
                for (int i = 0; i < size; i++) {
                    int nextInt = random.nextInt(1000000);
                    if (nextInt > 100) {
                        states[i] = ChannelState.RECEIVED;
                    } else {
                        states[i] = values[nextInt % values.length];
                    }
                }
            }
        }
    
        @Fork(value = 5)
        @Benchmark
        @BenchmarkMode(Mode.Throughput)
        public void benchSiwtch(ExecutionPlan plan, Blackhole bh) {
            int result = 0;
            for (int i = 0; i < plan.size; ++i) {
                switch (plan.states[i]) {
                case CONNECTED:
                    result += ChannelState.CONNECTED.ordinal();
                    break;
                case DISCONNECTED:
                    result += ChannelState.DISCONNECTED.ordinal();
                    break;
                case SENT:
                    result += ChannelState.SENT.ordinal();
                    break;
                case RECEIVED:
                    result += ChannelState.RECEIVED.ordinal();
                    break;
                case CAUGHT:
                    result += ChannelState.CAUGHT.ordinal();
                    break;
                }
            }
            bh.consume(result);
        }
    
        @Fork(value = 5)
        @Benchmark
        @BenchmarkMode(Mode.Throughput)
        public void benchIfAndSwitch(ExecutionPlan plan, Blackhole bh) {
            int result = 0;
            for (int i = 0; i < plan.size; ++i) {
                ChannelState state = plan.states[i];
                if (state == ChannelState.RECEIVED) {
                    result += ChannelState.RECEIVED.ordinal();
                } else {
                    switch (state) {
                    case CONNECTED:
                        result += ChannelState.CONNECTED.ordinal();
                        break;
                    case SENT:
                        result += ChannelState.SENT.ordinal();
                        break;
                    case DISCONNECTED:
                        result += ChannelState.DISCONNECTED.ordinal();
                        break;
                    case CAUGHT:
                        result += ChannelState.CAUGHT.ordinal();
                        break;
                    }
                }
            }
            bh.consume(result);
        }
    }

    代码见附件。

    https://files.cnblogs.com/files/z-test/logging.rar

  • 相关阅读:
    硬盘SSD、HDD和SSHD都是什么意思?哪种类型硬盘最好?
    记录vlookup的小问题
    找到两个字符串中相同的部分| 对字符串list后的奇妙发现
    jiayan:Cannot read model 'jiayan.klm' (utilfile.cc:74 in util::OpenReadOrThrow threw ErrnoException because `-1 == (ret = _open(name,
    windows下装kenlm
    每日一题力扣98 验证二叉搜索树
    每日一题力扣230 二叉搜索树中的第K小的元素
    每日一题力扣538 把二叉搜索树转换为累加树
    每日一题力扣530 二叉搜索树的最小绝对查
    每日一题力扣700 二叉搜索树中的搜索
  • 原文地址:https://www.cnblogs.com/z-test/p/9372568.html
Copyright © 2020-2023  润新知