How to use Caliper for benchmarking?

て烟熏妆下的殇ゞ 提交于 2020-01-14 08:02:08

问题


I am trying to figure out how to use Caliper to do benchmark testing in Eclipse and I am getting nowhere. I tried following the 26 minute tutorial found here: https://code.google.com/p/caliper/ but I get lost quickly. I have downloaded the Caliper jar file but I'm not sure what folder it should be in. I've also downloaded Maven for Eclipse plugin but I'm not even sure if that is necessary. Is it possible to install Caliper from the 'Install New Software..' option in the Help menu in Eclipse? I just want to be able to do very simple speed tests for some of the algorithms I've created for a Data Structures and Algorithms class I am taking.


回答1:


This answer is now obsolete. Caliper has worked in Windows for more than a year, at least: https://code.google.com/p/caliper/issues/detail?id=167


Caliper doesn't work in Windows. See this case. You need to use version 0.5-rc1, which has other issues but is still pretty okay and is missing a lot of features, but it does work in Windows.

  • If you know how to use Maven, add this pom snippet to your pom.xml.

    <dependency>
        <groupId>com.google.caliper</groupId>
        <artifactId>caliper</artifactId>
        <version>0.5-rc1</version>
    </dependency>
    
    • If you want to learn maven, first read this: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
    • Convert your project to a maven project (Right click on project -> Configure -> Convert to Maven Project)
  • If you don't know how to use Maven (here is a guide to how to do this with pictures):
    • Download the 0.5-rc1 jar
    • Right click on the project you want to use and choose Build Path -> Configure Build Path
    • Add it to your libraries tab using Add External Jar

Once you've done that, you can start writing benchmarks. Here is an example of a benchmark I wrote for a different Stack Overflow question.




回答2:


Here is how you set up a working Caliper class using the latest version of Caliper as of this writing, caliper-1.0-beta2 . As far as I can tell this procedure isn't documented anywhere outside of inline comments in the Caliper code files.

First install caliper-1.0-beta2 in pom.xml or by downloading the jar file. Then make a file like this:

import com.google.caliper.Benchmark;
import com.google.caliper.runner.CaliperMain;

public class DemoBenchmark {
  public static void main(String[] args) {
    CaliperMain.main(DemoBenchmark.class, args);
  }

  @Benchmark
  void timeStringBuilder(int reps) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < reps; i++) {
      sb.setLength(0);
      sb.append("hello world");
    }
  }
}

Run that file, and Caliper will do the benchmark for you.



来源:https://stackoverflow.com/questions/21341505/how-to-use-caliper-for-benchmarking

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!