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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.