Fix benchmark to compile on JDK8
[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 java.io.IOException;
11 import java.util.concurrent.TimeUnit;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
14 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.*;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.openjdk.jmh.annotations.*;
24 import org.openjdk.jmh.runner.Runner;
25 import org.openjdk.jmh.runner.RunnerException;
26 import org.openjdk.jmh.runner.options.Options;
27 import org.openjdk.jmh.runner.options.OptionsBuilder;
28
29 /**
30  * Benchmarking of InMemoryDataTree performance.
31  *
32  * JMH is used for microbenchmarking.
33  *
34  * @author Lukas Sedlak <lsedlak@cisco.com>
35  *
36  * @see <a href="http://openjdk.java.net/projects/code-tools/jmh/">JMH</a>
37  */
38 @State(Scope.Thread)
39 @BenchmarkMode(Mode.AverageTime)
40 @OutputTimeUnit(TimeUnit.MILLISECONDS)
41 @Fork(1)
42 public class InMemoryDataTreeBenchmark {
43
44     private static final int WARMUP_ITERATIONS = 20;
45     private static final int MEASUREMENT_ITERATIONS = 20;
46
47     private static final int OUTER_LIST_100K = 100000;
48     private static final int OUTER_LIST_50K = 50000;
49     private static final int OUTER_LIST_10K = 10000;
50
51     private static final YangInstanceIdentifier[] OUTER_LIST_100K_PATHS = initOuterListPaths(OUTER_LIST_100K);
52     private static final YangInstanceIdentifier[] OUTER_LIST_50K_PATHS = initOuterListPaths(OUTER_LIST_50K);
53     private static final YangInstanceIdentifier[] OUTER_LIST_10K_PATHS = initOuterListPaths(OUTER_LIST_10K);
54
55     private static YangInstanceIdentifier[] initOuterListPaths(final int outerListPathsCount) {
56         final YangInstanceIdentifier[] paths = new YangInstanceIdentifier[outerListPathsCount];
57
58         for (int outerListKey = 0; outerListKey < outerListPathsCount; ++outerListKey) {
59             paths[outerListKey] = YangInstanceIdentifier.builder(BenchmarkModel.OUTER_LIST_PATH)
60                 .nodeWithKey(BenchmarkModel.OUTER_LIST_QNAME, BenchmarkModel.ID_QNAME, outerListKey)
61                 .build();
62         }
63         return paths;
64     }
65
66     private static final MapNode ONE_ITEM_INNER_LIST = initInnerListItems(1);
67     private static final MapNode TWO_ITEM_INNER_LIST = initInnerListItems(2);
68     private static final MapNode TEN_ITEM_INNER_LIST = initInnerListItems(10);
69
70     private static MapNode initInnerListItems(final int count) {
71         final CollectionNodeBuilder<MapEntryNode, MapNode> mapEntryBuilder = ImmutableNodes
72             .mapNodeBuilder(BenchmarkModel.INNER_LIST_QNAME);
73
74         for (int i = 1; i <= count; ++i) {
75             mapEntryBuilder
76                 .withChild(ImmutableNodes.mapEntry(BenchmarkModel.INNER_LIST_QNAME, BenchmarkModel.NAME_QNAME, i));
77         }
78
79         return mapEntryBuilder.build();
80     }
81
82     private static final NormalizedNode<?, ?>[] OUTER_LIST_ONE_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_100K, ONE_ITEM_INNER_LIST);
83     private static final NormalizedNode<?, ?>[] OUTER_LIST_TWO_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_50K, TWO_ITEM_INNER_LIST);
84     private static final NormalizedNode<?, ?>[] OUTER_LIST_TEN_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_10K, TEN_ITEM_INNER_LIST);
85
86     private static NormalizedNode<?,?>[] initOuterListItems(int outerListItemsCount, MapNode innerList) {
87         final NormalizedNode<?,?>[] outerListItems = new NormalizedNode[outerListItemsCount];
88
89         for (int i = 0; i < outerListItemsCount; ++i) {
90             int outerListKey = i;
91             outerListItems[i] = ImmutableNodes.mapEntryBuilder(BenchmarkModel.OUTER_LIST_QNAME, BenchmarkModel.ID_QNAME, outerListKey)
92                 .withChild(innerList).build();
93         }
94         return outerListItems;
95     }
96
97     private SchemaContext schemaContext;
98     private DataTree datastore;
99
100     public static void main(String... args) throws IOException, RunnerException {
101         Options opt = new OptionsBuilder()
102             .include(".*" + InMemoryDataTreeBenchmark.class.getSimpleName() + ".*")
103             .forks(1)
104             .build();
105
106         new Runner(opt).run();
107     }
108
109     @Setup(Level.Trial)
110     public void setup() throws DataValidationFailedException {
111         schemaContext = BenchmarkModel.createTestContext();
112         final InMemoryDataTreeFactory factory = InMemoryDataTreeFactory.getInstance();
113         datastore = factory.create();
114         datastore.setSchemaContext(schemaContext);
115         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
116         initTestNode(snapshot);
117     }
118
119     @TearDown
120     public void tearDown() {
121         schemaContext = null;
122         datastore = null;
123     }
124
125     private void initTestNode(final DataTreeSnapshot snapshot) throws DataValidationFailedException {
126         final DataTreeModification modification = snapshot.newModification();
127         final YangInstanceIdentifier testPath = YangInstanceIdentifier.builder(BenchmarkModel.TEST_PATH)
128             .build();
129
130         modification.write(testPath, provideOuterListNode());
131         datastore.validate(modification);
132         final DataTreeCandidate candidate = datastore.prepare(modification);
133         datastore.commit(candidate);
134     }
135
136     private DataContainerChild<?, ?> provideOuterListNode() {
137         return ImmutableContainerNodeBuilder
138             .create()
139             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(BenchmarkModel.TEST_QNAME))
140             .withChild(
141                 ImmutableNodes.mapNodeBuilder(BenchmarkModel.OUTER_LIST_QNAME)
142                     .build()).build();
143     }
144
145     @Benchmark
146     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
147     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
148     public void write100KSingleNodeWithOneInnerItemInOneCommitBenchmark() throws Exception {
149         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
150         final DataTreeModification modification = snapshot.newModification();
151         for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
152             modification.write(OUTER_LIST_100K_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
153         }
154         datastore.validate(modification);
155         final DataTreeCandidate candidate = datastore.prepare(modification);
156         datastore.commit(candidate);
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 Exception {
163         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
164         for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
165             final DataTreeModification modification = snapshot.newModification();
166             modification.write(OUTER_LIST_100K_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
167             datastore.validate(modification);
168             final DataTreeCandidate candidate = datastore.prepare(modification);
169             datastore.commit(candidate);
170         }
171     }
172
173     @Benchmark
174     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
175     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
176     public void write50KSingleNodeWithTwoInnerItemsInOneCommitBenchmark() throws Exception {
177         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
178         final DataTreeModification modification = snapshot.newModification();
179         for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
180             modification.write(OUTER_LIST_50K_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
181         }
182         datastore.validate(modification);
183         final DataTreeCandidate candidate = datastore.prepare(modification);
184         datastore.commit(candidate);
185     }
186
187     @Benchmark
188     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
189     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
190     public void write50KSingleNodeWithTwoInnerItemsInCommitPerWriteBenchmark() throws Exception {
191         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
192         for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
193             final DataTreeModification modification = snapshot.newModification();
194             modification.write(OUTER_LIST_50K_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
195             datastore.validate(modification);
196             final DataTreeCandidate candidate = datastore.prepare(modification);
197             datastore.commit(candidate);
198         }
199     }
200
201     @Benchmark
202     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
203     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
204     public void write10KSingleNodeWithTenInnerItemsInOneCommitBenchmark() throws Exception {
205         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
206         final DataTreeModification modification = snapshot.newModification();
207         for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
208             modification.write(OUTER_LIST_10K_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
209         }
210         datastore.validate(modification);
211         final DataTreeCandidate candidate = datastore.prepare(modification);
212         datastore.commit(candidate);
213     }
214
215     @Benchmark
216     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
217     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
218     public void write10KSingleNodeWithTenInnerItemsInCommitPerWriteBenchmark() throws Exception {
219         final DataTreeSnapshot snapshot = datastore.takeSnapshot();
220         for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
221             final DataTreeModification modification = snapshot.newModification();
222             modification.write(OUTER_LIST_10K_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
223             datastore.validate(modification);
224             final DataTreeCandidate candidate = datastore.prepare(modification);
225             datastore.commit(candidate);
226         }
227     }
228 }