我想为 android 编写简单的基准测试软件。我研究并找到了 Caliper + Vogar。但是没有关于它们的例子。有人用吗?我需要简单的代码来从 android Activity 调用 Caliper Benchmarks。
感谢帮助。
在Android Application Testing Guide中有一个用于 Android 的 Caliper Benchmark 的完整示例,但不是针对 Activity,它还解释了:
Caliper 基准测试通常扩展了实现 Benchmark 接口的 com.google.caliper.SimpleBenchmark。基准测试的结构与 JUnit 3 测试类似,并保持相同的结构,不同之处在于基准测试从前缀时间开始,而不是测试。
有问题的基准如下所示:
public final void timeCelsiusToFahrenheit(int reps) {
for (int i=0; i < reps; i++) {
for (double t: temps) {
TemperatureConverter.celsiusToFahrenheit(t);
}
}
}
最有趣的功能之一是caliper可以上传基准测试结果,您可以以图形方式比较它们
最近,我和你做同样的事情,我成功了
1.下载并配置构建aosp的环境
2.构建aosp
3.build /external/vogar,你会在out/host/linux-x86/bin/vogar下得到vogar
4.使用caliper编写你的测试代码
5.用vogar在主机上运行你的测试
vogar --mode app_process --benchmark
vogar 会自动将您的代码编译为 dex 文件并将它们推送到设备,
调用 app_process(这将调用注册了 android JNI 的 dalvikvm 命令)来运行 vogar 测试运行器来运行您的测试用例
vogar 需要您指定的类路径来编译或构建您的测试用例
以下是我运行 vogar 时的日志:
dvdface@ubuntu:~/Source/aosp/out/host/linux-x86/bin$ ./vogar --mode
app_process --benchmark
~/Source/aosp/libcore/benchmarks/src/benchmarks/AdditionBenchmark.java
行动:1 执行
[/home/dvdface/Source/aosp/prebuilts/sdk/tools/jack, -D,
jack.java.source.version=1.8,-D,jack.android.min-api-level=o-b1,
-cp,/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/core -oj_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/conscrypt_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES /okhttp_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/legacy-test_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj /JAVA_LIBRARIES/bouncycastle_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj /JAVA_LIBRARIES/framework_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/telephony-common_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/mms-common_intermediates/ classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/android。 policy_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/ apache-xml_intermediates/classes.jack:/home/dvdface/Source/aosp/out/host/linux-x86/framework/vogar.jar,/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/android.policy_intermediates/classes。 jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/apache-xml_intermediates/ classes.jack:/home/dvdface/Source/aosp/out/host/linux-x86/framework/vogar.jar,/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/android.policy_intermediates/classes。 jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/services_intermediates/classes.jack:/home/dvdface/Source/aosp/out/target/common/obj/JAVA_LIBRARIES/apache-xml_intermediates/ classes.jack:/home/dvdface/Source/aosp/out/host/linux-x86/framework/vogar.jar,/home/dvdface/Source/aosp/out/host/linux-x86/framework/vogar.jar,/home/dvdface/Source/aosp/out/host/linux-x86/framework/vogar.jar,
--输出插孔,/tmp/vogar/24a8baa0-30f1-4861-99c1-6660e97bf1d8/benchmarks.AdditionBenchmark/benchmarks.AdditionBenchmark.jack,
--import-resource, /tmp/vogar/24a8baa0-30f1-4861-99c1-6660e97bf1d8/benchmarks.AdditionBenchmarkexecuting
[亚行,外壳,mkdir,
/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark]
执行 [/home/dvdface/Source/aosp/prebuilts/sdk/tools/jack,
--output-dex-zip, /tmp/vogar/24a8baa0-30f1-4861-99c1-6660e97bf1d8/benchmarks.AdditionBenchmark/benchmarks.AdditionBenchmark.dex.jar,
-D,jack.java.source.version=1.8,-D,jack.android.min-api-level=o-b1,--multi-dex,本机,--import,/tmp/vogar/24a8baa0-30f1 -4861-99c1-6660e97bf1d8/benchmarks.AdditionBenchmark/benchmarks.AdditionBenchmark.jackAction
benchmarks.AdditionBenchmark 执行 [adb, shell, cd
/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark &&
ANDROID_DATA=/data/local/tmp/vogar app_process
-Djava.class.path=/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark.dex.jar
-Djava.io.tmpdir=/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark
-Duser.home=/data/local/tmp/vogar/run/user.home /data/local/tmp/vogar/run/benchmarks.AdditionBenchmark
vogar.target.TestRunn 实验选择:
Benchmark Methods: [timeAddConstantToLocalDouble, timeAddConstantToLocalFloat, timeAddConstantToLocalInt,
timeAddConstantToLocalLong, timeAddTwoLocalDoubles,
timeAddTwoLocalFloats, timeAddTwoLocalInts, timeAddTwoLocalLongs]
Instruments: [runtime] User parameters: {} Virtual machines: [app_process] Selection type: Full cartesian product This selection yields 8 experiments. Trial Report (1 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddConstantToLocalDouble, vm=app_process,
参数={}}
Results: runtime(ns): min=3.48, 1st qu.=3.48, median=3.48, mean=3.48, 3rd qu.=3.48, max=3.48 Trial Report (2 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddConstantToLocalFloat, vm=app_process,
参数={}}
Results: runtime(ns): min=5.28, 1st qu.=5.28, median=5.28, mean=5.28, 3rd qu.=5.28, max=5.28 Trial Report (3 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddConstantToLocalInt, vm=app_process,
参数={}}
Results: runtime(ns): min=2.11, 1st qu.=2.11, median=2.11, mean=2.11, 3rd qu.=2.11, max=2.11 Trial Report (4 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddConstantToLocalLong, vm=app_process,
参数={}}
Results: runtime(ns): min=2.10, 1st qu.=2.10, median=2.10, mean=2.10, 3rd qu.=2.10, max=2.10 Trial Report (5 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddTwoLocalDoubles, vm=app_process, parameters={}} Results: runtime(ns): min=5.31, 1st qu.=5.31, median=5.31, mean=5.31, 3rd qu.=5.31, max=5.31 Trial Report (6 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddTwoLocalFloats, vm=app_process, parameters={}} Results: runtime(ns): min=5.23, 1st qu.=5.23, median=5.23, mean=5.23, 3rd qu.=5.23, max=5.23 Trial Report (7 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddTwoLocalInts, vm=app_process, parameters={}} Results: runtime(ns): min=1.63, 1st qu.=1.63, median=1.63, mean=1.63, 3rd qu.=1.63, max=1.63 Trial Report (8 of 8): Experiment {instrument=runtime, benchmarkMethod=timeAddTwoLocalLongs, vm=app_process, parameters={}} Results: runtime(ns): min=2.14, 1st qu.=2.14, median=2.14, mean=2.14, 3rd qu.=2.14, max=2.14 Collected 8 measurements from: 1 instrument(s) 1 virtual machine(s) 8 benchmark(s) Execution complete: 19.90 s. Instrument com.google.caliper.runner.AllocationInstrument not supported on Dalvik, ignoring Instrument com.google.caliper.runner.AllocationInstrument not supported on Dalvik, ignoring Some trials failed to upload. Consider uploading them manually. benchmarks.AdditionBenchmark OK (SUCCESS) received exit value 0 from
破坏命令 adb shell cd
/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark &&
ANDROID_DATA=/data/local/tmp/vogar app_process
-Djava.class.path=/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark.dex.jar
-Djava.io.tmpdir=/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark
-Duser.home=/data/local/tmp/vogar/run/user.home /data/local/tmp/vogar/run/benchmarks.AdditioMoving
/data/local/tmp/vogar/run/benchmarks.AdditionBenchmark/benchmarks.AdditionBenchmark.2018-04-01T15:43:17Z.json
到 ./vogar-results 结果: 1. 全部成功。花了 10 分 35 秒。