1 package org.opendaylight.controller.md.sal.dom.broker.impl;
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;
32 public class DOMBrokerPerformanceTest {
34 private static final Logger log = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
36 private static NormalizedNode<?, ?> outerList(final int i) {
37 return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
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) //
46 private SchemaContext schemaContext;
47 private AbstractDOMDataBroker domBroker;
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();
54 return callable.call();
56 long endNano = System.nanoTime();
57 log.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
62 public void setupStore() {
63 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
64 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
65 schemaContext = TestModel.createTestContext();
67 operStore.onGlobalContextUpdated(schemaContext);
68 configStore.onGlobalContextUpdated(schemaContext);
70 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
71 .put(CONFIGURATION, configStore) //
72 .put(OPERATIONAL, operStore) //
74 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
75 domBroker = new SerializedDOMDataBroker(stores, executor);
79 public void testPerformance() throws Exception {
80 measure("Test Suite (all tests)", new Callable<Void>() {
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);
96 private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
97 measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
100 public Void call() throws Exception {
101 measureOneTransactionTopContainer();
102 measureSeparateWritesOneLevel(txNum, innerListWriteNum);
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>>() {
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);
121 assertEquals(txNum, transactions.size());
122 measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
124 public Void call() throws Exception {
126 for (DOMDataReadWriteTransaction writeTx :transactions) {
127 // Writes /test/outer-list/i in writeTx
128 writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
135 measure("Txs:"+ txNum + " Writes:" + innerNum, new Callable<Void>() {
137 public Void call() throws Exception {
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))
151 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
160 measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
162 public Void call() throws Exception {
163 List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
164 new Callable<List<ListenableFuture<?>>>() {
166 public List<ListenableFuture<?>> call() throws Exception {
167 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
168 for (DOMDataReadWriteTransaction tx :transactions) {
169 builder.add(tx.submit());
174 Futures.allAsList(allFutures).get();
179 final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
181 public DOMDataReadTransaction call() throws Exception {
182 return domBroker.newReadOnlyTransaction();
188 measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
190 public Void call() throws Exception {
191 for (int i = 0; i < txNum; i++) {
192 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
194 assertTrue("outerList/" + i, potential.get().isPresent());
200 measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
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))
208 .node(TestModel.INNER_LIST_QNAME)
209 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
211 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
212 assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
220 private void measureOneTransactionTopContainer() throws Exception {
222 final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
224 public DOMDataReadWriteTransaction call() throws Exception {
225 return domBroker.newReadWriteTransaction();
229 measure("Txs:1 Write", new Callable<Void>() {
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());
239 measure("Txs:1 Reads:1", new Callable<Void>() {
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());
250 measure("Txs:1 Reads:1", new Callable<Void>() {
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());
261 measure("Txs:1 Submit, Finish", new Callable<Void>() {
263 public Void call() throws Exception {
264 measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
266 public ListenableFuture<?> call() throws Exception {
267 return writeTx.submit();