我正在尝试使用Scalameter对计算运行中值的两种不同实现进行微基准测试。我有一些大大小小的测试文件,这些文件的数字来自于这些文件。问题是,下面的代码在没有生成任何基准的情况下立即完成。
object MedianMaintenanceBenchmark extends Bench[Double] {
/* configuration */
lazy val executor = LocalExecutor(
new Warmer.Default,
Aggregator.median[Double],
measurer
)
lazy val measurer = new Measurer.Default
lazy val reporter = new LoggingReporter[Double]
lazy val persistor: Persistor.None.type = Persistor.None
/* inputs */
private val files: Gen[String] = Gen.enumeration("files")("median-test")
private val num: Gen[Seq[Int]] = (for (f <- files) yield numbers(f)).cached
/* tests */
performance of "MedianMaintenance" config (
exec.benchRuns -> 10
) in {
measure method "using heap" in {
using(num) in {
xs => MedianMaintenanceUsingHeaps(xs).medians
}
}
}
private def numbers(filename: String): Seq[Int] = // elided
}
输出:
::Benchmark MedianMaintenance.using heap::
cores: 8
hostname: ***
name: OpenJDK 64-Bit Server VM
osArch: x86_64
osName: Mac OS X
vendor: Azul Systems, Inc.
version: 11.0.1+13-LTS
Parameters(files -> median-test): 3.612799 ms
这是怎么回事?
编辑:
按以下方式更改代码至少有一定的作用,但不尊重这些选项。它似乎为文件"中位数"总共运行了18次测试,这不是3+10的总和。
object MedianMaintenanceBenchmark extends Bench.ForkedTime {
/* configuration */
override def aggregator: Aggregator[Double] = Aggregator.median
private val opts = Context(
exec.minWarmupRuns-> 3,
exec.maxWarmupRuns -> 3,
exec.benchRuns -> 10,
exec.jvmflags -> List("-Xms2g", "-Xmx2g")
)
/* inputs */
private val files: Gen[String] = Gen.enumeration("files")("median-test", "Median")
private val num: Gen[Seq[Int]] = (for (f <- files) yield numbers(f)).cached
/* tests */
performance of "MedianMaintenance" config opts in {
measure method "using heap" in {
using(num) in {
xs => MedianMaintenanceUsingHeaps(xs).medians
}
}
measure method "using red-black BST" in {
using(num) in {
xs => MedianMaintenanceUsingRedBlackTree(xs).medians
}
}
}
private def numbers(filename: String): Seq[Int] = // elided
}
OP在这里:几个小时后,我终于能够浏览过时的文档,不管怎样,它都存在,并找出以下内容:
除了我上面的编辑,还有几种方法可以覆盖执行计数等
- 对于当前文件中的所有基准测试,请使用
override def defaultConfig: Context = Context(exec.benchRuns -> 10)
- 对于特定的基准测试,定义内联或定义
val opts: Context
并使用config opts in
DSL - 对于特定的方法,除了在方法DSL中使用
config opts in
之外,请执行与#2相同的操作 - 文档声称可以覆盖每个"曲线"的配置,但我找不到什么是"曲线",也找不到如何覆盖它的配置
independentSamples=派生的独立JVM的数量。
使用一组测试数据(随机选择?(在每个JVM上运行(minWarmupRuns to maxWarmupRuns)
次预热(有意义(,然后在每个JVM中运行benchRuns
次测试。进行多少次热身取决于对"稳定状态"的检测。对于每个JVM,似乎最后都有一个执行未被解释。
Total number of executions = independentSamples * ((minWarmupRuns to maxWarmupRuns) + benchRuns + 1)
例如,给定:
Context(
exec.minWarmupRuns -> 5,
exec.maxWarmupRuns -> 5,
exec.benchRuns -> 10,
exec.independentSamples -> 2
)
测试中的代码将有32次执行。