use

During the test, parameters are initialized once per thread. Since three data are configured for CT parameters, the test will be performed three times, respectively for each parameter. The following demo is 1 thread.

During each test, a JMH thread is started, warmup is performed twice, and 5s is executed in each iteration, that is, 5s check is executed in each iteration.

Fork(1) means several processes per test, equivalent to the above test logic being run several times.

The State annotation is because the demo uses the @setup annotation: scope. Benchmark to indicate that the @setup initialization parameter should be shared by all JMH worker threads in this test.

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 2, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 5, timeUnit = TimeUnit.SECONDS)
@Threads(1)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class Jmh {

    ExecutorService executor;

    @Param({"10000", "100000", "1000000"})
    int ct;

    AtomicInteger count;


    @Benchmark
    public void noCheck(a) {
        System.setProperty("check"."false");
        test();
    }

    @Benchmark
    public void check(a) {
        System.setProperty("check"."true");
        test();
    }

    private void test(a) {
        final CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run(a) {
                    while(! Thread.currentThread().isInterrupted()) {if (count.incrementAndGet() >= ct) {
                            break; } } countDownLatch.countDown(); }}); }try {
            // this is an await method, not await method. 😓
            countDownLatch.await();
        } catch(InterruptedException e) { e.printStackTrace(); }}@Setup
    public void prepare(a) {
        mscExecutor = Executors.newFixedThreadPool(10);
        count = new AtomicInteger(0);
        System.out.println("ct: " + ct);
    }

    @TearDown
    public void shutdown(a) {
        executor.shutdownNow();
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(Jmh.class.getSimpleName())
                .build();

        Collection<RunResult> results =  new Runner(opt).run();

        try {
            ResultExporter.exportResult("executor test", results, "ct"."ops/ms");
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Generate images

You can use the Github address to export images.

reference

-Java microbenchmark framework JMH

– Integrate Echarts to export images

– Github address of the exported image

-Java Compares the performance of several common JSON libraries