2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.sal.dom.broker.impl;
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;
40 public class DOMBrokerPerformanceTest {
42 private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
44 private static NormalizedNode<?, ?> outerList(final int i) {
45 return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
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) //
54 private SchemaContext schemaContext;
55 private AbstractDOMDataBroker domBroker;
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();
62 return callable.call();
64 long endNano = System.nanoTime();
65 LOG.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
70 public void setupStore() {
71 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
72 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
73 schemaContext = TestModel.createTestContext();
75 operStore.onGlobalContextUpdated(schemaContext);
76 configStore.onGlobalContextUpdated(schemaContext);
78 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
79 .put(CONFIGURATION, configStore) //
80 .put(OPERATIONAL, operStore) //
82 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
83 domBroker = new SerializedDOMDataBroker(stores, executor);
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);
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);
108 private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
109 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);
118 assertEquals(txNum, transactions.size());
119 measure("Txs:"+ txNum + " Writes:1", (Callable<Void>) () -> {
121 for (DOMDataReadWriteTransaction writeTx :transactions) {
122 // Writes /test/outer-list/i in writeTx
123 writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
129 measure("Txs:"+ txNum + " Writes:" + innerNum, (Callable<Void>) () -> {
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))
143 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
151 measure("Txs:" + txNum + " Submit, Finish", (Callable<Void>) () -> {
152 List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
154 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
155 for (DOMDataReadWriteTransaction tx :transactions) {
156 builder.add(tx.submit());
160 Futures.allAsList(allFutures).get();
164 final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate",
165 (Callable<DOMDataReadTransaction>) () -> domBroker.newReadOnlyTransaction());
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,
172 assertTrue("outerList/" + i, potential.get().isPresent());
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))
183 .node(TestModel.INNER_LIST_QNAME)
184 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
186 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
187 assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
194 private void measureOneTransactionTopContainer() throws Exception {
196 final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
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());
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());
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());
221 measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
222 measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) () -> writeTx.submit()).get();