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