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;
16 import org.opendaylight.mdsal.dom.api.DOMDataReadTransaction;
17 import org.opendaylight.mdsal.dom.api.DOMDataReadWriteTransaction;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.ListeningExecutorService;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.Executors;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
33 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class DOMBrokerPerformanceTest {
44 private static final Logger log = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
46 private static NormalizedNode<?, ?> outerList(final int i) {
47 return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
50 private static YangInstanceIdentifier outerListPath(final int i) {
51 return YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
52 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i) //
56 private SchemaContext schemaContext;
57 private AbstractDOMDataBroker domBroker;
59 private static <V> V measure(final String name, final Callable<V> callable) throws Exception {
60 // TODO Auto-generated method stub
61 log.debug("Measurement:{} Start", name);
62 long startNano = System.nanoTime();
64 return callable.call();
66 long endNano = System.nanoTime();
67 log.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
72 public void setupStore() {
73 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
74 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
75 schemaContext = TestModel.createTestContext();
77 operStore.onGlobalContextUpdated(schemaContext);
78 configStore.onGlobalContextUpdated(schemaContext);
80 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
81 .put(CONFIGURATION, configStore) //
82 .put(OPERATIONAL, operStore) //
84 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
85 domBroker = new SerializedDOMDataBroker(stores, executor);
89 public void testPerformance() throws Exception {
90 measure("Test Suite (all tests)", new Callable<Void>() {
93 public Void call() throws Exception {
94 smallTestSuite(10, 1000);
95 //smallTestSuite(10, 100);
96 smallTestSuite(100, 100);
97 //smallTestSuite(100, 100);
98 //smallTestSuite(1000, 10);
99 smallTestSuite(1000, 10);
100 //smallTestSuite(1000, 1000);
106 private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
107 measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
110 public Void call() throws Exception {
111 measureOneTransactionTopContainer();
112 measureSeparateWritesOneLevel(txNum, innerListWriteNum);
118 private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
119 final List<DOMDataReadWriteTransaction> transactions = measure("Txs:"+ txNum + " Allocate",
120 new Callable<List<DOMDataReadWriteTransaction>>() {
122 public List<DOMDataReadWriteTransaction> call() throws Exception {
123 List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
124 for (int i = 0; i < txNum; i++) {
125 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
126 builder.add(writeTx);
131 assertEquals(txNum, transactions.size());
132 measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
134 public Void call() throws Exception {
136 for (DOMDataReadWriteTransaction writeTx :transactions) {
137 // Writes /test/outer-list/i in writeTx
138 writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
145 measure("Txs:"+ txNum + " Writes:" + innerNum, new Callable<Void>() {
147 public Void call() throws Exception {
149 for (DOMDataReadWriteTransaction writeTx :transactions) {
150 // Writes /test/outer-list/i in writeTx
151 YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
152 .node(TestModel.INNER_LIST_QNAME).build();
153 writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
154 for (int j = 0; j < innerNum; j++) {
155 YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
156 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
161 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
170 measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
172 public Void call() throws Exception {
173 List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
174 new Callable<List<ListenableFuture<?>>>() {
176 public List<ListenableFuture<?>> call() throws Exception {
177 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
178 for (DOMDataReadWriteTransaction tx :transactions) {
179 builder.add(tx.submit());
184 Futures.allAsList(allFutures).get();
189 final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
191 public DOMDataReadTransaction call() throws Exception {
192 return domBroker.newReadOnlyTransaction();
198 measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
200 public Void call() throws Exception {
201 for (int i = 0; i < txNum; i++) {
202 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
204 assertTrue("outerList/" + i, potential.get().isPresent());
210 measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
212 public Void call() throws Exception {
213 for (int i = 0; i < txNum; i++) {
214 for (int j = 0; j < innerNum; j++) {
215 YangInstanceIdentifier path = YangInstanceIdentifier
216 .builder(outerListPath(i))
218 .node(TestModel.INNER_LIST_QNAME)
219 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
221 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
222 assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
230 private void measureOneTransactionTopContainer() throws Exception {
232 final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
234 public DOMDataReadWriteTransaction call() throws Exception {
235 return domBroker.newReadWriteTransaction();
239 measure("Txs:1 Write", new Callable<Void>() {
241 public Void call() throws Exception {
242 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
243 writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
244 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
249 measure("Txs:1 Reads:1", new Callable<Void>() {
251 public Void call() throws Exception {
252 // Reads /test in writeTx
253 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
254 TestModel.TEST_PATH);
255 assertTrue(writeTxContainer.get().isPresent());
260 measure("Txs:1 Reads:1", new Callable<Void>() {
262 public Void call() throws Exception {
263 // Reads /test in writeTx
264 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
265 TestModel.TEST_PATH);
266 assertTrue(writeTxContainer.get().isPresent());
271 measure("Txs:1 Submit, Finish", new Callable<Void>() {
273 public Void call() throws Exception {
274 measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
276 public ListenableFuture<?> call() throws Exception {
277 return writeTx.submit();