jcstress 高并发测试框架使用教程

jcstress 高并发测试框架使用教程

1. 创建Maven项目

1.1 修改pom文件


    4.0.0
    com.jcstress.learning
    jcstress
    1.0
    jar
    JCStress learning sample

    

    
        3.0
    

    
        
            org.openjdk.jcstress
            jcstress-core
            ${jcstress.version}
        
        
            org.openjdk.jcstress
            jcstress-samples
            ${jcstress.version}
        
    

    
        UTF-8

        
        0.5

        
        8

        
        jcstress-learning
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.1
                
                    ${javac.target}
                    ${javac.target}
                    ${javac.target}
                
            
            
                org.apache.maven.plugins
                maven-clean-plugin
                3.1.0
                
                    
                        
                            ${basedir}/results
                            false
                        
                        
                            ${basedir}
                            
                                jcstress-results*
                            
                            false
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-shade-plugin
                2.2
                
                    
                        main
                        package
                        
                            shade
                        
                        
                            ${uberjar.name}
                            
                                
                                    org.openjdk.jcstress.Main
                                
                                
                                    META-INF/TestList
                                
                            
                        
                    
                
            
        
    



2. 创建测试类或者使用官方Demo

2.1 使用官方demo

官方demo下载地址, GitHub的demo示例

2.2 自己创建测试类

public class TestJMM {
        
    // 指定使用并发测试
    @JCStressTest
    // 预测的结果与类型,附加描述信息
    @Outcome(id = {"0, 1", "1, 0", "1, 1"}, expect = ACCEPTABLE, desc = "Trivial under sequential consistency")
    @Outcome(id = "0, 0", expect = ACCEPTABLE_INTERESTING, desc = "Violates sequential consistency")
    // 标注需要测试的类
    @State
    public static class PlainExecutionOrder {
        int x, y;
        int i, j;
                
        // 标记方法使用多线程
        @Actor
        public void actor1(II_Result r) {
            x = 1;
            r.r2 = y;
        }

        @Actor
        public void actor2(II_Result r) {
            y = 1;
            r.r1 = x;
        }

    }
}

3 打包并启动

必须使用mvn clean install打包成jar包否则会缺失文件无法运行

3.1 启动命令

java -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-RestrictContended -jar target/jcstress-learning.jar -v -t TestJMM.PlainExecutionOrder

参考文章:

https://www.jianshu.com/p/aa0e7d0bf158

https://wiki.openjdk.java.net/display/CodeTools/jcstress

https://github.com/jwoschitz/jcstress-examples

https://github.com/jerzykrlk/jcstress-gradle-plugin

https://stackoverflow.com/questions/53458367/running-first-jcstress-test

你可能感兴趣的:(jcstress 高并发测试框架使用教程)