🎓 A1Learner
🔍
0 of 45 lessons completed

A Benchmark Harness

A benchmark harness is a small reusable helper that runs a piece of work and reports its result. It keeps the repetition and reporting in one place, so each test uses the same rules.

One helper runs any block

The harness takes a name, a count, and a block. This program runs a sum three times and prints the name, the count, and the result.

def bench(name: String, times: Int, work: () => Int): Unit =
  var result = 0
  var i = 0
  while i < times do
    result = work()
    i = i + 1
  println(name)
  println(times)
  println(result)

@main def main(): Unit =
  bench("sum", 3, () => List(1, 2, 3).sum)

The same helper serves two tests

Each call reports in the same format. This program runs a sum test and a count test and prints both reports.

def bench(name: String, times: Int, work: () => Int): Unit =
  var result = 0
  var i = 0
  while i < times do
    result = work()
    i = i + 1
  println(name)
  println(times)
  println(result)

@main def main(): Unit =
  bench("sum", 2, () => List(1, 2, 3, 4).sum)
  bench("count", 4, () => List(5, 6, 7).size)

Setup stays outside the block

The list is built before the harness call. This program prints the harness report, then the setup size kept apart from it.

def bench(name: String, times: Int, work: () => Int): Unit =
  var result = 0
  var i = 0
  while i < times do
    result = work()
    i = i + 1
  println(name)
  println(times)
  println(result)

@main def main(): Unit =
  val numbers = (1 to 10).toList
  bench("sum", 3, () => numbers.sum)
  println(numbers.size)

Example

def bench(name: String, times: Int, work: () => Int): Unit =
  var result = 0
  var i = 0
  while i < times do
    result = work()
    i = i + 1
  println(name)
  println(times)
  println(result)

@main def main(): Unit =
  val numbers = List(1, 2, 3, 4, 5)
  bench("sum", 5, () => numbers.sum)
  bench("squares", 2, () => numbers.map(x => x * x).sum)

Key points

  • Harness: A helper that runs work and reports a result in a fixed format.
  • Block: A block passed to the harness is a function with no inputs that returns a result when called.
  • Repetition: A fixed repetition count is the same number of runs for every test.
  • Name: A name labels which test produced a report, so reports stay separate.
  • Result: The final result is the value the work produced on its last run.

In this example, the harness is defined once, then it runs a sum test five times and a squares test twice, and each report prints its name, its repetition count, and its final result.

Note

In Scala, a function value written as () => a value runs only when it is called, not when it is written.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Scastie

Exercise

What does this harness print for each test?