Renamed controller.md.sal.dom.api to mdsal.dom.api
[mdsal.git] / dom / mdsal-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 org.opendaylight.mdsal.dom.api.DOMDataReadTransaction;
17 import org.opendaylight.mdsal.dom.api.DOMDataReadWriteTransaction;
18
19 import com.google.common.base.Optional;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.ListeningExecutorService;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.Executors;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
33 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class DOMBrokerPerformanceTest {
43
44     private static final Logger log = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
45
46     private static NormalizedNode<?, ?> outerList(final int i) {
47         return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
48     }
49
50     private static YangInstanceIdentifier outerListPath(final int i) {
51         return YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
52                 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i) //
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)", new Callable<Void>() {
91
92             @Override
93             public Void call() throws Exception {
94                 smallTestSuite(10, 1000);
95                 //smallTestSuite(10, 100);
96                 smallTestSuite(100, 100);
97                 //smallTestSuite(100, 100);
98                 //smallTestSuite(1000, 10);
99                 smallTestSuite(1000, 10);
100                 //smallTestSuite(1000, 1000);
101                 return null;
102             }
103         });
104     }
105
106     private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
107         measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
108
109             @Override
110             public Void call() throws Exception {
111                 measureOneTransactionTopContainer();
112                 measureSeparateWritesOneLevel(txNum, innerListWriteNum);
113                 return null;
114             }
115         });
116     }
117
118     private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
119         final List<DOMDataReadWriteTransaction> transactions = measure("Txs:"+ txNum + " Allocate",
120                 new Callable<List<DOMDataReadWriteTransaction>>() {
121                     @Override
122                     public List<DOMDataReadWriteTransaction> call() throws Exception {
123                         List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
124                         for (int i = 0; i < txNum; i++) {
125                             DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
126                             builder.add(writeTx);
127                         }
128                         return builder;
129                     }
130                 });
131         assertEquals(txNum, transactions.size());
132         measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
133             @Override
134             public Void call() throws Exception {
135                 int i = 0;
136                 for (DOMDataReadWriteTransaction writeTx :transactions) {
137                     // Writes /test/outer-list/i in writeTx
138                     writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
139                     i++;
140                 }
141                 return null;
142             }
143         });
144
145         measure("Txs:"+ txNum +  " Writes:" + innerNum, new Callable<Void>() {
146             @Override
147             public Void call() throws Exception {
148                 int i = 0;
149                 for (DOMDataReadWriteTransaction writeTx :transactions) {
150                     // Writes /test/outer-list/i in writeTx
151                     YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
152                             .node(TestModel.INNER_LIST_QNAME).build();
153                     writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
154                     for (int j = 0; j < innerNum; j++) {
155                         YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
156                                 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
157                                 .build();
158                         writeTx.put(
159                                 OPERATIONAL,
160                                 innerPath,
161                                 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
162                                         String.valueOf(j)));
163                     }
164                     i++;
165                 }
166                 return null;
167             }
168         });
169
170         measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
171             @Override
172             public Void call() throws Exception {
173                 List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
174                         new Callable<List<ListenableFuture<?>>>() {
175                             @Override
176                             public List<ListenableFuture<?>> call() throws Exception {
177                                 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
178                                 for (DOMDataReadWriteTransaction tx :transactions) {
179                                     builder.add(tx.submit());
180                                 }
181                                 return builder;
182                             }
183                         });
184                 Futures.allAsList(allFutures).get();
185                 return null;
186             }
187         });
188
189         final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
190             @Override
191             public DOMDataReadTransaction call() throws Exception {
192                 return domBroker.newReadOnlyTransaction();
193
194             }
195         });
196
197
198         measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
199             @Override
200             public Void call() throws Exception {
201                 for (int i = 0; i < txNum; i++) {
202                     ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
203                             outerListPath(i));
204                     assertTrue("outerList/" + i, potential.get().isPresent());
205                 }
206                 return null;
207             }
208         });
209
210         measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
211             @Override
212             public Void call() throws Exception {
213                 for (int i = 0; i < txNum; i++) {
214                     for (int j = 0; j < innerNum; j++) {
215                         YangInstanceIdentifier path = YangInstanceIdentifier
216                                 .builder(outerListPath(i))
217                                 //
218                                 .node(TestModel.INNER_LIST_QNAME)
219                                 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
220                                 .build();
221                         ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
222                         assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
223                     }
224                 }
225                 return null;
226             }
227         });
228     }
229
230     private void measureOneTransactionTopContainer() throws Exception {
231
232         final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
233             @Override
234             public DOMDataReadWriteTransaction call() throws Exception {
235                 return domBroker.newReadWriteTransaction();
236             }
237         });
238
239         measure("Txs:1 Write", new Callable<Void>() {
240             @Override
241             public Void call() throws Exception {
242                 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
243                 writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
244                         ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
245                 return null;
246             }
247         });
248
249         measure("Txs:1 Reads:1", new Callable<Void>() {
250             @Override
251             public Void call() throws Exception {
252                 // Reads /test in writeTx
253                 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
254                         TestModel.TEST_PATH);
255                 assertTrue(writeTxContainer.get().isPresent());
256                 return null;
257             }
258         });
259
260         measure("Txs:1 Reads:1", new Callable<Void>() {
261             @Override
262             public Void call() throws Exception {
263                 // Reads /test in writeTx
264                 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
265                         TestModel.TEST_PATH);
266                 assertTrue(writeTxContainer.get().isPresent());
267                 return null;
268             }
269         });
270
271         measure("Txs:1 Submit, Finish", new Callable<Void>() {
272             @Override
273             public Void call() throws Exception {
274                 measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
275                     @Override
276                     public ListenableFuture<?> call() throws Exception {
277                         return writeTx.submit();
278                     }
279                 }).get();
280                 return null;
281             }
282         });
283     }
284 }