|
| 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