Skip to content

Commit c736ad5

Browse files
committed
[SPIKE] Comparing speed of two ES7 approaches: single vs multi index
1 parent 48da60f commit c736ad5

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

java-utils-elasticsearch/src/main/java/ca/nrc/dtrc/elasticsearch/ESTestHelpers.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,27 @@ public ESTestHelpers(ESFactory _esFactory) throws ElasticSearchException {
100100
this.esFactory = _esFactory;
101101
}
102102

103-
public ESTestHelpers(Integer version) throws ElasticSearchException {
103+
public ESTestHelpers(Integer version) throws Exception {
104+
init_ESTestHelpers(version, (Boolean)null);
105+
}
106+
107+
public ESTestHelpers(Integer version, Boolean multiIndex) throws Exception {
108+
init_ESTestHelpers(version, multiIndex);
109+
}
110+
111+
private void init_ESTestHelpers(Integer version, Boolean multiIndex) throws Exception {
112+
if (multiIndex == null) {
113+
multiIndex = false;
114+
}
104115
if (version != null) {
105116
if (version <= 5) {
106117
esFactory = new ES5Factory("");
107118
} else if (version <= 7) {
108-
esFactory = new ES7Factory("");
119+
if (multiIndex) {
120+
esFactory = new ES7miFactory("");
121+
} else {
122+
esFactory = new ES7Factory("");
123+
}
109124
} else {
110125
throw new ElasticSearchException("Unsupported ESFactory version "+version);
111126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ca.nrc.dtrc.elasticsearch.es7;
2+
3+
import ca.nrc.dtrc.elasticsearch.DocIDIterator;
4+
import ca.nrc.dtrc.elasticsearch.ESFactory;
5+
import ca.nrc.dtrc.elasticsearch.ESTestHelpers;
6+
import static ca.nrc.dtrc.elasticsearch.ESTestHelpers.PlayLine;
7+
8+
import ca.nrc.dtrc.elasticsearch.SearchResults;
9+
import ca.nrc.dtrc.elasticsearch.es7mi.ES7miFactory;
10+
import ca.nrc.testing.AssertNumber;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
/**
18+
* This class compares the speed of the es7mi vs es7 apprach.
19+
* - es7mi: Stores different types of documents in different indices
20+
* - es7: Stores all types in same index, but with a 'type' field to differenciate them
21+
*/
22+
public class SpeedComparison__MultiVersuSingleIndexTest {
23+
24+
protected ESFactory esFactory(String indexName, Boolean multiIndex)
25+
throws Exception {
26+
ESFactory factory = null;
27+
if (multiIndex) {
28+
factory = new ES7miFactory(indexName);
29+
} else {
30+
factory = new ES7Factory(indexName);
31+
}
32+
return factory;
33+
}
34+
35+
@Test
36+
public void test__listAll__SpeedComparison() throws Exception {
37+
Assertions.fail("TODO: Implement this test");
38+
39+
ESFactory factoryMultiIndex = new ESTestHelpers(7, true).makeHamletTestIndex();
40+
ESFactory factorySigleIndex = new ESTestHelpers(7, false).makeHamletTestIndex();
41+
42+
Map<ESFactory,Long> elapsedTimes = new HashMap<ESFactory,Long>();
43+
for (ESFactory esFactory: new ESFactory[] {factoryMultiIndex, factoryMultiIndex}) {
44+
PlayLine proto = new PlayLine();
45+
Long startTime = System.currentTimeMillis();
46+
try(SearchResults<PlayLine> results =
47+
esFactory.indexAPI().listAll(proto)) {
48+
DocIDIterator<PlayLine> iter = results.docIDIterator();
49+
while (iter.hasNext()) {
50+
iter.next();
51+
}
52+
}
53+
Long endTime = System.currentTimeMillis();
54+
Long elapsedTime = endTime - startTime;
55+
elapsedTimes.put(esFactory, elapsedTime);
56+
}
57+
58+
double gotRatio = 1.0 * elapsedTimes.get(factoryMultiIndex) / elapsedTimes.get(factorySigleIndex);
59+
double expMinRatio = 1000;
60+
// System.out.println("batchSize=null : "+elapsedTimes.get(null));
61+
// System.out.println("batchSize=1000 : "+elapsedTimes.get(new Integer(1000)));
62+
// System.out.println("null/1000 ratio : "+gotRatio);
63+
AssertNumber.isGreaterOrEqualTo(
64+
"listAll with multi-index should have been much faster than single-index approach",
65+
gotRatio, expMinRatio);
66+
}
67+
}

0 commit comments

Comments
 (0)