31d3fdf733090741653e9ef0fa608fa12db342f6
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMBrokerPerformanceTest.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.md.sal.dom.broker.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
14 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
15
16 import com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.Executors;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
32 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @Deprecated
42 public class DOMBrokerPerformanceTest {
43
44     private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
45
46     private static NormalizedNode<?, ?> outerList(final int index) {
47         return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index);
48     }
49
50     private static YangInstanceIdentifier outerListPath(final int index) {
51         return YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
52                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index) //
53                 .build();
54     }
55
56     private SchemaContext schemaContext;
57     private AbstractDOMDataBroker domBroker;
58
59     private static <V> V measure(final String name, final Callable<V> callable) throws Exception {
60         // TODO Auto-generated method stub
61         LOG.debug("Measurement:{} Start", name);
62         long startNano = System.nanoTime();
63         try {
64             return callable.call();
65         } finally {
66             long endNano = System.nanoTime();
67             LOG.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
68         }
69     }
70
71     @Before
72     public void setupStore() {
73         InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
74         InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
75         schemaContext = TestModel.createTestContext();
76
77         operStore.onGlobalContextUpdated(schemaContext);
78         configStore.onGlobalContextUpdated(schemaContext);
79
80         ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder() //
81                 .put(CONFIGURATION, configStore) //
82                 .put(OPERATIONAL, operStore) //
83                 .build();
84         ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
85         domBroker = new SerializedDOMDataBroker(stores, executor);
86     }
87
88     @Test
89     public void testPerformance() throws Exception {
90         measure("Test Suite (all tests)", (Callable<Void>) () -> {
91             smallTestSuite(10, 1000);
92             //smallTestSuite(10, 100);
93             smallTestSuite(100, 100);
94             //smallTestSuite(100, 100);
95             //smallTestSuite(1000, 10);
96             smallTestSuite(1000, 10);
97             //smallTestSuite(1000, 1000);
98             return null;
99         });
100     }
101
102     private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
103         measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", (Callable<Void>) () -> {
104             measureOneTransactionTopContainer();
105             measureSeparateWritesOneLevel(txNum, innerListWriteNum);
106             return null;
107         });
108     }
109
110     private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
111         final List<DOMDataReadWriteTransaction> transactions = measure("Txs:" + txNum + " Allocate", () -> {
112             List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
113             for (int i = 0; i < txNum; i++) {
114                 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
115                 builder.add(writeTx);
116             }
117             return builder;
118         });
119         assertEquals(txNum, transactions.size());
120         measure("Txs:" + txNum + " Writes:1", (Callable<Void>) () -> {
121             int index = 0;
122             for (DOMDataReadWriteTransaction writeTx : transactions) {
123                 // Writes /test/outer-list/i in writeTx
124                 writeTx.put(OPERATIONAL, outerListPath(index), outerList(index));
125                 index++;
126             }
127             return null;
128         });
129
130         measure("Txs:" + txNum + " Writes:" + innerNum, (Callable<Void>) () -> {
131             int index = 0;
132             for (DOMDataReadWriteTransaction writeTx : transactions) {
133                 // Writes /test/outer-list/i in writeTx
134                 YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(index))
135                         .node(TestModel.INNER_LIST_QNAME).build();
136                 writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
137                 for (int j = 0; j < innerNum; j++) {
138                     YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
139                             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)).build();
140                     writeTx.put(OPERATIONAL, innerPath, ImmutableNodes
141                             .mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)));
142                 }
143                 index++;
144             }
145             return null;
146         });
147
148         measure("Txs:" + txNum + " Submit, Finish", (Callable<Void>) () -> {
149             List<ListenableFuture<?>> allFutures = measure(txNum + " Submits", () -> {
150                 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
151                 for (DOMDataReadWriteTransaction tx : transactions) {
152                     builder.add(tx.commit());
153                 }
154                 return builder;
155             });
156             Futures.allAsList(allFutures).get();
157             return null;
158         });
159
160         final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate",
161                                                       (Callable<DOMDataReadTransaction>) () -> domBroker
162                                                               .newReadOnlyTransaction());
163
164
165         measure("Txs:1 (ro) Reads:" + txNum + " (1-level)", (Callable<Void>) () -> {
166             for (int i = 0; i < txNum; i++) {
167                 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, outerListPath(i));
168                 assertTrue("outerList/" + i, potential.get().isPresent());
169             }
170             return null;
171         });
172
173         measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", (Callable<Void>) () -> {
174             for (int i = 0; i < txNum; i++) {
175                 for (int j = 0; j < innerNum; j++) {
176                     YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
177                             //
178                             .node(TestModel.INNER_LIST_QNAME)
179                             .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)).build();
180                     ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
181                     assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
182                 }
183             }
184             return null;
185         });
186     }
187
188     private void measureOneTransactionTopContainer() throws Exception {
189
190         final DOMDataReadWriteTransaction writeTx =
191                 measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
192
193         measure("Txs:1 Write", (Callable<Void>) () -> {
194             writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
195             writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
196                         ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
197             return null;
198         });
199
200         measure("Txs:1 Reads:1", (Callable<Void>) () -> {
201             // Reads /test in writeTx
202             ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx
203                     .read(OPERATIONAL, TestModel.TEST_PATH);
204             assertTrue(writeTxContainer.get().isPresent());
205             return null;
206         });
207
208         measure("Txs:1 Reads:1", (Callable<Void>) () -> {
209             // Reads /test in writeTx
210             ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx
211                     .read(OPERATIONAL, TestModel.TEST_PATH);
212             assertTrue(writeTxContainer.get().isPresent());
213             return null;
214         });
215
216         measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
217             measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) writeTx::commit).get();
218             return null;
219         });
220     }
221 }