2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.tree;
10 import com.google.common.collect.Streams;
11 import java.util.Arrays;
12 import java.util.concurrent.TimeUnit;
13 import java.util.stream.IntStream;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
18 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.data.tree.api.CursorAwareDataTreeModification;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
24 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.BenchmarkMode;
27 import org.openjdk.jmh.annotations.Fork;
28 import org.openjdk.jmh.annotations.Level;
29 import org.openjdk.jmh.annotations.Measurement;
30 import org.openjdk.jmh.annotations.Mode;
31 import org.openjdk.jmh.annotations.OutputTimeUnit;
32 import org.openjdk.jmh.annotations.Scope;
33 import org.openjdk.jmh.annotations.Setup;
34 import org.openjdk.jmh.annotations.State;
35 import org.openjdk.jmh.annotations.TearDown;
36 import org.openjdk.jmh.annotations.Warmup;
37 import org.openjdk.jmh.runner.Runner;
38 import org.openjdk.jmh.runner.RunnerException;
39 import org.openjdk.jmh.runner.options.Options;
40 import org.openjdk.jmh.runner.options.OptionsBuilder;
43 * Benchmarking of InMemoryDataTree performance. JMH is used for microbenchmarking.
45 * @author Lukas Sedlak <lsedlak@cisco.com>
46 * @see <a href="http://openjdk.java.net/projects/code-tools/jmh/">JMH</a>
49 @BenchmarkMode(Mode.AverageTime)
50 @OutputTimeUnit(TimeUnit.MILLISECONDS)
52 public class InMemoryDataTreeBenchmark {
54 private static final int WARMUP_ITERATIONS = 10;
55 private static final int MEASUREMENT_ITERATIONS = 10;
57 private static final int OUTER_LIST_100K = 100000;
58 private static final int OUTER_LIST_50K = 50000;
59 private static final int OUTER_LIST_10K = 10000;
61 private static final NodeIdentifierWithPredicates[] OUTER_LIST_IDS = Streams.mapWithIndex(
62 IntStream.range(0, OUTER_LIST_100K),
63 (i, index) -> NodeIdentifierWithPredicates.of(BenchmarkModel.OUTER_LIST_QNAME, BenchmarkModel.ID_QNAME, i))
64 .toArray(NodeIdentifierWithPredicates[]::new);
66 private static final YangInstanceIdentifier[] OUTER_LIST_PATHS = Arrays.stream(OUTER_LIST_IDS)
67 .map(id -> BenchmarkModel.OUTER_LIST_PATH.node(id).toOptimized())
68 .toArray(YangInstanceIdentifier[]::new);
70 private static final MapNode EMPTY_OUTER_LIST = ImmutableNodes.newSystemMapBuilder()
71 .withNodeIdentifier(BenchmarkModel.OUTER_LIST)
73 private static final MapNode ONE_ITEM_INNER_LIST = initInnerListItems(1);
74 private static final MapNode TWO_ITEM_INNER_LIST = initInnerListItems(2);
75 private static final MapNode TEN_ITEM_INNER_LIST = initInnerListItems(10);
77 private static MapNode initInnerListItems(final int count) {
78 final var mapEntryBuilder = ImmutableNodes.newSystemMapBuilder()
79 .withNodeIdentifier(BenchmarkModel.INNER_LIST);
81 for (int i = 0; i < count; ++i) {
83 .withChild(ImmutableNodes.mapEntry(BenchmarkModel.INNER_LIST_QNAME, BenchmarkModel.NAME_QNAME, i));
86 return mapEntryBuilder.build();
89 private static final MapEntryNode[] OUTER_LIST_ONE_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_100K,
91 private static final MapEntryNode[] OUTER_LIST_TWO_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_50K,
93 private static final MapEntryNode[] OUTER_LIST_TEN_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_10K,
96 private static MapEntryNode[] initOuterListItems(final int outerListItemsCount, final MapNode innerList) {
97 return Arrays.stream(OUTER_LIST_IDS)
98 .limit(outerListItemsCount)
99 .map(id -> ImmutableNodes.newMapEntryBuilder().withNodeIdentifier(id).withChild(innerList).build())
100 .toArray(MapEntryNode[]::new);
103 private DataTree datastore;
105 public static void main(final String... args) throws RunnerException {
106 Options opt = new OptionsBuilder()
107 .include(".*" + InMemoryDataTreeBenchmark.class.getSimpleName() + ".*")
111 new Runner(opt).run();
115 public void setup() throws DataValidationFailedException {
116 datastore = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
117 BenchmarkModel.createTestContext());
119 final DataTreeModification modification = begin();
120 modification.write(BenchmarkModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
121 .withNodeIdentifier(BenchmarkModel.TEST)
122 .withChild(EMPTY_OUTER_LIST)
124 commit(modification);
128 public void tearDown() {
133 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
134 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
135 public void write100KSingleNodeWithOneInnerItemInOneCommitBenchmark() throws DataValidationFailedException {
136 final DataTreeModification modification = begin();
137 for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
138 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
140 commit(modification);
144 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
145 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
146 public void write100KSingleNodeWithOneInnerItemInOneCommitCursorBenchmark() throws DataValidationFailedException {
147 final CursorAwareDataTreeModification modification = begin();
148 try (var cursor = modification.openCursor(BenchmarkModel.OUTER_LIST_PATH).orElseThrow()) {
149 for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
150 cursor.write(OUTER_LIST_IDS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
153 commit(modification);
157 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
158 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
159 public void write100KSingleNodeWithOneInnerItemInCommitPerWriteBenchmark() throws DataValidationFailedException {
160 for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
161 final DataTreeModification modification = begin();
162 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
163 commit(modification);
168 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
169 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
170 public void write50KSingleNodeWithTwoInnerItemsInOneCommitBenchmark() throws DataValidationFailedException {
171 final DataTreeModification modification = begin();
172 for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
173 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
175 commit(modification);
179 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
180 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
181 public void write50KSingleNodeWithTwoInnerItemsInOneCommitCursorBenchmark() throws DataValidationFailedException {
182 final CursorAwareDataTreeModification modification = begin();
183 try (var cursor = modification.openCursor(BenchmarkModel.OUTER_LIST_PATH).orElseThrow()) {
184 for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
185 cursor.write(OUTER_LIST_IDS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
188 commit(modification);
192 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
193 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
194 public void write50KSingleNodeWithTwoInnerItemsInCommitPerWriteBenchmark() throws DataValidationFailedException {
195 for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
196 final DataTreeModification modification = begin();
197 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
198 commit(modification);
203 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
204 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
205 public void write10KSingleNodeWithTenInnerItemsInOneCommitBenchmark() throws DataValidationFailedException {
206 final DataTreeModification modification = begin();
207 for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
208 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
210 commit(modification);
214 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
215 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
216 public void write10KSingleNodeWithTenInnerItemsInOneCommitCursorBenchmark() throws DataValidationFailedException {
217 final CursorAwareDataTreeModification modification = begin();
218 try (var cursor = modification.openCursor(BenchmarkModel.OUTER_LIST_PATH).orElseThrow()) {
219 for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
220 cursor.write(OUTER_LIST_IDS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
223 commit(modification);
227 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
228 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
229 public void write10KSingleNodeWithTenInnerItemsInCommitPerWriteBenchmark() throws DataValidationFailedException {
230 for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
231 final DataTreeModification modification = begin();
232 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
233 commit(modification);
237 private CursorAwareDataTreeModification begin() {
238 return (CursorAwareDataTreeModification) datastore.takeSnapshot().newModification();
241 private void commit(final DataTreeModification modification) throws DataValidationFailedException {
242 modification.ready();
243 datastore.validate(modification);
244 datastore.commit(datastore.prepare(modification));