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 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;
41 public class DOMBrokerPerformanceTest {
43 private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
45 private static NormalizedNode<?, ?> outerList(final int index) {
46 return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index);
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) //
55 private SchemaContext schemaContext;
56 private AbstractDOMDataBroker domBroker;
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();
63 return callable.call();
65 long endNano = System.nanoTime();
66 LOG.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
71 public void setupStore() {
72 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
73 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
74 schemaContext = TestModel.createTestContext();
76 operStore.onGlobalContextUpdated(schemaContext);
77 configStore.onGlobalContextUpdated(schemaContext);
79 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder() //
80 .put(CONFIGURATION, configStore) //
81 .put(OPERATIONAL, operStore) //
83 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
84 domBroker = new SerializedDOMDataBroker(stores, executor);
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);
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);
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);
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(index), outerList(index));
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(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)));
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());
155 Futures.allAsList(allFutures).get();
159 final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate",
160 (Callable<DOMDataReadTransaction>) () -> domBroker
161 .newReadOnlyTransaction());
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());
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))
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());
187 private void measureOneTransactionTopContainer() throws Exception {
189 final DOMDataReadWriteTransaction writeTx =
190 measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
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());
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());
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());
215 measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
216 measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) writeTx::submit).get();