|
| 1 | +/* |
| 2 | + * Copyright (c) 2017-2021 Nitrite author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.dizitart.no2.integration.collection; |
| 19 | + |
| 20 | +import org.dizitart.no2.Nitrite; |
| 21 | +import org.dizitart.no2.collection.Document; |
| 22 | +import org.dizitart.no2.collection.FindOptions; |
| 23 | +import org.dizitart.no2.collection.NitriteCollection; |
| 24 | +import org.dizitart.no2.common.SortOrder; |
| 25 | +import org.dizitart.no2.index.IndexOptions; |
| 26 | +import org.dizitart.no2.index.IndexType; |
| 27 | +import org.dizitart.no2.mvstore.MVStoreModule; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +import static org.dizitart.no2.filters.Filter.ALL; |
| 36 | +import static org.dizitart.no2.integration.TestUtil.deleteDb; |
| 37 | +import static org.dizitart.no2.integration.TestUtil.getRandomTempDbFile; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | + |
| 40 | +/** |
| 41 | + * The cost half of {@link CollectionSortedFindTest}, which needs a store that actually |
| 42 | + * serializes documents. |
| 43 | + * <p> |
| 44 | + * A blocking sort deserializes every stored document to read one field, so |
| 45 | + * {@code orderBy(indexed).limit(20)} cost what draining the whole collection cost - and the |
| 46 | + * gap grows with document size, not just document count. Taking the sort keys from the index |
| 47 | + * removes the decode: over 2000 rows carrying a 150-element array the sorted page went from |
| 48 | + * ~37 ms to ~1 ms. |
| 49 | + * |
| 50 | + * @author Anindya Chatterjee |
| 51 | + */ |
| 52 | +public class CollectionSortedFindCostTest { |
| 53 | + private static final int ROWS = 2000; |
| 54 | + |
| 55 | + private final String fileName = getRandomTempDbFile(); |
| 56 | + private Nitrite db; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setUp() { |
| 60 | + db = Nitrite.builder() |
| 61 | + .loadModule(MVStoreModule.withConfig().filePath(fileName).build()) |
| 62 | + .openOrCreate(); |
| 63 | + } |
| 64 | + |
| 65 | + @After |
| 66 | + public void tearDown() { |
| 67 | + if (db != null && !db.isClosed()) { |
| 68 | + db.close(); |
| 69 | + } |
| 70 | + deleteDb(fileName); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * The same query, the same row count, the same index - only the size of the documents |
| 75 | + * differs. A sorted page that decodes every row pays for the payload of every row, so |
| 76 | + * the fat collection costs many times the lean one. A sorted page that decodes only the |
| 77 | + * rows it returns costs the same either way. |
| 78 | + * <p> |
| 79 | + * Deliberately not "sorted page vs. full drain": both halves here are measured back to |
| 80 | + * back under whatever load the machine is under, and the only variable between them is |
| 81 | + * the thing that was broken. |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void testSortedPageCostDoesNotFollowDocumentSize() { |
| 85 | + double lean = sortedPageCost("lean", 0); |
| 86 | + double fat = sortedPageCost("fat", 150); |
| 87 | + |
| 88 | + assertTrue("a sorted page over fat documents took " + fat + "ms against " + lean |
| 89 | + + "ms over lean ones, same row count - it is still decoding rows it discards", |
| 90 | + fat < lean * 4); |
| 91 | + } |
| 92 | + |
| 93 | + private double sortedPageCost(String name, int payloadSize) { |
| 94 | + NitriteCollection collection = db.getCollection(name); |
| 95 | + collection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "seq"); |
| 96 | + |
| 97 | + for (int i = 0; i < ROWS; i++) { |
| 98 | + Document document = Document.createDocument("seq", i); |
| 99 | + if (payloadSize > 0) { |
| 100 | + List<Document> payload = new ArrayList<>(payloadSize); |
| 101 | + for (int w = 0; w < payloadSize; w++) { |
| 102 | + payload.add(Document.createDocument("text", "word" + w).put("start", w * 300)); |
| 103 | + } |
| 104 | + document.put("payload", payload); |
| 105 | + } |
| 106 | + collection.insert(document); |
| 107 | + } |
| 108 | + |
| 109 | + FindOptions page = FindOptions.orderBy("seq", SortOrder.Descending).limit(20); |
| 110 | + return timeOf(() -> { |
| 111 | + for (Document ignored : collection.find(ALL, page)) { |
| 112 | + // force the fetch |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + private static double timeOf(Runnable task) { |
| 118 | + task.run(); // warm |
| 119 | + long start = System.nanoTime(); |
| 120 | + for (int i = 0; i < 3; i++) { |
| 121 | + task.run(); |
| 122 | + } |
| 123 | + return (System.nanoTime() - start) / 3e6; |
| 124 | + } |
| 125 | +} |
0 commit comments