alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Bench.scala)

This example Scala source code file (Bench.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

bench, bench, benchcompanion, int, int, list, map, option, some, string, string, unit, unit, unknown

The Scala Bench.scala source code

package scala.collection.parallel.benchmarks


import scala.collection._
import scala.testing.Benchmark



trait BenchCompanion {
  def benchName: String
  def collectionName: String
  def fullname = collectionName + "." + benchName
  def defaultSize = 100000
  def comparisons = List[String]()
  def apply(sz: Int, parallelism: Int, what: String): Bench
}


/**
 * An interface for all benchmark classes.
 * A benchmark runs some functionality a prespecified number of times.
 */
trait Bench extends Benchmark {
  val size: Int
  
  val parallelism: Int
  
  val runWhat: String
  
  /**
   * Name of the benchmark. Convention is for it to start with the name of the collection being
   * tested, continuing '.' and ending with the name of the specific functionality being benchmarked. 
   * @return
   */
  def name: String = companion.fullname
  def collectionName: String = companion.collectionName
  def benchName: String = companion.benchName
  
  def companion: BenchCompanion
  
  def runseq: Unit
  
  def runpar: Unit
  
  /**
   * Describes the number of runs of the test.
   */
  val runs = 10
  
  /**
   * Returns the number of repetitions for this benchmark.
   */
  def repetitionsPerRun = 500
  
  /**
   * Resets the benchmark object. Typically, this means recreating
   * the collection being tested.
   */
  def reset: Unit
  
  /**
   * Returns a map of available comparison tests.
   */
  def comparisons: List[String] = companion.comparisons
  
  def comparison(name: String): Option[() => Unit] = comparisonMap.get(name)
  
  def comparisonMap: Map[String, () => Unit]
  
  def run = runWhat match {
    case "seq" => for (i <- 0 until repetitionsPerRun) runseq
    case "par" => for (i <- 0 until repetitionsPerRun) runpar
    case _ => comparison(runWhat) match {
      case Some(fun) => for (i <- 0 until repetitionsPerRun) fun()
      case None => throw new IllegalArgumentException("Unknown bench option: `" + runWhat + 
          "`, need `seq`, `par` or one of: " + comparisons.mkString("`", "`, `", "`"))
    }
  }
  
  /**
   * Prints results of the benchmark. May be overidden in benchmarks.
   */
  def printResults {}
  
  def onEnd {}
  
  def executeBenchmark = {
    println("-----------------------")
    print(name + ", " + runWhat + ", par.=" + parallelism + ", sz=" + niceSize + ": ")
    
    val times = runBenchmark(runs)
    
    onEnd
    
    for (t <- times) print(t + " ")
    println
    printResults
  }
  
  private def niceSize = if (size < 1000 || size % 1000 != 0) size.toString else size / 1000 + "k"
}


trait HavingResult[T] extends Bench {
  var runresult: T = null.asInstanceOf[T]
  
  abstract override def printResults {
    println("result: " + (if (runresult != null) runresult else "<not set>"))
    super.printResults
  }
}















Other Scala examples (source code examples)

Here is a short list of links related to this Scala Bench.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.