1 package org.apache.lucene.benchmark;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.StringReader;
27 import org.apache.lucene.benchmark.byTask.Benchmark;
28 import org.apache.lucene.util.LuceneTestCase;
29 import org.apache.lucene.util._TestUtil;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
33 /** Base class for all Benchmark unit tests. */
34 public abstract class BenchmarkTestCase extends LuceneTestCase {
35 private static File WORKDIR;
38 public static void beforeClassBenchmarkTestCase() throws Exception {
39 WORKDIR = _TestUtil.getTempDir("benchmark");
45 public static void afterClassBenchmarkTestCase() throws Exception {
49 public File getWorkDir() {
53 /** Copy a resource into the workdir */
54 public void copyToWorkDir(String resourceName) throws IOException {
55 InputStream resource = getClass().getResourceAsStream(resourceName);
56 OutputStream dest = new FileOutputStream(new File(getWorkDir(), resourceName));
57 byte[] buffer = new byte[8192];
60 while ((len = resource.read(buffer)) > 0) {
61 dest.write(buffer, 0, len);
68 /** Return a path, suitable for a .alg config file, for a resource in the workdir */
69 public String getWorkDirResourcePath(String resourceName) {
70 return new File(getWorkDir(), resourceName).getAbsolutePath().replace("\\", "/");
73 /** Return a path, suitable for a .alg config file, for the workdir */
74 public String getWorkDirPath() {
75 return getWorkDir().getAbsolutePath().replace("\\", "/");
78 // create the benchmark and execute it.
79 public Benchmark execBenchmark(String[] algLines) throws Exception {
80 String algText = algLinesToText(algLines);
82 Benchmark benchmark = new Benchmark(new StringReader(algText));
87 // properties in effect in all tests here
88 final String propLines [] = {
89 "work.dir=" + getWorkDirPath(),
90 "directory=RAMDirectory",
94 static final String NEW_LINE = System.getProperty("line.separator");
96 // catenate alg lines to make the alg text
97 private String algLinesToText(String[] algLines) {
99 StringBuilder sb = new StringBuilder();
100 for (int i = 0; i < propLines.length; i++) {
101 sb.append(indent).append(propLines[i]).append(NEW_LINE);
103 for (int i = 0; i < algLines.length; i++) {
104 sb.append(indent).append(algLines[i]).append(NEW_LINE);
106 return sb.toString();
109 private static void logTstLogic (String txt) {
112 System.out.println("Test logic of:");
113 System.out.println(txt);