Add ImmutableNode.newXYXBuilder() methods
[yangtools.git] / benchmarks / src / main / java / org / opendaylight / yangtools / yang / data / impl / tree / InMemoryDataTreeBenchmark.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.tree;
9
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;
41
42 /**
43  * Benchmarking of InMemoryDataTree performance. JMH is used for microbenchmarking.
44  *
45  * @author Lukas Sedlak <lsedlak@cisco.com>
46  * @see <a href="http://openjdk.java.net/projects/code-tools/jmh/">JMH</a>
47  */
48 @State(Scope.Thread)
49 @BenchmarkMode(Mode.AverageTime)
50 @OutputTimeUnit(TimeUnit.MILLISECONDS)
51 @Fork(1)
52 public class InMemoryDataTreeBenchmark {
53
54     private static final int WARMUP_ITERATIONS = 10;
55     private static final int MEASUREMENT_ITERATIONS = 10;
56
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;
60
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);
65
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);
69
70     private static final MapNode EMPTY_OUTER_LIST = ImmutableNodes.newSystemMapBuilder()
71         .withNodeIdentifier(BenchmarkModel.OUTER_LIST)
72         .build();
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);
76
77     private static MapNode initInnerListItems(final int count) {
78         final var mapEntryBuilder = ImmutableNodes.newSystemMapBuilder()
79             .withNodeIdentifier(BenchmarkModel.INNER_LIST);
80
81         for (int i = 0; i < count; ++i) {
82             mapEntryBuilder
83                 .withChild(ImmutableNodes.mapEntry(BenchmarkModel.INNER_LIST_QNAME, BenchmarkModel.NAME_QNAME, i));
84         }
85
86         return mapEntryBuilder.build();
87     }
88
89     private static final MapEntryNode[] OUTER_LIST_ONE_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_100K,
90         ONE_ITEM_INNER_LIST);
91     private static final MapEntryNode[] OUTER_LIST_TWO_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_50K,
92         TWO_ITEM_INNER_LIST);
93     private static final MapEntryNode[] OUTER_LIST_TEN_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_10K,
94         TEN_ITEM_INNER_LIST);
95
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);
101     }
102
103     private DataTree datastore;
104
105     public static void main(final String... args) throws RunnerException {
106         Options opt = new OptionsBuilder()
107             .include(".*" + InMemoryDataTreeBenchmark.class.getSimpleName() + ".*")
108             .forks(1)
109             .build();
110
111         new Runner(opt).run();
112     }
113
114     @Setup(Level.Trial)
115     public void setup() throws DataValidationFailedException {
116         datastore = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
117             BenchmarkModel.createTestContext());
118
119         final DataTreeModification modification = begin();
120         modification.write(BenchmarkModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
121             .withNodeIdentifier(BenchmarkModel.TEST)
122             .withChild(EMPTY_OUTER_LIST)
123             .build());
124         commit(modification);
125     }
126
127     @TearDown
128     public void tearDown() {
129         datastore = null;
130     }
131
132     @Benchmark
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]);
139         }
140         commit(modification);
141     }
142
143     @Benchmark
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]);
151             }
152         }
153         commit(modification);
154     }
155
156     @Benchmark
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);
164         }
165     }
166
167     @Benchmark
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]);
174         }
175         commit(modification);
176     }
177
178     @Benchmark
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]);
186             }
187         }
188         commit(modification);
189     }
190
191     @Benchmark
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);
199         }
200     }
201
202     @Benchmark
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]);
209         }
210         commit(modification);
211     }
212
213     @Benchmark
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]);
221             }
222         }
223         commit(modification);
224     }
225
226     @Benchmark
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);
234         }
235     }
236
237     private CursorAwareDataTreeModification begin() {
238         return (CursorAwareDataTreeModification) datastore.takeSnapshot().newModification();
239     }
240
241     private void commit(final DataTreeModification modification) throws DataValidationFailedException {
242         modification.ready();
243         datastore.validate(modification);
244         datastore.commit(datastore.prepare(modification));
245     }
246 }