Last active
August 29, 2015 14:08
-
-
Save tteofili/52856d938fcd465eab58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.tteofili.samples.lucene.benchmarks; | |
| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| * (the "License"); you may not use this file except in compliance with | |
| * the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| import java.io.IOException; | |
| import java.util.Random; | |
| import org.apache.lucene.analysis.Analyzer; | |
| import org.apache.lucene.analysis.MockAnalyzer; | |
| import org.apache.lucene.document.BinaryDocValuesField; | |
| import org.apache.lucene.document.Document; | |
| import org.apache.lucene.document.Field; | |
| import org.apache.lucene.document.FieldType; | |
| import org.apache.lucene.document.SortedSetDocValuesField; | |
| import org.apache.lucene.document.TextField; | |
| import org.apache.lucene.index.RandomIndexWriter; | |
| import org.apache.lucene.index.Term; | |
| import org.apache.lucene.search.ConstantScoreQuery; | |
| import org.apache.lucene.search.FieldValueFilter; | |
| import org.apache.lucene.search.Filter; | |
| import org.apache.lucene.search.IndexSearcher; | |
| import org.apache.lucene.search.MatchAllDocsQuery; | |
| import org.apache.lucene.search.Query; | |
| import org.apache.lucene.search.TermQuery; | |
| import org.apache.lucene.search.TermRangeQuery; | |
| import org.apache.lucene.search.TopDocs; | |
| import org.apache.lucene.search.WildcardQuery; | |
| import org.apache.lucene.store.Directory; | |
| import org.apache.lucene.util.BytesRef; | |
| import org.apache.lucene.util.LuceneTestCase; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| /** | |
| * benchmark test for "field exists" query | |
| */ | |
| public class FieldExistBenchmarkTest extends LuceneTestCase { | |
| private String changing = "changing"; | |
| private String constant = "constant"; | |
| private String even = "even"; | |
| private String fields = "fields"; | |
| private String changing_dv = "changing_dv"; | |
| private String constant_dv = "constant_dv"; | |
| private String even_dv = "even_dv"; | |
| private RandomIndexWriter indexWriter; | |
| private Directory dir; | |
| private FieldType textFt; | |
| @Before | |
| public void setUp() throws Exception { | |
| super.setUp(); | |
| dir = newDirectory(); | |
| indexWriter = new RandomIndexWriter(new Random(), dir); | |
| textFt = new FieldType(TextField.TYPE_NOT_STORED); | |
| } | |
| @Override | |
| @After | |
| public void tearDown() throws Exception { | |
| super.tearDown(); | |
| indexWriter.close(); | |
| dir.close(); | |
| } | |
| @Test | |
| public void testFieldExists() throws Exception { | |
| populateIndex(new MockAnalyzer(new Random())); | |
| IndexSearcher indexSearcher = new IndexSearcher(indexWriter.getReader()); | |
| // warm up caches | |
| indexSearcher.search(new MatchAllDocsQuery(), 100000); | |
| // queries on changing field | |
| benchmarkQuery(new TermRangeQuery(changing, null, null, true, true), null, indexSearcher); | |
| benchmarkQuery(new WildcardQuery(new Term(changing, "*")), null, indexSearcher); | |
| benchmarkQuery(new TermQuery(new Term(fields, changing)), null, indexSearcher); | |
| benchmarkQuery(new ConstantScoreQuery(new FieldValueFilter(changing_dv)), null, indexSearcher); | |
| benchmarkQuery(new MatchAllDocsQuery(), new FieldValueFilter(changing_dv), indexSearcher); | |
| // queries on constant field | |
| benchmarkQuery(new TermRangeQuery(constant, null, null, true, true), null, indexSearcher); | |
| benchmarkQuery(new WildcardQuery(new Term(constant, "*")), null, indexSearcher); | |
| benchmarkQuery(new TermQuery(new Term(fields, constant)), null, indexSearcher); | |
| benchmarkQuery(new ConstantScoreQuery(new FieldValueFilter(constant_dv)), null, indexSearcher); | |
| benchmarkQuery(new MatchAllDocsQuery(), new FieldValueFilter(constant_dv), indexSearcher); | |
| // queries on even field | |
| benchmarkQuery(new TermRangeQuery(even, null, null, true, true), null, indexSearcher); | |
| benchmarkQuery(new WildcardQuery(new Term(even, "*")), null, indexSearcher); | |
| benchmarkQuery(new TermQuery(new Term(fields, even)), null, indexSearcher); | |
| benchmarkQuery(new ConstantScoreQuery(new FieldValueFilter(even_dv)), null, indexSearcher); | |
| benchmarkQuery(new MatchAllDocsQuery(), new FieldValueFilter(even_dv), indexSearcher); | |
| } | |
| private void benchmarkQuery(Query query, Filter filter, IndexSearcher indexSearcher) throws Exception { | |
| System.out.println(query + (filter != null ? " " + filter : "")); | |
| long start = System.currentTimeMillis(); | |
| TopDocs search; | |
| if (filter == null) { | |
| search = indexSearcher.search(query, Integer.MAX_VALUE); | |
| } else { | |
| search = indexSearcher.search(query, filter, Integer.MAX_VALUE); | |
| } | |
| System.out.println(System.currentTimeMillis() - start + "ms"); | |
| System.out.println(search.totalHits + " hits"); | |
| } | |
| private String createVectorString(int x, int y, int z) { | |
| StringBuilder text = new StringBuilder(); | |
| for (int i = 0; i < x; i++) | |
| text.append("x "); | |
| for (int i = 0; i < y; i++) | |
| text.append("y "); | |
| for (int i = 0; i < z; i++) | |
| text.append("z "); | |
| return text.toString(); | |
| } | |
| private Document createDoc(int x, int y, int z, String token) { | |
| Document doc = new Document(); | |
| String vectorString = createVectorString(x, y, z); | |
| String randomValue = vectorString != null ? vectorString : String.valueOf(random().nextInt(1000000)); | |
| doc.add(new Field(changing, randomValue, textFt)); | |
| doc.add(new BinaryDocValuesField(changing_dv, new BytesRef(randomValue))); | |
| doc.add(new Field(constant, token, textFt)); | |
| doc.add(new BinaryDocValuesField(constant_dv, new BytesRef(token))); | |
| if (x % 2 == 0) { | |
| doc.add(new Field(even, token, textFt)); | |
| doc.add(new BinaryDocValuesField(even_dv, new BytesRef(token))); | |
| doc.add(new Field(fields, even, textFt)); | |
| } | |
| doc.add(new Field(fields, changing, textFt)); | |
| doc.add(new Field(fields, constant, textFt)); | |
| return doc; | |
| } | |
| private void addDoc(int x, int y, int z, String className, Analyzer analyzer) throws IOException { | |
| Document doc = createDoc(x, y, z, className); | |
| indexWriter.addDocument(doc, analyzer); | |
| } | |
| public void populateIndex(Analyzer analyzer) throws IOException { | |
| indexWriter.deleteAll(); | |
| indexWriter.commit(); | |
| String constantToken = Character.toString((char) random().nextInt(255)); | |
| for (int i = 0; i < 100000; i++) { | |
| int seed = (i % 254) + 1; | |
| addDoc(random().nextInt(seed), random().nextInt(seed), random().nextInt(seed), constantToken, analyzer); | |
| } | |
| indexWriter.commit(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this because of the fact that you don't have a common base (e.g. wrt Analyzers, Codecs, etc.) or because there's some other LuceneTestCase specific limitation?
I thought that randomization would have been a good thing also for benchmarks because (on a significant bunch of runs) it allows to find out what's the most performant approach on average, regardless of the other variables.