pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / byTask / tasks / WriteLineDocTaskTest.java
1 package org.apache.lucene.benchmark.byTask.tasks;
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.BufferedReader;
21 import java.io.File;
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;
27 import java.util.Set;
28
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;
39
40 /** Tests the functionality of {@link WriteLineDocTask}. */
41 public class WriteLineDocTaskTest extends BenchmarkTestCase {
42
43   // class has to be public so that Class.forName.newInstance() will work
44   public static final class WriteLineDocMaker extends DocMaker {
45   
46     @Override
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));
52       return doc;
53     }
54     
55   }
56   
57   // class has to be public so that Class.forName.newInstance() will work
58   public static final class NewLinesDocMaker extends DocMaker {
59   
60     @Override
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));
66       return doc;
67     }
68     
69   }
70   
71   // class has to be public so that Class.forName.newInstance() will work
72   public static final class NoBodyDocMaker extends DocMaker {
73     @Override
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));
78       return doc;
79     }
80   }
81   
82   // class has to be public so that Class.forName.newInstance() will work
83   public static final class NoTitleDocMaker extends DocMaker {
84     @Override
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));
89       return doc;
90     }
91   }
92   
93   // class has to be public so that Class.forName.newInstance() will work
94   public static final class JustDateDocMaker extends DocMaker {
95     @Override
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));
99       return doc;
100     }
101   }
102
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 {
106     @Override
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));
110       return doc;
111     }
112   }
113
114   // class has to be public so that Class.forName.newInstance() will work
115   public static final class EmptyDocMaker extends DocMaker {
116     @Override
117     public Document makeDocument() throws Exception {
118       return new Document();
119     }
120   }
121   
122   // class has to be public so that Class.forName.newInstance() will work
123   public static final class ThreadingDocMaker extends DocMaker {
124   
125     @Override
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));
132       return doc;
133     }
134     
135   }
136
137   private static final CompressorStreamFactory csFactory = new CompressorStreamFactory();
138
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", ",");
148     }
149     if (docMakerName.equals(LegalJustDateDocMaker.class.getName())) {
150       props.setProperty("line.fields", DocMaker.DATE_FIELD);
151       props.setProperty("sufficient.fields", DocMaker.DATE_FIELD);
152     }
153     Config config = new Config(props);
154     return new PerfRunData(config);
155   }
156   
157   private void doReadTest(File file, Type fileType, String expTitle,
158                           String expDate, String expBody) throws Exception {
159     InputStream in = new FileInputStream(file);
160     switch(fileType) {
161         case BZIP2:
162                 in = csFactory.createCompressorInputStream(CompressorStreamFactory.BZIP2, in);
163                 break;
164         case GZIP:
165                 in = csFactory.createCompressorInputStream(CompressorStreamFactory.GZIP, in);
166                 break;
167         case PLAIN:
168                 break; // nothing to do
169         default:
170                 assertFalse("Unknown file type!",true); //fail, should not happen
171     }
172     BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
173     try {
174       String line = br.readLine();
175       assertHeaderLine(line);
176       line = br.readLine();
177       assertNotNull(line);
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]);
185       }
186       assertNull(br.readLine());
187     } finally {
188       br.close();
189     }
190   }
191
192   private void assertHeaderLine(String line) {
193     assertTrue("First line should be a header line",line.startsWith(WriteLineDocTask.FIELDS_HEADER_INDICATOR));
194   }
195   
196   /* Tests WriteLineDocTask with a bzip2 format. */
197   public void testBZip2() throws Exception {
198     
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);
203     wldt.doLogic();
204     wldt.close();
205     
206     doReadTest(file, Type.BZIP2, "title", "date", "body");
207   }
208   
209   /* Tests WriteLineDocTask with a gzip format. */
210   public void testGZip() throws Exception {
211     
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);
216     wldt.doLogic();
217     wldt.close();
218     
219     doReadTest(file, Type.GZIP, "title", "date", "body");
220   }
221   
222   public void testRegularFile() throws Exception {
223     
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);
228     wldt.doLogic();
229     wldt.close();
230     
231     doReadTest(file, Type.PLAIN, "title", "date", "body");
232   }
233
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);
241     wldt.doLogic();
242     wldt.close();
243     
244     doReadTest(file, Type.PLAIN, "title text", "date text", "body text two");
245   }
246   
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);
254     wldt.doLogic();
255     wldt.close();
256     
257     doReadTest(file, Type.PLAIN, "title", "date", null);
258   }
259   
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);
264     wldt.doLogic();
265     wldt.close();
266     
267     doReadTest(file, Type.PLAIN, "", "date", "body");
268   }
269   
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);
275     wldt.doLogic();
276     wldt.close();
277     
278     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
279     try {
280       String line = br.readLine();
281       assertHeaderLine(line);
282       line = br.readLine();
283       assertNull(line);
284     } finally {
285       br.close();
286     }
287   }
288
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);
293     wldt.doLogic();
294     wldt.close();
295     
296     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
297     try {
298       String line = br.readLine();
299       assertHeaderLine(line);
300       line = br.readLine();
301       assertNotNull(line);
302     } finally {
303       br.close();
304     }
305   }
306
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);
311     wldt.doLogic();
312     wldt.close();
313     
314     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
315     try {
316       String line = br.readLine();
317       assertHeaderLine(line);
318       line = br.readLine();
319       assertNotNull(line);
320     } finally {
321       br.close();
322     }
323   }
324
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) {
332         @Override
333         public void run() {
334           try {
335             wldt.doLogic();
336           } catch (Exception e) {
337             throw new RuntimeException(e);
338           }
339         }
340       };
341     }
342     
343     for (Thread t : threads) t.start();
344     for (Thread t : threads) t.join();
345     
346     wldt.close();
347     
348     Set<String> ids = new HashSet<String>();
349     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
350     try {
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('_'));
359         ids.add(tname);
360         assertEquals(tname, parts[1].substring(parts[1].indexOf('_')));
361         assertEquals(tname, parts[2].substring(parts[2].indexOf('_')));
362       }
363       // only threads.length lines should exist
364       assertNull(br.readLine());
365       assertEquals(threads.length, ids.size());
366     } finally {
367       br.close();
368     }
369   }
370
371 }