paulwong

          JUnitBenchmark Junit性能測試

          如果你希望用 JUnit 來測試一些性能問題,那么 JUnitBenchmark 可以幫到你,主要特性:

          • 記錄執行時間
          • 監控垃圾收集
          • 測試熱身

          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>


          結果顯示:
          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 閱讀(789) 評論(0)  編輯  收藏 所屬分類: 性能優化

          主站蜘蛛池模板: 鄯善县| 阳新县| 双桥区| 张北县| 蛟河市| 方正县| 大姚县| 平舆县| 盘锦市| 凤凰县| 通化县| 三明市| 临沭县| 南部县| 柞水县| 哈巴河县| 溧水县| 淮北市| 九江市| 定襄县| 宝坻区| 民乐县| 富川| 临武县| 宜州市| 丰城市| 秦皇岛市| 陇西县| 刚察县| 丰县| 澎湖县| 南召县| 普洱| 胶州市| 独山县| 茌平县| 雷州市| 庆城县| 梨树县| 泰顺县| 舒城县|