Deprecate simple DataTreeFactory.create()
[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.withChild(ImmutableNodes.newMapEntryBuilder()
83                 .withNodeIdentifier(
84                     NodeIdentifierWithPredicates.of(BenchmarkModel.INNER_LIST_QNAME, BenchmarkModel.NAME_QNAME, i))
85                 .withChild(ImmutableNodes.leafNode(BenchmarkModel.NAME_QNAME, i))
86                 .build());
87         }
88
89         return mapEntryBuilder.build();
90     }
91
92     private static final MapEntryNode[] OUTER_LIST_ONE_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_100K,
93         ONE_ITEM_INNER_LIST);
94     private static final MapEntryNode[] OUTER_LIST_TWO_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_50K,
95         TWO_ITEM_INNER_LIST);
96     private static final MapEntryNode[] OUTER_LIST_TEN_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_10K,
97         TEN_ITEM_INNER_LIST);
98
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);
104     }
105
106     private DataTree datastore;
107
108     public static void main(final String... args) throws RunnerException {
109         Options opt = new OptionsBuilder()
110             .include(".*" + InMemoryDataTreeBenchmark.class.getSimpleName() + ".*")
111             .forks(1)
112             .build();
113
114         new Runner(opt).run();
115     }
116
117     @Setup(Level.Trial)
118     public void setup() throws DataValidationFailedException {
119         datastore = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
120             BenchmarkModel.createTestContext());
121
122         final DataTreeModification modification = begin();
123         modification.write(BenchmarkModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
124             .withNodeIdentifier(BenchmarkModel.TEST)
125             .withChild(EMPTY_OUTER_LIST)
126             .build());
127         commit(modification);
128     }
129
130     @TearDown
131     public void tearDown() {
132         datastore = null;
133     }
134
135     @Benchmark
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]);
142         }
143         commit(modification);
144     }
145
146     @Benchmark
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]);
154             }
155         }
156         commit(modification);
157     }
158
159     @Benchmark
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);
167         }
168     }
169
170     @Benchmark
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]);
177         }
178         commit(modification);
179     }
180
181     @Benchmark
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]);
189             }
190         }
191         commit(modification);
192     }
193
194     @Benchmark
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);
202         }
203     }
204
205     @Benchmark
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]);
212         }
213         commit(modification);
214     }
215
216     @Benchmark
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]);
224             }
225         }
226         commit(modification);
227     }
228
229     @Benchmark
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);
237         }
238     }
239
240     private CursorAwareDataTreeModification begin() {
241         return (CursorAwareDataTreeModification) datastore.takeSnapshot().newModification();
242     }
243
244     private void commit(final DataTreeModification modification) throws DataValidationFailedException {
245         modification.ready();
246         datastore.validate(modification);
247         datastore.commit(datastore.prepare(modification));
248     }
249 }