|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.benchmarks.jmh.tree; |
| 19 | + |
| 20 | +import java.util.concurrent.ThreadLocalRandom; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import org.apache.ignite.IgniteCheckedException; |
| 23 | +import org.apache.ignite.IgniteDataStreamer; |
| 24 | +import org.apache.ignite.Ignition; |
| 25 | +import org.apache.ignite.cache.query.annotations.QuerySqlField; |
| 26 | +import org.apache.ignite.configuration.CacheConfiguration; |
| 27 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 28 | +import org.apache.ignite.internal.IgniteEx; |
| 29 | +import org.apache.ignite.internal.cache.query.index.sorted.IndexKeyType; |
| 30 | +import org.apache.ignite.internal.cache.query.index.sorted.IndexRow; |
| 31 | +import org.apache.ignite.internal.cache.query.index.sorted.IndexSearchRowImpl; |
| 32 | +import org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndex; |
| 33 | +import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey; |
| 34 | +import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKeyFactory; |
| 35 | +import org.apache.ignite.internal.util.lang.GridCursor; |
| 36 | +import org.openjdk.jmh.annotations.Benchmark; |
| 37 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 38 | +import org.openjdk.jmh.annotations.Fork; |
| 39 | +import org.openjdk.jmh.annotations.Level; |
| 40 | +import org.openjdk.jmh.annotations.Measurement; |
| 41 | +import org.openjdk.jmh.annotations.Mode; |
| 42 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 43 | +import org.openjdk.jmh.annotations.Scope; |
| 44 | +import org.openjdk.jmh.annotations.Setup; |
| 45 | +import org.openjdk.jmh.annotations.State; |
| 46 | +import org.openjdk.jmh.annotations.TearDown; |
| 47 | +import org.openjdk.jmh.annotations.Warmup; |
| 48 | +import org.openjdk.jmh.runner.Runner; |
| 49 | +import org.openjdk.jmh.runner.options.Options; |
| 50 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 51 | + |
| 52 | +/** |
| 53 | + * Index find benchmark. |
| 54 | + */ |
| 55 | +@State(Scope.Benchmark) |
| 56 | +@Fork(1) |
| 57 | +@BenchmarkMode(Mode.Throughput) |
| 58 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 59 | +@Warmup(iterations = 3, time = 3) |
| 60 | +@Measurement(iterations = 5, time = 10) |
| 61 | +public class IndexFindBenchmark { |
| 62 | + /** Items count. */ |
| 63 | + private static final int CNT = 1_000_000; |
| 64 | + |
| 65 | + /** Items in each range. */ |
| 66 | + private static final int RANGE = 1; |
| 67 | + |
| 68 | + /** Cache name. */ |
| 69 | + private static final String CACHE_NAME = "cache"; |
| 70 | + |
| 71 | + /** Ignite. */ |
| 72 | + private IgniteEx ignite; |
| 73 | + |
| 74 | + /** */ |
| 75 | + InlineIndex idxId; |
| 76 | + |
| 77 | + /** */ |
| 78 | + InlineIndex idxName; |
| 79 | + |
| 80 | + /** */ |
| 81 | + InlineIndex idxSalary; |
| 82 | + |
| 83 | + /** */ |
| 84 | + @Benchmark |
| 85 | + public void findOneIndex() { |
| 86 | + int key = ThreadLocalRandom.current().nextInt(CNT - RANGE); |
| 87 | + |
| 88 | + find(idxSalary, searchRowSalary(key), searchRowSalary(key + RANGE)); |
| 89 | + } |
| 90 | + |
| 91 | + /** */ |
| 92 | + @Benchmark |
| 93 | + public void findThreeIndexes() { |
| 94 | + int key = ThreadLocalRandom.current().nextInt(CNT); |
| 95 | + |
| 96 | + find(idxId, searchRowId(key), searchRowId(key + RANGE)); |
| 97 | + find(idxName, searchRowName(key), searchRowName(key + RANGE)); |
| 98 | + find(idxSalary, searchRowSalary(key), searchRowSalary(key + RANGE)); |
| 99 | + } |
| 100 | + |
| 101 | + /** */ |
| 102 | + private static void find(InlineIndex idx, IndexRow lower, IndexRow upper) { |
| 103 | + try { |
| 104 | + GridCursor<IndexRow> cur = idx.find(lower, upper, true, false, 0, null); |
| 105 | + |
| 106 | + int cnt = 0; |
| 107 | + |
| 108 | + while (cur.next()) |
| 109 | + cnt++; |
| 110 | + |
| 111 | + assert cnt == RANGE; |
| 112 | + } |
| 113 | + catch (IgniteCheckedException e) { |
| 114 | + throw new AssertionError(e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** */ |
| 119 | + private static IndexRow searchRow(Object key, IndexKeyType type) { |
| 120 | + IndexKey[] keys = new IndexKey[] { IndexKeyFactory.wrap(key, type, null, null), null }; |
| 121 | + return new IndexSearchRowImpl(keys, null); |
| 122 | + } |
| 123 | + |
| 124 | + /** */ |
| 125 | + private static IndexRow searchRowId(int key) { |
| 126 | + return searchRow(key, IndexKeyType.INT); |
| 127 | + } |
| 128 | + |
| 129 | + /** */ |
| 130 | + private static IndexRow searchRowName(int key) { |
| 131 | + return searchRow("name" + String.format("%07d", key), IndexKeyType.STRING); |
| 132 | + } |
| 133 | + |
| 134 | + /** */ |
| 135 | + private static IndexRow searchRowSalary(int key) { |
| 136 | + return searchRow(key * 1_000d, IndexKeyType.DOUBLE); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Initiate Ignite and caches. |
| 141 | + */ |
| 142 | + @Setup(Level.Trial) |
| 143 | + public void setup() { |
| 144 | + ignite = (IgniteEx)Ignition.start(new IgniteConfiguration().setIgniteInstanceName("test")); |
| 145 | + |
| 146 | + CacheConfiguration<Integer, Person> cfg = new CacheConfiguration<>(CACHE_NAME); |
| 147 | + cfg.setIndexedTypes(Integer.class, Person.class); |
| 148 | + |
| 149 | + ignite.getOrCreateCache(cfg); |
| 150 | + |
| 151 | + try (IgniteDataStreamer<Integer, Person> dataLdr = ignite.dataStreamer(CACHE_NAME)) { |
| 152 | + for (int i = 0; i < CNT; i++) |
| 153 | + dataLdr.addData(i, new Person(i, "name" + String.format("%07d", i), i * 1_000d)); |
| 154 | + } |
| 155 | + |
| 156 | + for (InlineIndex idx : ignite.context().indexProcessor().treeIndexes(CACHE_NAME, true)) { |
| 157 | + if (idx.name().contains("_ID_")) |
| 158 | + idxId = idx; |
| 159 | + else if (idx.name().contains("_NAME_")) |
| 160 | + idxName = idx; |
| 161 | + else if (idx.name().contains("_SALARY_")) |
| 162 | + idxSalary = idx; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Stop Ignite instance. |
| 168 | + */ |
| 169 | + @TearDown |
| 170 | + public void tearDown() { |
| 171 | + ignite.close(); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Run benchmarks. |
| 176 | + * |
| 177 | + * @param args Args. |
| 178 | + * @throws Exception Exception. |
| 179 | + */ |
| 180 | + public static void main(String[] args) throws Exception { |
| 181 | + final Options options = new OptionsBuilder() |
| 182 | + .include(IndexFindBenchmark.class.getSimpleName()) |
| 183 | + .build(); |
| 184 | + |
| 185 | + new Runner(options).run(); |
| 186 | + } |
| 187 | + |
| 188 | + /** */ |
| 189 | + private static class Person { |
| 190 | + /** */ |
| 191 | + @QuerySqlField(index = true) |
| 192 | + private final int id; |
| 193 | + |
| 194 | + /** */ |
| 195 | + @QuerySqlField(index = true) |
| 196 | + private final String name; |
| 197 | + |
| 198 | + /** */ |
| 199 | + @QuerySqlField(index = true) |
| 200 | + private final double salary; |
| 201 | + |
| 202 | + /** */ |
| 203 | + private Person(int id, String name, double salary) { |
| 204 | + this.id = id; |
| 205 | + this.name = name; |
| 206 | + this.salary = salary; |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments