From bb73b041480e06dac7fc1e851776a3654541e594 Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Thu, 21 Oct 2004 09:50:39 +0000 Subject: Scala benchmark suite added --- config/list/library.lst | 1 + sources/scala/testing/Benchmark.scala | 74 +++++++++++++++++++++++++++++++++ test/benchmark/build.xml | 22 ++++++++++ test/benchmark/predef.xml | 78 +++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 sources/scala/testing/Benchmark.scala create mode 100644 test/benchmark/build.xml create mode 100644 test/benchmark/predef.xml diff --git a/config/list/library.lst b/config/list/library.lst index 43cd4e6807..544c29cde6 100644 --- a/config/list/library.lst +++ b/config/list/library.lst @@ -184,6 +184,7 @@ runtime/types/JavaTypeRepository.java testing/UnitTest.scala testing/SUnit.scala +testing/Benchmark.scala util/alphabet/Alphabet.scala util/alphabet/AlphabetPlusWildcard.scala diff --git a/sources/scala/testing/Benchmark.scala b/sources/scala/testing/Benchmark.scala new file mode 100644 index 0000000000..f6523a7271 --- /dev/null +++ b/sources/scala/testing/Benchmark.scala @@ -0,0 +1,74 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + + +/** + * Scala benchmarking mini framework. + */ + +package scala.testing; + +/** Benchmark can be used to quickly turn an existing + * class into a benchmark. Here is a short example: + * + *
+ *  object sort1 extends Sorter with Benchmark {
+ *     def run = sort(List.range(1, 1000));
+ *  }
+ *  
+ * + * The run method has to be defined by the user, who will perform + * the timed operation there. + * Run the benchmark as follows: + *
+ *   scala sort1 5 times.log
+ * 
+ * This will run the benchmark 5 times and log the execution times in + * a file called times.log + */ +trait Benchmark { + + /** this method should be implemented by the concrete benchmark */ + def run: Unit; + + /** Run the benchmark the specified number of times + * and return a list with the execution times in milliseconds + * in reverse order of the execution */ + def runBenchmark(noTimes: Int): List[Long] = + + for (val i <- List.range(1, noTimes + 1)) yield { + val startTime = System.currentTimeMillis(); + run; + val stopTime = System.currentTimeMillis(); + System.gc(); + + stopTime - startTime + } + + /** + * The entry point. It takes two arguments: the number of + * consecutive runs, and the name of a log file where to + * append the times. + * + */ + def main(args: Array[String]): Unit = { + if (args.length > 1) { + val logFile = new java.io.FileWriter(args(1), true); // append, not overwrite + + logFile.write(getClass().getName()); + for (val t <- runBenchmark(Integer.parseInt(args(0)))) + logFile.write("\t\t" + t); + + logFile.write("\n"); + logFile.flush(); + } else + Console.println("Usage: scala benchmarks.program "); + } +} + diff --git a/test/benchmark/build.xml b/test/benchmark/build.xml new file mode 100644 index 0000000000..da7645d81e --- /dev/null +++ b/test/benchmark/build.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/test/benchmark/predef.xml b/test/benchmark/predef.xml new file mode 100644 index 0000000000..72bab526fe --- /dev/null +++ b/test/benchmark/predef.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ** Running benchmark @{classname} in @{location}.. + + + + + Done + + + + + + + + + + + + + + + + + + + + + + + +** Building project ${ant.project.name} to @{destination} + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3