/src/main/java/org/scalabench/benchmarks/scalatest/ScalaTest.java

https://bitbucket.org/scalabench/scalatest-dacapo-benchmark · Java · 91 lines · 45 code · 16 blank · 30 comment · 5 complexity · 105969964fd00b94d5ac86f96c4e68f3 MD5 · raw file

  1. /*
  2. * ScalaTest Benchmark
  3. * Copyright (C) 2010 Technische Universität Darmstadt
  4. * info@scalabench.org
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.scalabench.benchmarks.scalatest;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import org.dacapo.parser.Config;
  22. import org.scalabench.dacapo.utils.AbstractBenchmark;
  23. /**
  24. * A benchmark harness for ScalaTest, a free, open-source testing toolkit for Scala and Java programmers.
  25. *
  26. * @see <a href="http://www.scalatest.org/">ScalaTest</a>
  27. * @see <a href="http://www.dacapobench.org/">DaCapo Benchmark Suite</a>
  28. */
  29. public class ScalaTest extends AbstractBenchmark {
  30. private String[] args;
  31. private boolean preexistingTargetDirectory = true;
  32. /**
  33. * Unfortunately, org.scalatest.tools.RunnerSuite requires a directory named <code>target</code> to exist in the
  34. * <em>current</em> directory.
  35. */
  36. private static final File TARGET_DIRECTORY = new File("target");
  37. public ScalaTest(Config config, File scratch) throws Exception {
  38. super(config, scratch);
  39. }
  40. @Override
  41. protected void prepare(String size) throws Exception {
  42. super.prepare(size);
  43. final String[] options = new String[] {
  44. // Execute suites concurrently, scaled to the number of threads.
  45. "-c" + config.getThreadCount(size),
  46. // Output to System.out; without ANSI color.
  47. "-oW",
  48. // Path from which the test classes are run within a temporary class loader
  49. "-p", new File(scratch, config.name).getAbsolutePath().replace(" ", "\\ "),
  50. };
  51. method = loader.loadClass("org.scalatest.tools.Runner").getMethod("run", String[].class);
  52. args = concat(options, intersperseSuiteParameters(config.getArgs(size)));
  53. preexistingTargetDirectory = TARGET_DIRECTORY.exists() && TARGET_DIRECTORY.isDirectory();
  54. if (!preexistingTargetDirectory && !TARGET_DIRECTORY.mkdir())
  55. throw new IOException("Couldn't create temporary directory: " + TARGET_DIRECTORY.getCanonicalPath());
  56. }
  57. @Override
  58. public void iterate(String size) throws Exception {
  59. method.invoke(null, (Object) args);
  60. }
  61. private String[] intersperseSuiteParameters(String[] args) {
  62. final String[] runnerArgs = new String[2 * args.length];
  63. for (int i = 0; i < args.length; i++) {
  64. runnerArgs[2 * i] = "-s";
  65. runnerArgs[2 * i + 1] = args[i];
  66. }
  67. return runnerArgs;
  68. }
  69. @Override
  70. public void cleanup() {
  71. super.cleanup();
  72. if (!preexistingTargetDirectory)
  73. TARGET_DIRECTORY.delete();
  74. }
  75. }