1 package org.apache.lucene.benchmark.byTask.tasks;
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.
20 import java.io.BufferedReader;
22 import java.io.FileInputStream;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.HashSet;
26 import java.util.Properties;
29 import org.apache.commons.compress.compressors.CompressorStreamFactory;
30 import org.apache.lucene.benchmark.BenchmarkTestCase;
31 import org.apache.lucene.benchmark.byTask.PerfRunData;
32 import org.apache.lucene.benchmark.byTask.feeds.DocMaker;
33 import org.apache.lucene.benchmark.byTask.utils.Config;
34 import org.apache.lucene.benchmark.byTask.utils.StreamUtils.Type;
35 import org.apache.lucene.document.Document;
36 import org.apache.lucene.document.Field;
37 import org.apache.lucene.document.Field.Index;
38 import org.apache.lucene.document.Field.Store;
40 /** Tests the functionality of {@link WriteLineDocTask}. */
41 public class WriteLineDocTaskTest extends BenchmarkTestCase {
43 // class has to be public so that Class.forName.newInstance() will work
44 public static final class WriteLineDocMaker extends DocMaker {
47 public Document makeDocument() throws Exception {
48 Document doc = new Document();
49 doc.add(new Field(BODY_FIELD, "body", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
50 doc.add(new Field(TITLE_FIELD, "title", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
51 doc.add(new Field(DATE_FIELD, "date", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
57 // class has to be public so that Class.forName.newInstance() will work
58 public static final class NewLinesDocMaker extends DocMaker {
61 public Document makeDocument() throws Exception {
62 Document doc = new Document();
63 doc.add(new Field(BODY_FIELD, "body\r\ntext\ttwo", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
64 doc.add(new Field(TITLE_FIELD, "title\r\ntext", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
65 doc.add(new Field(DATE_FIELD, "date\r\ntext", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
71 // class has to be public so that Class.forName.newInstance() will work
72 public static final class NoBodyDocMaker extends DocMaker {
74 public Document makeDocument() throws Exception {
75 Document doc = new Document();
76 doc.add(new Field(TITLE_FIELD, "title", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
77 doc.add(new Field(DATE_FIELD, "date", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
82 // class has to be public so that Class.forName.newInstance() will work
83 public static final class NoTitleDocMaker extends DocMaker {
85 public Document makeDocument() throws Exception {
86 Document doc = new Document();
87 doc.add(new Field(BODY_FIELD, "body", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
88 doc.add(new Field(DATE_FIELD, "date", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
93 // class has to be public so that Class.forName.newInstance() will work
94 public static final class JustDateDocMaker extends DocMaker {
96 public Document makeDocument() throws Exception {
97 Document doc = new Document();
98 doc.add(new Field(DATE_FIELD, "date", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
103 // class has to be public so that Class.forName.newInstance() will work
104 // same as JustDate just that this one is treated as legal
105 public static final class LegalJustDateDocMaker extends DocMaker {
107 public Document makeDocument() throws Exception {
108 Document doc = new Document();
109 doc.add(new Field(DATE_FIELD, "date", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
114 // class has to be public so that Class.forName.newInstance() will work
115 public static final class EmptyDocMaker extends DocMaker {
117 public Document makeDocument() throws Exception {
118 return new Document();
122 // class has to be public so that Class.forName.newInstance() will work
123 public static final class ThreadingDocMaker extends DocMaker {
126 public Document makeDocument() throws Exception {
127 Document doc = new Document();
128 String name = Thread.currentThread().getName();
129 doc.add(new Field(BODY_FIELD, "body_" + name, Store.NO, Index.NOT_ANALYZED_NO_NORMS));
130 doc.add(new Field(TITLE_FIELD, "title_" + name, Store.NO, Index.NOT_ANALYZED_NO_NORMS));
131 doc.add(new Field(DATE_FIELD, "date_" + name, Store.NO, Index.NOT_ANALYZED_NO_NORMS));
137 private static final CompressorStreamFactory csFactory = new CompressorStreamFactory();
139 private PerfRunData createPerfRunData(File file,
140 boolean allowEmptyDocs,
141 String docMakerName) throws Exception {
142 Properties props = new Properties();
143 props.setProperty("doc.maker", docMakerName);
144 props.setProperty("line.file.out", file.getAbsolutePath());
145 props.setProperty("directory", "RAMDirectory"); // no accidental FS dir.
146 if (allowEmptyDocs) {
147 props.setProperty("sufficient.fields", ",");
149 if (docMakerName.equals(LegalJustDateDocMaker.class.getName())) {
150 props.setProperty("line.fields", DocMaker.DATE_FIELD);
151 props.setProperty("sufficient.fields", DocMaker.DATE_FIELD);
153 Config config = new Config(props);
154 return new PerfRunData(config);
157 private void doReadTest(File file, Type fileType, String expTitle,
158 String expDate, String expBody) throws Exception {
159 InputStream in = new FileInputStream(file);
162 in = csFactory.createCompressorInputStream(CompressorStreamFactory.BZIP2, in);
165 in = csFactory.createCompressorInputStream(CompressorStreamFactory.GZIP, in);
168 break; // nothing to do
170 assertFalse("Unknown file type!",true); //fail, should not happen
172 BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
174 String line = br.readLine();
175 assertHeaderLine(line);
176 line = br.readLine();
178 String[] parts = line.split(Character.toString(WriteLineDocTask.SEP));
179 int numExpParts = expBody == null ? 2 : 3;
180 assertEquals(numExpParts, parts.length);
181 assertEquals(expTitle, parts[0]);
182 assertEquals(expDate, parts[1]);
183 if (expBody != null) {
184 assertEquals(expBody, parts[2]);
186 assertNull(br.readLine());
192 private void assertHeaderLine(String line) {
193 assertTrue("First line should be a header line",line.startsWith(WriteLineDocTask.FIELDS_HEADER_INDICATOR));
196 /* Tests WriteLineDocTask with a bzip2 format. */
197 public void testBZip2() throws Exception {
199 // Create a document in bz2 format.
200 File file = new File(getWorkDir(), "one-line.bz2");
201 PerfRunData runData = createPerfRunData(file, false, WriteLineDocMaker.class.getName());
202 WriteLineDocTask wldt = new WriteLineDocTask(runData);
206 doReadTest(file, Type.BZIP2, "title", "date", "body");
209 /* Tests WriteLineDocTask with a gzip format. */
210 public void testGZip() throws Exception {
212 // Create a document in gz format.
213 File file = new File(getWorkDir(), "one-line.gz");
214 PerfRunData runData = createPerfRunData(file, false, WriteLineDocMaker.class.getName());
215 WriteLineDocTask wldt = new WriteLineDocTask(runData);
219 doReadTest(file, Type.GZIP, "title", "date", "body");
222 public void testRegularFile() throws Exception {
224 // Create a document in regular format.
225 File file = new File(getWorkDir(), "one-line");
226 PerfRunData runData = createPerfRunData(file, false, WriteLineDocMaker.class.getName());
227 WriteLineDocTask wldt = new WriteLineDocTask(runData);
231 doReadTest(file, Type.PLAIN, "title", "date", "body");
234 public void testCharsReplace() throws Exception {
235 // WriteLineDocTask replaced only \t characters w/ a space, since that's its
236 // separator char. However, it didn't replace newline characters, which
237 // resulted in errors in LineDocSource.
238 File file = new File(getWorkDir(), "one-line");
239 PerfRunData runData = createPerfRunData(file, false, NewLinesDocMaker.class.getName());
240 WriteLineDocTask wldt = new WriteLineDocTask(runData);
244 doReadTest(file, Type.PLAIN, "title text", "date text", "body text two");
247 public void testEmptyBody() throws Exception {
248 // WriteLineDocTask threw away documents w/ no BODY element, even if they
249 // had a TITLE element (LUCENE-1755). It should throw away documents if they
250 // don't have BODY nor TITLE
251 File file = new File(getWorkDir(), "one-line");
252 PerfRunData runData = createPerfRunData(file, false, NoBodyDocMaker.class.getName());
253 WriteLineDocTask wldt = new WriteLineDocTask(runData);
257 doReadTest(file, Type.PLAIN, "title", "date", null);
260 public void testEmptyTitle() throws Exception {
261 File file = new File(getWorkDir(), "one-line");
262 PerfRunData runData = createPerfRunData(file, false, NoTitleDocMaker.class.getName());
263 WriteLineDocTask wldt = new WriteLineDocTask(runData);
267 doReadTest(file, Type.PLAIN, "", "date", "body");
270 /** Fail by default when there's only date */
271 public void testJustDate() throws Exception {
272 File file = new File(getWorkDir(), "one-line");
273 PerfRunData runData = createPerfRunData(file, false, JustDateDocMaker.class.getName());
274 WriteLineDocTask wldt = new WriteLineDocTask(runData);
278 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
280 String line = br.readLine();
281 assertHeaderLine(line);
282 line = br.readLine();
289 public void testLegalJustDate() throws Exception {
290 File file = new File(getWorkDir(), "one-line");
291 PerfRunData runData = createPerfRunData(file, false, LegalJustDateDocMaker.class.getName());
292 WriteLineDocTask wldt = new WriteLineDocTask(runData);
296 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
298 String line = br.readLine();
299 assertHeaderLine(line);
300 line = br.readLine();
307 public void testEmptyDoc() throws Exception {
308 File file = new File(getWorkDir(), "one-line");
309 PerfRunData runData = createPerfRunData(file, true, EmptyDocMaker.class.getName());
310 WriteLineDocTask wldt = new WriteLineDocTask(runData);
314 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
316 String line = br.readLine();
317 assertHeaderLine(line);
318 line = br.readLine();
325 public void testMultiThreaded() throws Exception {
326 File file = new File(getWorkDir(), "one-line");
327 PerfRunData runData = createPerfRunData(file, false, ThreadingDocMaker.class.getName());
328 final WriteLineDocTask wldt = new WriteLineDocTask(runData);
329 Thread[] threads = new Thread[10];
330 for (int i = 0; i < threads.length; i++) {
331 threads[i] = new Thread("t" + i) {
336 } catch (Exception e) {
337 throw new RuntimeException(e);
343 for (Thread t : threads) t.start();
344 for (Thread t : threads) t.join();
348 Set<String> ids = new HashSet<String>();
349 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
351 String line = br.readLine();
352 assertHeaderLine(line); // header line is written once, no matter how many threads there are
353 for (int i = 0; i < threads.length; i++) {
354 line = br.readLine();
355 String[] parts = line.split(Character.toString(WriteLineDocTask.SEP));
356 assertEquals(3, parts.length);
357 // check that all thread names written are the same in the same line
358 String tname = parts[0].substring(parts[0].indexOf('_'));
360 assertEquals(tname, parts[1].substring(parts[1].indexOf('_')));
361 assertEquals(tname, parts[2].substring(parts[2].indexOf('_')));
363 // only threads.length lines should exist
364 assertNull(br.readLine());
365 assertEquals(threads.length, ids.size());