add --shared
[pylucene.git] / lucene-java-3.4.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
30 /** Base class for all Benchmark unit tests. */
31 public abstract class BenchmarkTestCase extends LuceneTestCase {
32   
33   public File getWorkDir() {
34     return TEMP_DIR;
35   }
36   
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];
42     int len;
43     
44     while ((len = resource.read(buffer)) > 0) {
45         dest.write(buffer, 0, len);
46     }
47
48     resource.close();
49     dest.close();
50   }
51   
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("\\", "/");
55   }
56   
57   /** Return a path, suitable for a .alg config file, for the workdir */
58   public String getWorkDirPath() {
59     return getWorkDir().getAbsolutePath().replace("\\", "/");
60   }
61   
62   // create the benchmark and execute it. 
63   public Benchmark execBenchmark(String[] algLines) throws Exception {
64     String algText = algLinesToText(algLines);
65     logTstLogic(algText);
66     Benchmark benchmark = new Benchmark(new StringReader(algText));
67     benchmark.execute();
68     return benchmark;
69   }
70   
71   // properties in effect in all tests here
72   final String propLines [] = {
73     "work.dir=" + getWorkDirPath(),
74     "directory=RAMDirectory",
75     "print.props=false",
76   };
77   
78   static final String NEW_LINE = System.getProperty("line.separator");
79   
80   // catenate alg lines to make the alg text
81   private String algLinesToText(String[] algLines) {
82     String indent = "  ";
83     StringBuilder sb = new StringBuilder();
84     for (int i = 0; i < propLines.length; i++) {
85       sb.append(indent).append(propLines[i]).append(NEW_LINE);
86     }
87     for (int i = 0; i < algLines.length; i++) {
88       sb.append(indent).append(algLines[i]).append(NEW_LINE);
89     }
90     return sb.toString();
91   }
92
93   private static void logTstLogic (String txt) {
94     if (!VERBOSE) 
95       return;
96     System.out.println("Test logic of:");
97     System.out.println(txt);
98   }
99
100 }