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;
42 public class DOMBrokerPerformanceTest {
44 private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
46 private static NormalizedNode<?, ?> outerList(final int index) {
47 return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index);
50 private static YangInstanceIdentifier outerListPath(final int index) {
51 return YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
52 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index) //
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)", (Callable<Void>) () -> {
91 smallTestSuite(10, 1000);
92 //smallTestSuite(10, 100);
93 smallTestSuite(100, 100);
94 //smallTestSuite(100, 100);
95 //smallTestSuite(1000, 10);
96 smallTestSuite(1000, 10);
97 //smallTestSuite(1000, 1000);
102 private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
103 measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", (Callable<Void>) () -> {
104 measureOneTransactionTopContainer();
105 measureSeparateWritesOneLevel(txNum, innerListWriteNum);
110 private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
111 final List<DOMDataReadWriteTransaction> transactions = measure("Txs:" + txNum + " Allocate", () -> {
112 List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
113 for (int i = 0; i < txNum; i++) {
114 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
115 builder.add(writeTx);
119 assertEquals(txNum, transactions.size());
120 measure("Txs:" + txNum + " Writes:1", (Callable<Void>) () -> {
122 for (DOMDataReadWriteTransaction writeTx : transactions) {
123 // Writes /test/outer-list/i in writeTx
124 writeTx.put(OPERATIONAL, outerListPath(index), outerList(index));
130 measure("Txs:" + txNum + " Writes:" + innerNum, (Callable<Void>) () -> {
132 for (DOMDataReadWriteTransaction writeTx : transactions) {
133 // Writes /test/outer-list/i in writeTx
134 YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(index))
135 .node(TestModel.INNER_LIST_QNAME).build();
136 writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
137 for (int j = 0; j < innerNum; j++) {
138 YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
139 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)).build();
140 writeTx.put(OPERATIONAL, innerPath, ImmutableNodes
141 .mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)));
148 measure("Txs:" + txNum + " Submit, Finish", (Callable<Void>) () -> {
149 List<ListenableFuture<?>> allFutures = measure(txNum + " Submits", () -> {
150 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
151 for (DOMDataReadWriteTransaction tx : transactions) {
152 builder.add(tx.commit());
156 Futures.allAsList(allFutures).get();
160 final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate",
161 (Callable<DOMDataReadTransaction>) () -> domBroker
162 .newReadOnlyTransaction());
165 measure("Txs:1 (ro) Reads:" + txNum + " (1-level)", (Callable<Void>) () -> {
166 for (int i = 0; i < txNum; i++) {
167 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, outerListPath(i));
168 assertTrue("outerList/" + i, potential.get().isPresent());
173 measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", (Callable<Void>) () -> {
174 for (int i = 0; i < txNum; i++) {
175 for (int j = 0; j < innerNum; j++) {
176 YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
178 .node(TestModel.INNER_LIST_QNAME)
179 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)).build();
180 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
181 assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
188 private void measureOneTransactionTopContainer() throws Exception {
190 final DOMDataReadWriteTransaction writeTx =
191 measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
193 measure("Txs:1 Write", (Callable<Void>) () -> {
194 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
195 writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
196 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
200 measure("Txs:1 Reads:1", (Callable<Void>) () -> {
201 // Reads /test in writeTx
202 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx
203 .read(OPERATIONAL, TestModel.TEST_PATH);
204 assertTrue(writeTxContainer.get().isPresent());
208 measure("Txs:1 Reads:1", (Callable<Void>) () -> {
209 // Reads /test in writeTx
210 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx
211 .read(OPERATIONAL, TestModel.TEST_PATH);
212 assertTrue(writeTxContainer.get().isPresent());
216 measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
217 measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) writeTx::commit).get();