paulwong

          JUnitBenchmark Junit性能測(cè)試

          如果你希望用 JUnit 來(lái)測(cè)試一些性能問(wèn)題,那么 JUnitBenchmark 可以幫到你,主要特性:

          • 記錄執(zhí)行時(shí)間
          • 監(jiān)控垃圾收集
          • 測(cè)試熱身

          package com.paul;

          import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
          import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
          import javolution.text.TextBuilder;
          import org.junit.Test;
           
          /**
           * Benchmark for String concatenation. Compares StringBUilder (JDK) and
           * TextBuilder (Javolution).
           
          */
          public class StringConcatenationBenchmark extends AbstractBenchmark {
           
              public static final long LOOPS_COUNT = 10000000;
           
              @Test
              @BenchmarkOptions(benchmarkRounds = 3, warmupRounds = 1)
              public void stringBuilderBenchmark()  {
                   
                  StringBuilder builder = new StringBuilder();
                  for (long i = 0; i < LOOPS_COUNT; i++) {
                      builder.append('i').append(i);
                  }
                  System.out.println(builder.toString().length());
              }
               
              @Test
              @BenchmarkOptions(benchmarkRounds = 3, warmupRounds = 1)
              public void textBuilderBenchmark()  {
                   
                  TextBuilder builder = new TextBuilder();
                  for (long i = 0; i < LOOPS_COUNT; i++) {
                      builder.append('i').append(i);
                  }
                  System.out.println(builder.toString().length());
              }
          }


          Maven依賴:
          <dependency>
                  <groupId>javolution</groupId>
                  <artifactId>javolution</artifactId>
                  <version>5.4.5</version>
          </dependency>


          結(jié)果顯示:
          78888890
          78888890
          78888890
          78888890
          StringConcatenationBenchmark.stringBuilderBenchmark: [measured 3 out of 4 rounds, threads: 1 (sequential)]
           round: 0.57 [+- 0.01], round.gc: 0.00 [+- 0.00], GC.calls: 33, GC.time: 0.02, time.total: 2.60, time.warmup: 0.90, time.bench: 1.70
          78888890
          78888890
          78888890
          78888890
          StringConcatenationBenchmark.textBuilderBenchmark: [measured 3 out of 4 rounds, threads: 1 (sequential)]
           round: 0.46 [+- 0.03], round.gc: 0.00 [+- 0.00], GC.calls: 14, GC.time: 0.14, time.total: 1.92, time.warmup: 0.55, time.bench: 1.38

          posted on 2013-03-01 10:37 paulwong 閱讀(786) 評(píng)論(0)  編輯  收藏 所屬分類: 性能優(yōu)化

          主站蜘蛛池模板: 宣化县| 汝南县| 龙门县| 都匀市| 丰宁| 丰顺县| 荔波县| 白玉县| 平泉县| 广饶县| 长兴县| 扶绥县| 泗水县| 杭锦后旗| 韩城市| 安化县| 射洪县| 进贤县| 巴林右旗| 潼关县| 桃源县| 凤城市| 丘北县| 邵东县| 广平县| 凤冈县| 黄石市| 镇巴县| 达拉特旗| 秭归县| 华池县| 南靖县| 安阳市| 普宁市| 彰化市| 海兴县| 武邑县| 昌黎县| 甘南县| 平潭县| 绥滨县|