使用Caliper时如何指定命令行? [英] How to specify the command line when using Caliper?

查看:103
本文介绍了使用Caliper时如何指定命令行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现Google的微型基准测试项目Caliper非常有趣,但该文档(除某些示例外)仍然不存在.

我有两种情况需要影响Caliper启动的JVM命令行:

  1. 我需要设置一些固定的(最好在几个固定值之间交替显示)-D参数
  2. 我需要指定一些固定的(最好是在几个固定值之间交替)JVM参数

我看到了有关添加这样的功能的讨论,但是我不能断定是否已添加它,在那种情况下语法变成了什么?

一些示例或指向Java doc的指针(假设这完全记录在某处)等

解决方案

要使用命令行参数修复基准参数,请使用-Dname=value.有一个名为benchmark的特殊参数.它的值是time方法的后缀.如果要将参数限制为多个值,请使用逗号分隔它们,如下所示:-Dname=value1,value2.

要设置JVM参数,请使用-Jname=flag1,flag2.

例如,考虑以下基准:

public class StringBuilderBenchmark extends SimpleBenchmark {

    @Param({"1", "10", "100"}) private int length;

    public void timeAppendBoolean(int reps) {
        for (int i = 0; i < reps; ++i) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < length; ++j) {
                sb.append(true);
            }
        }
    }

    public void timeAppendChar(int reps) {
        for (int i = 0; i < reps; ++i) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < length; ++j) {
                sb.append('c');
            }
        }
    }
}

要使用长度5和6以及大小不同的堆来运行此基准测试,请使用以下参数:

java -cp caliper-0.0.jar:build/classes/test \
    com.google.caliper.Runner examples.StringBuilderBenchmark \
    -Dlength=5,6 -Dbenchmark=AppendBoolean -Jmemory=-Xmx512M,-Xmx16M

这将产生以下内容:

 0% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=5, memory=-Xmx512M} 81.79 ns; σ=0.31 ns @ 3 trials
25% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=6, memory=-Xmx512M} 89.72 ns; σ=2.19 ns @ 10 trials
50% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=5, memory=-Xmx16M} 111.44 ns; σ=6.01 ns @ 10 trials
75% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=6, memory=-Xmx16M} 120.23 ns; σ=4.59 ns @ 10 trials

  memory length    ns logarithmic runtime
-Xmx512M      5  81.8 =
-Xmx512M      6  89.7 =======
 -Xmx16M      5 111.4 ========================
 -Xmx16M      6 120.2 =============================

vm: java
trial: 0
benchmark: AppendBoolean

I find Google's micro benchmark project Caliper very interesting but the documentation is still (except some examples) quite non-existent.

I have two different cases where I need to influence the command line of the JVMs Caliper starts:

  1. I need to set some fixed (ideally alternated between a few fixed values) -D parameters
  2. I need to specify some fixed (ideally alternated between a few fixed values) JVM parameters

I saw some discussion about adding features like this but I could not conclude if it has been added or not and in that case what the syntax became?

Some example or pointers into Java doc (assuming this is at all documented somewhere) etc would be very much appreciated!

解决方案

To fix a benchmark parameter with a command line argument, use -Dname=value. There is one special parameter named benchmark; it's values are the suffixes to your time methods. If you'd like to limit a parameter to multiple values, separate them by commas like this: -Dname=value1,value2.

To set JVM parameters, use -Jname=flag1,flag2.

For example consider this benchmark:

public class StringBuilderBenchmark extends SimpleBenchmark {

    @Param({"1", "10", "100"}) private int length;

    public void timeAppendBoolean(int reps) {
        for (int i = 0; i < reps; ++i) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < length; ++j) {
                sb.append(true);
            }
        }
    }

    public void timeAppendChar(int reps) {
        for (int i = 0; i < reps; ++i) {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < length; ++j) {
                sb.append('c');
            }
        }
    }
}

To run this benchmark with lengths 5 and 6, and large and small heaps, use these parameters:

java -cp caliper-0.0.jar:build/classes/test \
    com.google.caliper.Runner examples.StringBuilderBenchmark \
    -Dlength=5,6 -Dbenchmark=AppendBoolean -Jmemory=-Xmx512M,-Xmx16M

This yields the following:

 0% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=5, memory=-Xmx512M} 81.79 ns; σ=0.31 ns @ 3 trials
25% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=6, memory=-Xmx512M} 89.72 ns; σ=2.19 ns @ 10 trials
50% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=5, memory=-Xmx16M} 111.44 ns; σ=6.01 ns @ 10 trials
75% Scenario{vm=java, trial=0, benchmark=AppendBoolean, length=6, memory=-Xmx16M} 120.23 ns; σ=4.59 ns @ 10 trials

  memory length    ns logarithmic runtime
-Xmx512M      5  81.8 =
-Xmx512M      6  89.7 =======
 -Xmx16M      5 111.4 ========================
 -Xmx16M      6 120.2 =============================

vm: java
trial: 0
benchmark: AppendBoolean

这篇关于使用Caliper时如何指定命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆