PageRenderTime 204ms CodeModel.GetById 123ms app.highlight 6ms RepoModel.GetById 72ms app.codeStats 0ms

/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 */
18package org.scalabench.benchmarks.scalatest;
19
20import java.io.File;
21import java.io.IOException;
22
23import org.dacapo.parser.Config;
24import org.scalabench.dacapo.utils.AbstractBenchmark;
25
26/**
27 * A benchmark harness for ScalaTest, a free, open-source testing toolkit for Scala and Java programmers.
28 * 
29 * @see <a href="http://www.scalatest.org/">ScalaTest</a>
30 * @see <a href="http://www.dacapobench.org/">DaCapo Benchmark Suite</a>
31 */
32public class ScalaTest extends AbstractBenchmark {
33    
34    private String[] args;
35    
36    private boolean preexistingTargetDirectory = true;
37    
38    /**
39     * Unfortunately, org.scalatest.tools.RunnerSuite requires a directory named <code>target</code> to exist in the
40     * <em>current</em> directory.
41     */
42    private static final File TARGET_DIRECTORY = new File("target");
43    
44    public ScalaTest(Config config, File scratch) throws Exception {
45        super(config, scratch);
46    }
47    
48    @Override
49    protected void prepare(String size) throws Exception {
50        super.prepare(size);
51        
52        final String[] options = new String[] {
53                // Execute suites concurrently, scaled to the number of threads.
54                "-c" + config.getThreadCount(size),
55                // Output to System.out; without ANSI color.
56                "-oW",
57                // Path from which the test classes are run within a temporary class loader
58                "-p", new File(scratch, config.name).getAbsolutePath().replace(" ", "\\ "),
59        };
60        
61        method = loader.loadClass("org.scalatest.tools.Runner").getMethod("run", String[].class);
62        
63        args = concat(options, intersperseSuiteParameters(config.getArgs(size)));
64        
65        preexistingTargetDirectory = TARGET_DIRECTORY.exists() && TARGET_DIRECTORY.isDirectory();
66        
67        if (!preexistingTargetDirectory && !TARGET_DIRECTORY.mkdir())
68            throw new IOException("Couldn't create temporary directory: " + TARGET_DIRECTORY.getCanonicalPath());
69    }
70    
71    @Override
72    public void iterate(String size) throws Exception {
73        method.invoke(null, (Object) args);
74    }
75    
76    private String[] intersperseSuiteParameters(String[] args) {
77        final String[] runnerArgs = new String[2 * args.length];
78        for (int i = 0; i < args.length; i++) {
79            runnerArgs[2 * i] = "-s";
80            runnerArgs[2 * i + 1] = args[i];
81        }
82        return runnerArgs;
83    }
84    
85    @Override
86    public void cleanup() {
87        super.cleanup();
88        if (!preexistingTargetDirectory)
89            TARGET_DIRECTORY.delete();
90    }
91}