Search code examples
scalascalameter

How can ScalaMeter chain configuration


I'm quite confusing on how can ScalaMeter chain configuration like this

val standardConfig = config(
  Key.exec.minWarmupRuns -> 5,
  Key.exec.maxWarmupRuns -> 10,
  Key.exec.benchRuns -> 10,
  Key.verbose -> true
) withWarmer(new Warmer.Default)

The first config(...) expression returns MeasureBuilder[T, U] type, this I understand.

However, how can we chain the second expression withWarmer(new Warmer.Default) which also returns MeasureBuilder type.

At first, I guess that MeasureBuilder type implement apply method that allow us to do this, but at the last step before measuring the performance of a piece of code we need

val partime = standardConfig measure {
  ...
}

where measure { ... } return Quantity[U] type which is not MeasureBuilder.

So, How can ScalaMeter configuration chain the expression like that?


Solution

  • Please first note that config method is exposed after imporing:

    import org.scalameter.config
    

    This is because it is declared on the companion object in the package level:

    package object scalameter extends MeasureBuilder[Unit, Double](
    

    Therefore when you declare val standardConfig = config(...) you get MeasureBuilder[Unit, Double]

    Afterwards, the class MeasureBuilder exposes a method withWarmer, which is actually called, and returns a MeasureBuilder[Unit, Double], with both configuration and warmer applied.

    MeasureBuilder exposes a method measure:

    def measure[S](b: =>S): Quantity[U] = measured(b)._2
    

    which returns Quantity[Double] in your example.