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) {
82 mapEntryBuilder.withChild(ImmutableNodes.newMapEntryBuilder()
84 NodeIdentifierWithPredicates.of(BenchmarkModel.INNER_LIST_QNAME, BenchmarkModel.NAME_QNAME, i))
85 .withChild(ImmutableNodes.leafNode(BenchmarkModel.NAME_QNAME, i))
89 return mapEntryBuilder.build();
92 private static final MapEntryNode[] OUTER_LIST_ONE_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_100K,
94 private static final MapEntryNode[] OUTER_LIST_TWO_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_50K,
96 private static final MapEntryNode[] OUTER_LIST_TEN_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_10K,
99 private static MapEntryNode[] initOuterListItems(final int outerListItemsCount, final MapNode innerList) {
100 return Arrays.stream(OUTER_LIST_IDS)
101 .limit(outerListItemsCount)
102 .map(id -> ImmutableNodes.newMapEntryBuilder().withNodeIdentifier(id).withChild(innerList).build())
103 .toArray(MapEntryNode[]::new);
106 private DataTree datastore;
108 public static void main(final String... args) throws RunnerException {
109 Options opt = new OptionsBuilder()
110 .include(".*" + InMemoryDataTreeBenchmark.class.getSimpleName() + ".*")
114 new Runner(opt).run();
118 public void setup() throws DataValidationFailedException {
119 datastore = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
120 BenchmarkModel.createTestContext());
122 final DataTreeModification modification = begin();
123 modification.write(BenchmarkModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
124 .withNodeIdentifier(BenchmarkModel.TEST)
125 .withChild(EMPTY_OUTER_LIST)
127 commit(modification);
131 public void tearDown() {
136 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
137 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
138 public void write100KSingleNodeWithOneInnerItemInOneCommitBenchmark() throws DataValidationFailedException {
139 final DataTreeModification modification = begin();
140 for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
141 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
143 commit(modification);
147 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
148 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
149 public void write100KSingleNodeWithOneInnerItemInOneCommitCursorBenchmark() throws DataValidationFailedException {
150 final CursorAwareDataTreeModification modification = begin();
151 try (var cursor = modification.openCursor(BenchmarkModel.OUTER_LIST_PATH).orElseThrow()) {
152 for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
153 cursor.write(OUTER_LIST_IDS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
156 commit(modification);
160 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
161 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
162 public void write100KSingleNodeWithOneInnerItemInCommitPerWriteBenchmark() throws DataValidationFailedException {
163 for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
164 final DataTreeModification modification = begin();
165 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
166 commit(modification);
171 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
172 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
173 public void write50KSingleNodeWithTwoInnerItemsInOneCommitBenchmark() throws DataValidationFailedException {
174 final DataTreeModification modification = begin();
175 for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
176 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
178 commit(modification);
182 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
183 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
184 public void write50KSingleNodeWithTwoInnerItemsInOneCommitCursorBenchmark() throws DataValidationFailedException {
185 final CursorAwareDataTreeModification modification = begin();
186 try (var cursor = modification.openCursor(BenchmarkModel.OUTER_LIST_PATH).orElseThrow()) {
187 for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
188 cursor.write(OUTER_LIST_IDS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
191 commit(modification);
195 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
196 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
197 public void write50KSingleNodeWithTwoInnerItemsInCommitPerWriteBenchmark() throws DataValidationFailedException {
198 for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
199 final DataTreeModification modification = begin();
200 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
201 commit(modification);
206 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
207 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
208 public void write10KSingleNodeWithTenInnerItemsInOneCommitBenchmark() throws DataValidationFailedException {
209 final DataTreeModification modification = begin();
210 for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
211 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
213 commit(modification);
217 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
218 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
219 public void write10KSingleNodeWithTenInnerItemsInOneCommitCursorBenchmark() throws DataValidationFailedException {
220 final CursorAwareDataTreeModification modification = begin();
221 try (var cursor = modification.openCursor(BenchmarkModel.OUTER_LIST_PATH).orElseThrow()) {
222 for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
223 cursor.write(OUTER_LIST_IDS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
226 commit(modification);
230 @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
231 @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
232 public void write10KSingleNodeWithTenInnerItemsInCommitPerWriteBenchmark() throws DataValidationFailedException {
233 for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
234 final DataTreeModification modification = begin();
235 modification.write(OUTER_LIST_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
236 commit(modification);
240 private CursorAwareDataTreeModification begin() {
241 return (CursorAwareDataTreeModification) datastore.takeSnapshot().newModification();
244 private void commit(final DataTreeModification modification) throws DataValidationFailedException {
245 modification.ready();
246 datastore.validate(modification);
247 datastore.commit(datastore.prepare(modification));