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