/
BenchmarkFacets.java
240 lines (198 loc) · 12.9 KB
/
BenchmarkFacets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package perf.facets;
/**
* 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.
*/
// javac -d ../../../../build -cp ../../../../../../lucene/lucene/core/build/libs/lucene-core-10.0.0-SNAPSHOT.jar:../../../../../../lucene/lucene/facet/build/libs/lucene-facet-10.0.0-SNAPSHOT.jar:../../../../build BenchmarkFacets.java
// java -cp ../../../../../../lucene/lucene/core/build/libs/lucene-core-10.0.0-SNAPSHOT.jar:../../../../../../lucene/lucene/facet/build/libs/lucene-facet-10.0.0-SNAPSHOT.jar:../../../../build perf.facets.BenchmarkFacets
// java -cp ../../../../../../lucene/lucene/core/build/libs/lucene-core-10.0.0-SNAPSHOT.jar:../../../../../../lucene/lucene/facet/build/libs/lucene-facet-10.0.0-SNAPSHOT.jar:../../../../build perf.facets.BenchmarkFacets ../../../../../indices/NADFacets numIters
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsConfig;
import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.ReferenceManager;
import org.apache.lucene.store.Directory;
import perf.OpenDirectory;
public class BenchmarkFacets {
public static double MILLION = 1_000_000;
public static String SSDV_FIELD_NAME = "address.sortedset";
public static String TAXO_FIELD_NAME = "address.taxonomy";
public static void main(String[] args) throws IOException {
File indexPath = new File(args[0]);
int numIters = Integer.parseInt(args[1]);
final Directory dir;
OpenDirectory od = OpenDirectory.get("NIOFSDirectory");
dir = od.open(indexPath.toPath());
IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
IndexWriter writer = new IndexWriter(dir, iwc);
final ReferenceManager<IndexSearcher> mgr = new SearcherManager(writer, new SearcherFactory() {
@Override
public IndexSearcher newSearcher(IndexReader reader, IndexReader previous) {
IndexSearcher s = new IndexSearcher(reader);
s.setQueryCache(null); // don't bench the cache
return s;
}
});
IndexSearcher s = mgr.acquire();
final Directory taxoDir;
File taxoPath = new File(args[0] + "/taxonomy");
taxoDir = od.open(taxoPath.toPath());
DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
FacetsConfig config = new FacetsConfig();
config.setIndexFieldName(TAXO_FIELD_NAME, TAXO_FIELD_NAME);
config.setHierarchical(TAXO_FIELD_NAME, true);
config.setHierarchical(SSDV_FIELD_NAME, true);
try {
// SSDV state creation
SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(s.getIndexReader(), config);
System.out.println("Warming up...");
// Warmup
for (int i = 0; i < 5; i++) {
FacetsCollector c = new FacetsCollector();
s.search(new MatchAllDocsQuery(), c);
Facets taxoFacets = new FastTaxonomyFacetCounts(TAXO_FIELD_NAME, taxoReader, config, c);
Facets ssdvFacets = new SortedSetDocValuesFacetCounts(state, c);
taxoFacets.getAllDims(5);
ssdvFacets.getAllDims(5);
taxoFacets.getTopDims(5, 5);
ssdvFacets.getTopDims(5, 5);
taxoFacets.getTopChildren(10, TAXO_FIELD_NAME, "TX");
ssdvFacets.getTopChildren(10, SSDV_FIELD_NAME, "TX");
taxoFacets.getAllChildren(TAXO_FIELD_NAME, "TX");
ssdvFacets.getAllChildren(SSDV_FIELD_NAME, "TX");
}
System.out.println("Number of docs: " + s.getIndexReader().numDocs());
System.out.println("Number of segments: " + s.getIndexReader().leaves().size());
System.out.println("Running benchmark...");
double totalCreateFastTaxoCountsTimeMS = 0;
double totalCreateSSDVCountsTimeMS = 0;
double totalTaxoGetAllDimsTimeMS = 0;
double totalSSDVGetAllDimsTimeMS = 0;
double totalTaxoGetTopDimsTimeMS = 0;
double totalSSDVGetTopDimsTimeMS = 0;
double totalTaxoGetTopChildrenTimeMS = 0;
double totalSSDVGetTopChildrenTimeMS = 0;
double totalTaxoGetAllChildrenTimeMS = 0;
double totalSSDVGetAllChildrenTimeMS = 0;
for (int i = 1; i < numIters + 1; i++) {
FacetsCollector c = new FacetsCollector();
s.search(new MatchAllDocsQuery(), c);
long createFastTaxoCountsStartNS = System.nanoTime();
Facets taxoFacets = new FastTaxonomyFacetCounts(TAXO_FIELD_NAME, taxoReader, config, c);
long createFastTaxoCountsEndNS = System.nanoTime();
double createFastTaxoCountsTimeMS = (double)(createFastTaxoCountsEndNS - createFastTaxoCountsStartNS) / MILLION;
totalCreateFastTaxoCountsTimeMS += createFastTaxoCountsTimeMS;
long createSSDVReaderCountsStartNS = System.nanoTime();
Facets ssdvFacets = new SortedSetDocValuesFacetCounts(state, c);
long createSSDVReaderCountsEndNS = System.nanoTime();
double createSSDVReaderStateTimeMS = (double)(createSSDVReaderCountsEndNS - createSSDVReaderCountsStartNS) / MILLION;
totalCreateSSDVCountsTimeMS += createSSDVReaderStateTimeMS;
long taxoGetAllDimsStartNS = System.nanoTime();
List<FacetResult> results = taxoFacets.getAllDims(5);
long taxoGetAllDimsEndNS = System.nanoTime();
double taxoGetAllDimsTimeMS = (double) (taxoGetAllDimsEndNS - taxoGetAllDimsStartNS) / MILLION;
totalTaxoGetAllDimsTimeMS += taxoGetAllDimsTimeMS;
long ssdvGetAllDimsStartNS = System.nanoTime();
results = ssdvFacets.getAllDims(5);
long ssdvGetAllDimsEndNS = System.nanoTime();
double ssdvGetAllDimsTimeMS = (double) (ssdvGetAllDimsEndNS - ssdvGetAllDimsStartNS) / MILLION;
totalSSDVGetAllDimsTimeMS += ssdvGetAllDimsTimeMS;
long taxoGetTopDimsStartNS = System.nanoTime();
results = taxoFacets.getTopDims(5, 5);
long taxoGetTopDimsEndNS = System.nanoTime();
double taxoGetTopDimsTimeMS = (double) (taxoGetTopDimsEndNS - taxoGetTopDimsStartNS) / MILLION;
totalTaxoGetTopDimsTimeMS += taxoGetTopDimsTimeMS;
long ssdvGetTopDimsStartNS = System.nanoTime();
results = ssdvFacets.getTopDims(5, 5);
long ssdvGetTopDimsEndNS = System.nanoTime();
double ssdvGetTopDimsTimeMS = (double) (ssdvGetTopDimsEndNS - ssdvGetTopDimsStartNS) / MILLION;
totalSSDVGetTopDimsTimeMS += ssdvGetTopDimsTimeMS;
long taxoGetTopChildrenStartNS = System.nanoTime();
FacetResult result = taxoFacets.getTopChildren(10, TAXO_FIELD_NAME, "TX");
long taxoGetTopChildrenEndNS = System.nanoTime();
double taxoGetTopChildrenTimeMS = (double) (taxoGetTopChildrenEndNS - taxoGetTopChildrenStartNS) / MILLION;
totalTaxoGetTopChildrenTimeMS += taxoGetTopChildrenTimeMS;
long ssdvGetTopChildrenStartNS = System.nanoTime();
result = ssdvFacets.getTopChildren(10, SSDV_FIELD_NAME, "TX");
long ssdvGetTopChildrenEndNS = System.nanoTime();
double ssdvGetTopChildrenTimeMS = (double) (ssdvGetTopChildrenEndNS - ssdvGetTopChildrenStartNS) / MILLION;
totalSSDVGetTopChildrenTimeMS += ssdvGetTopChildrenTimeMS;
long taxoGetAllChildrenStartNS = System.nanoTime();
result = taxoFacets.getAllChildren(TAXO_FIELD_NAME, "TX");
long taxoGetAllChildrenEndNS = System.nanoTime();
double taxoGetAllChildrenTimeMS = (double) (taxoGetAllChildrenEndNS - taxoGetAllChildrenStartNS) / MILLION;
totalTaxoGetAllChildrenTimeMS += taxoGetAllChildrenTimeMS;
long ssdvGetAllChildrenStartNS = System.nanoTime();
result = ssdvFacets.getAllChildren(SSDV_FIELD_NAME, "TX");
long ssdvGetAllChildrenEndNS = System.nanoTime();
double ssdvGetAllChildrenTimeMS = (double) (ssdvGetAllChildrenEndNS - ssdvGetAllChildrenStartNS) / MILLION;
totalSSDVGetAllChildrenTimeMS += ssdvGetAllChildrenTimeMS;
System.out.println("Results after iteration " + i + "\n");
System.out.println("Time (ms) taken to instanciate FastTaxonomyFacetCounts: " + totalCreateFastTaxoCountsTimeMS / (double) i);
System.out.println("Time (ms) taken to instanciate SSDVFacetCounts: " + totalCreateSSDVCountsTimeMS / (double) i);
reportPercentDifference(totalCreateFastTaxoCountsTimeMS / (double) i, totalCreateSSDVCountsTimeMS / (double) i);
System.out.println("");
System.out.println("Time (ms) taken to get all dims for taxonomy: " + totalTaxoGetAllDimsTimeMS / (double) i);
System.out.println("Time (ms) taken to get all dims for SSDV: " + totalSSDVGetAllDimsTimeMS / (double) i);
reportPercentDifference(totalTaxoGetAllDimsTimeMS / (double) i, totalSSDVGetAllDimsTimeMS / (double) i);
System.out.println("");
System.out.println("Time (ms) taken to get top dims for taxonomy: " + totalTaxoGetTopDimsTimeMS / (double) i);
System.out.println("Time (ms) taken to get top dims for SSDV: " + totalSSDVGetTopDimsTimeMS / (double) i);
reportPercentDifference(totalTaxoGetTopDimsTimeMS / (double) i, totalSSDVGetTopDimsTimeMS / (double) i);
System.out.println("");
System.out.println("Time (ms) taken to get top \"address.taxonomy/TX\" children for taxonomy: " + totalTaxoGetTopChildrenTimeMS / (double) i);
System.out.println("Time (ms) taken to get top \"address.sortedset/TX\" children for SSDV: " + totalSSDVGetTopChildrenTimeMS / (double) i);
reportPercentDifference(totalTaxoGetTopChildrenTimeMS / (double) i, totalSSDVGetTopChildrenTimeMS / (double) i);
System.out.println("Time (ms) taken to get all \"address.taxonomy/TX\" children for taxonomy: " + totalTaxoGetAllChildrenTimeMS / (double) i);
System.out.println("Time (ms) taken to get all \"address.sortedset/TX\" children for SSDV: " + totalSSDVGetAllChildrenTimeMS / (double) i);
reportPercentDifference(totalTaxoGetAllChildrenTimeMS / (double) i, totalSSDVGetAllChildrenTimeMS / (double) i);
System.out.print("\n");
}
} finally {
mgr.release(s);
}
}
public static void reportPercentDifference(double taxo, double ssdv) {
if (taxo == ssdv) {
System.out.println("No difference");
return;
}
double larger = taxo > ssdv ? taxo : ssdv;
double smaller = taxo > ssdv ? ssdv : taxo;
double percent = ((larger - smaller) / smaller) * 100;
String percentString = String.format("%.1f", percent);
if (taxo == larger) {
System.out.println("Taxonomy is " + percentString + "% slower than SSDV");
} else {
System.out.println("SSDV is " + percentString + "% slower than Taxonomy");
}
}
}