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;
30 /** Base class for all Benchmark unit tests. */
31 public abstract class BenchmarkTestCase extends LuceneTestCase {
33 public File getWorkDir() {
37 /** Copy a resource into the workdir */
38 public void copyToWorkDir(String resourceName) throws IOException {
39 InputStream resource = getClass().getResourceAsStream(resourceName);
40 OutputStream dest = new FileOutputStream(new File(getWorkDir(), resourceName));
41 byte[] buffer = new byte[8192];
44 while ((len = resource.read(buffer)) > 0) {
45 dest.write(buffer, 0, len);
52 /** Return a path, suitable for a .alg config file, for a resource in the workdir */
53 public String getWorkDirResourcePath(String resourceName) {
54 return new File(getWorkDir(), resourceName).getAbsolutePath().replace("\\", "/");
57 /** Return a path, suitable for a .alg config file, for the workdir */
58 public String getWorkDirPath() {
59 return getWorkDir().getAbsolutePath().replace("\\", "/");
62 // create the benchmark and execute it.
63 public Benchmark execBenchmark(String[] algLines) throws Exception {
64 String algText = algLinesToText(algLines);
66 Benchmark benchmark = new Benchmark(new StringReader(algText));
71 // properties in effect in all tests here
72 final String propLines [] = {
73 "work.dir=" + getWorkDirPath(),
74 "directory=RAMDirectory",
78 static final String NEW_LINE = System.getProperty("line.separator");
80 // catenate alg lines to make the alg text
81 private String algLinesToText(String[] algLines) {
83 StringBuilder sb = new StringBuilder();
84 for (int i = 0; i < propLines.length; i++) {
85 sb.append(indent).append(propLines[i]).append(NEW_LINE);
87 for (int i = 0; i < algLines.length; i++) {
88 sb.append(indent).append(algLines[i]).append(NEW_LINE);
93 private static void logTstLogic (String txt) {
96 System.out.println("Test logic of:");
97 System.out.println(txt);