pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / tasks / ReadTokensTask.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.Reader;
21 import java.util.List;
22
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.TokenStream;
25 import org.apache.lucene.benchmark.byTask.PerfRunData;
26 import org.apache.lucene.benchmark.byTask.feeds.DocMaker;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Fieldable;
29 import org.apache.lucene.document.NumericField;
30
31 /**
32  * Simple task to test performance of tokenizers.  It just
33  * creates a token stream for each field of the document and
34  * read all tokens out of that stream.
35  */
36 public class ReadTokensTask extends PerfTask {
37
38   public ReadTokensTask(PerfRunData runData) {
39     super(runData);
40   }
41
42   private int totalTokenCount = 0;
43   
44   // volatile data passed between setup(), doLogic(), tearDown().
45   private Document doc = null;
46   
47   @Override
48   public void setup() throws Exception {
49     super.setup();
50     DocMaker docMaker = getRunData().getDocMaker();
51     doc = docMaker.makeDocument();
52   }
53
54   @Override
55   protected String getLogMessage(int recsCount) {
56     return "read " + recsCount + " docs; " + totalTokenCount + " tokens";
57   }
58   
59   @Override
60   public void tearDown() throws Exception {
61     doc = null;
62     super.tearDown();
63   }
64
65   @Override
66   public int doLogic() throws Exception {
67     List<Fieldable> fields = doc.getFields();
68     Analyzer analyzer = getRunData().getAnalyzer();
69     int tokenCount = 0;
70     for(final Fieldable field : fields) {
71       if (!field.isTokenized() || field instanceof NumericField) continue;
72       
73       final TokenStream stream;
74       final TokenStream streamValue = field.tokenStreamValue();
75
76       if (streamValue != null) 
77         stream = streamValue;
78       else {
79         // the field does not have a TokenStream,
80         // so we have to obtain one from the analyzer
81         final Reader reader;                      // find or make Reader
82         final Reader readerValue = field.readerValue();
83
84         if (readerValue != null)
85           reader = readerValue;
86         else {
87           String stringValue = field.stringValue();
88           if (stringValue == null)
89             throw new IllegalArgumentException("field must have either TokenStream, String or Reader value");
90           stringReader.init(stringValue);
91           reader = stringReader;
92         }
93         
94         // Tokenize field
95         stream = analyzer.reusableTokenStream(field.name(), reader);
96       }
97
98       // reset the TokenStream to the first token
99       stream.reset();
100
101       while(stream.incrementToken())
102         tokenCount++;
103     }
104     totalTokenCount += tokenCount;
105     return tokenCount;
106   }
107
108   /* Simple StringReader that can be reset to a new string;
109    * we use this when tokenizing the string value from a
110    * Field. */
111   ReusableStringReader stringReader = new ReusableStringReader();
112
113   private final static class ReusableStringReader extends Reader {
114     int upto;
115     int left;
116     String s;
117     
118     ReusableStringReader() {}
119     void init(String s) {
120       this.s = s;
121       left = s.length();
122       this.upto = 0;
123     }
124     @Override
125     public int read(char[] c) {
126       return read(c, 0, c.length);
127     }
128     @Override
129     public int read(char[] c, int off, int len) {
130       if (left > len) {
131         s.getChars(upto, upto+len, c, off);
132         upto += len;
133         left -= len;
134         return len;
135       } else if (0 == left) {
136         return -1;
137       } else {
138         s.getChars(upto, upto+left, c, off);
139         int r = left;
140         left = 0;
141         upto = s.length();
142         return r;
143       }
144     }
145     @Override
146     public void close() {}
147   }
148 }