Merge "Bug#1854 - Exit command in console causing OOM."
[controller.git] / opendaylight / md-sal / benchmark-data-store / src / main / java / org / opendaylight / controller / md / sal / dom / store / benchmark / AbstractInMemoryDatastoreWriteTransactionBenchmark.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.controller.md.sal.dom.store.benchmark;
9
10 import java.util.concurrent.TimeUnit;
11 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
12 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
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.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.openjdk.jmh.annotations.Benchmark;
24 import org.openjdk.jmh.annotations.Measurement;
25 import org.openjdk.jmh.annotations.Warmup;
26
27 /**
28  * @author Lukas Sedlak <lsedlak@cisco.com>
29  */
30 public abstract class AbstractInMemoryDatastoreWriteTransactionBenchmark {
31
32     private static final int WARMUP_ITERATIONS = 20;
33     private static final int MEASUREMENT_ITERATIONS = 20;
34
35     private static final int OUTER_LIST_100K = 100000;
36     private static final int OUTER_LIST_50K = 50000;
37     private static final int OUTER_LIST_10K = 10000;
38
39     private static final YangInstanceIdentifier[] OUTER_LIST_100K_PATHS = initOuterListPaths(OUTER_LIST_100K);
40     private static final YangInstanceIdentifier[] OUTER_LIST_50K_PATHS = initOuterListPaths(OUTER_LIST_50K);
41     private static final YangInstanceIdentifier[] OUTER_LIST_10K_PATHS = initOuterListPaths(OUTER_LIST_10K);
42
43     private static YangInstanceIdentifier[] initOuterListPaths(final int outerListPathsCount) {
44         final YangInstanceIdentifier[] paths = new YangInstanceIdentifier[outerListPathsCount];
45
46         for (int outerListKey = 0; outerListKey < outerListPathsCount; ++outerListKey) {
47             paths[outerListKey] = YangInstanceIdentifier.builder(BenchmarkModel.OUTER_LIST_PATH)
48                 .nodeWithKey(BenchmarkModel.OUTER_LIST_QNAME, BenchmarkModel.ID_QNAME, outerListKey)
49                 .build();
50         }
51         return paths;
52     }
53
54     private static final MapNode ONE_ITEM_INNER_LIST = initInnerListItems(1);
55     private static final MapNode TWO_ITEM_INNER_LIST = initInnerListItems(2);
56     private static final MapNode TEN_ITEM_INNER_LIST = initInnerListItems(10);
57
58     private static MapNode initInnerListItems(final int count) {
59         final CollectionNodeBuilder<MapEntryNode, MapNode> mapEntryBuilder = ImmutableNodes
60             .mapNodeBuilder(BenchmarkModel.INNER_LIST_QNAME);
61
62         for (int i = 1; i <= count; ++i) {
63             mapEntryBuilder
64                 .withChild(ImmutableNodes.mapEntry(BenchmarkModel.INNER_LIST_QNAME, BenchmarkModel.NAME_QNAME, i));
65         }
66         return mapEntryBuilder.build();
67     }
68
69     private static final NormalizedNode<?, ?>[] OUTER_LIST_ONE_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_100K, ONE_ITEM_INNER_LIST);
70     private static final NormalizedNode<?, ?>[] OUTER_LIST_TWO_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_50K, TWO_ITEM_INNER_LIST);
71     private static final NormalizedNode<?, ?>[] OUTER_LIST_TEN_ITEM_INNER_LIST = initOuterListItems(OUTER_LIST_10K, TEN_ITEM_INNER_LIST);
72
73     private static NormalizedNode<?,?>[] initOuterListItems(int outerListItemsCount, MapNode innerList) {
74         final NormalizedNode<?,?>[] outerListItems = new NormalizedNode[outerListItemsCount];
75
76         for (int i = 0; i < outerListItemsCount; ++i) {
77             int outerListKey = i;
78             outerListItems[i] = ImmutableNodes.mapEntryBuilder(BenchmarkModel.OUTER_LIST_QNAME, BenchmarkModel.ID_QNAME, outerListKey)
79                 .withChild(innerList).build();
80         }
81         return outerListItems;
82     }
83
84     protected SchemaContext schemaContext;
85     protected InMemoryDOMDataStore domStore;
86
87     abstract public void setUp() throws Exception;
88
89     abstract public void tearDown();
90
91     protected void initTestNode() throws Exception {
92         final YangInstanceIdentifier testPath = YangInstanceIdentifier.builder(BenchmarkModel.TEST_PATH)
93             .build();
94         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
95         writeTx.write(testPath, provideOuterListNode());
96
97         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
98         cohort.canCommit().get();
99         cohort.preCommit().get();
100         cohort.commit().get();
101     }
102
103     private DataContainerChild<?, ?> provideOuterListNode() {
104         return ImmutableContainerNodeBuilder
105             .create()
106             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(BenchmarkModel.TEST_QNAME))
107             .withChild(
108                 ImmutableNodes.mapNodeBuilder(BenchmarkModel.OUTER_LIST_QNAME)
109                     .build()).build();
110     }
111
112     @Benchmark
113     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
114     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
115     public void write100KSingleNodeWithOneInnerItemInOneCommitBenchmark() throws Exception {
116         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
117         for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
118             writeTx.write(OUTER_LIST_100K_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
119         }
120         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
121         cohort.canCommit().get();
122         cohort.preCommit().get();
123         cohort.commit().get();
124     }
125
126     @Benchmark
127     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
128     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
129     public void write100KSingleNodeWithOneInnerItemInCommitPerWriteBenchmark() throws Exception {
130         for (int outerListKey = 0; outerListKey < OUTER_LIST_100K; ++outerListKey) {
131             DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
132             writeTx.write(OUTER_LIST_100K_PATHS[outerListKey], OUTER_LIST_ONE_ITEM_INNER_LIST[outerListKey]);
133
134             DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
135             cohort.canCommit().get();
136             cohort.preCommit().get();
137             cohort.commit().get();
138         }
139     }
140
141     @Benchmark
142     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
143     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
144     public void write50KSingleNodeWithTwoInnerItemsInOneCommitBenchmark() throws Exception {
145         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
146         for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
147             writeTx.write(OUTER_LIST_50K_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
148         }
149         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
150         cohort.canCommit().get();
151         cohort.preCommit().get();
152         cohort.commit().get();
153     }
154
155     @Benchmark
156     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
157     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
158     public void write50KSingleNodeWithTwoInnerItemsInCommitPerWriteBenchmark() throws Exception {
159         for (int outerListKey = 0; outerListKey < OUTER_LIST_50K; ++outerListKey) {
160             DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
161             writeTx.write(OUTER_LIST_50K_PATHS[outerListKey], OUTER_LIST_TWO_ITEM_INNER_LIST[outerListKey]);
162             DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
163             cohort.canCommit().get();
164             cohort.preCommit().get();
165             cohort.commit().get();
166         }
167     }
168
169     @Benchmark
170     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
171     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
172     public void write10KSingleNodeWithTenInnerItemsInOneCommitBenchmark() throws Exception {
173         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
174         for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
175             writeTx.write(OUTER_LIST_10K_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
176         }
177         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
178         cohort.canCommit().get();
179         cohort.preCommit().get();
180         cohort.commit().get();
181     }
182
183     @Benchmark
184     @Warmup(iterations = WARMUP_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
185     @Measurement(iterations = MEASUREMENT_ITERATIONS, timeUnit = TimeUnit.MILLISECONDS)
186     public void write10KSingleNodeWithTenInnerItemsInCommitPerWriteBenchmark() throws Exception {
187         for (int outerListKey = 0; outerListKey < OUTER_LIST_10K; ++outerListKey) {
188             DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
189             writeTx.write(OUTER_LIST_10K_PATHS[outerListKey], OUTER_LIST_TEN_ITEM_INNER_LIST[outerListKey]);
190             DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
191             cohort.canCommit().get();
192             cohort.preCommit().get();
193             cohort.commit().get();
194         }
195     }
196 }