pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / BenchmarkTestCase.java
1 package org.apache.lucene.benchmark;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.File;
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;
26
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;
32
33 /** Base class for all Benchmark unit tests. */
34 public abstract class BenchmarkTestCase extends LuceneTestCase {
35   private static File WORKDIR;
36   
37   @BeforeClass
38   public static void beforeClassBenchmarkTestCase() throws Exception {
39     WORKDIR = _TestUtil.getTempDir("benchmark");
40     WORKDIR.delete();
41     WORKDIR.mkdirs();
42   }
43   
44   @AfterClass
45   public static void afterClassBenchmarkTestCase() throws Exception {
46     WORKDIR = null;
47   }
48   
49   public File getWorkDir() {
50     return WORKDIR;
51   }
52   
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];
58     int len;
59     
60     while ((len = resource.read(buffer)) > 0) {
61         dest.write(buffer, 0, len);
62     }
63
64     resource.close();
65     dest.close();
66   }
67   
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("\\", "/");
71   }
72   
73   /** Return a path, suitable for a .alg config file, for the workdir */
74   public String getWorkDirPath() {
75     return getWorkDir().getAbsolutePath().replace("\\", "/");
76   }
77   
78   // create the benchmark and execute it. 
79   public Benchmark execBenchmark(String[] algLines) throws Exception {
80     String algText = algLinesToText(algLines);
81     logTstLogic(algText);
82     Benchmark benchmark = new Benchmark(new StringReader(algText));
83     benchmark.execute();
84     return benchmark;
85   }
86   
87   // properties in effect in all tests here
88   final String propLines [] = {
89     "work.dir=" + getWorkDirPath(),
90     "directory=RAMDirectory",
91     "print.props=false",
92   };
93   
94   static final String NEW_LINE = System.getProperty("line.separator");
95   
96   // catenate alg lines to make the alg text
97   private String algLinesToText(String[] algLines) {
98     String indent = "  ";
99     StringBuilder sb = new StringBuilder();
100     for (int i = 0; i < propLines.length; i++) {
101       sb.append(indent).append(propLines[i]).append(NEW_LINE);
102     }
103     for (int i = 0; i < algLines.length; i++) {
104       sb.append(indent).append(algLines[i]).append(NEW_LINE);
105     }
106     return sb.toString();
107   }
108
109   private static void logTstLogic (String txt) {
110     if (!VERBOSE) 
111       return;
112     System.out.println("Test logic of:");
113     System.out.println(txt);
114   }
115
116 }