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;
8 import java.util.ArrayList;
10 import java.util.concurrent.Callable;
11 import java.util.concurrent.Executors;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
19 import org.opendaylight.controller.md.sal.dom.store.impl.TestModel;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Optional;
29 import com.google.common.collect.ImmutableMap;
30 import com.google.common.util.concurrent.Futures;
31 import com.google.common.util.concurrent.ListenableFuture;
32 import com.google.common.util.concurrent.ListeningExecutorService;
33 import com.google.common.util.concurrent.MoreExecutors;
35 public class DOMBrokerPerformanceTest {
37 private static final Logger log = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
39 private static NormalizedNode<?, ?> outerList(final int i) {
40 return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
43 private static InstanceIdentifier outerListPath(final int i) {
44 return InstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
45 .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i) //
49 private SchemaContext schemaContext;
50 private DOMDataBrokerImpl domBroker;
52 private static <V> V measure(final String name, final Callable<V> callable) throws Exception {
53 // TODO Auto-generated method stub
54 log.debug("Measurement:{} Start", name);
55 long startNano = System.nanoTime();
57 return callable.call();
59 long endNano = System.nanoTime();
60 log.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
65 public void setupStore() {
66 InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
67 InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
68 schemaContext = TestModel.createTestContext();
70 operStore.onGlobalContextUpdated(schemaContext);
71 configStore.onGlobalContextUpdated(schemaContext);
73 ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
74 .put(CONFIGURATION, configStore) //
75 .put(OPERATIONAL, operStore) //
77 ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
78 domBroker = new DOMDataBrokerImpl(stores, executor);
82 public void testPerformance() throws Exception {
83 measure("Test Suite (all tests)", new Callable<Void>() {
86 public Void call() throws Exception {
87 smallTestSuite(10, 1000);
88 //smallTestSuite(10, 100);
89 smallTestSuite(100, 100);
90 //smallTestSuite(100, 100);
91 //smallTestSuite(1000, 10);
92 smallTestSuite(1000, 10);
93 //smallTestSuite(1000, 1000);
99 private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
100 measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
103 public Void call() throws Exception {
104 measureOneTransactionTopContainer();
105 measureSeparateWritesOneLevel(txNum, innerListWriteNum);
111 private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
112 final List<DOMDataReadWriteTransaction> transactions = measure("Txs:"+ txNum + " Allocate",
113 new Callable<List<DOMDataReadWriteTransaction>>() {
115 public List<DOMDataReadWriteTransaction> call() throws Exception {
116 List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
117 for (int i = 0; i < txNum; i++) {
118 DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
119 builder.add(writeTx);
124 assertEquals(txNum, transactions.size());
125 measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
127 public Void call() throws Exception {
129 for (DOMDataReadWriteTransaction writeTx :transactions) {
130 // Writes /test/outer-list/i in writeTx
131 writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
138 measure("Txs:"+ txNum + " Writes:" + innerNum, new Callable<Void>() {
140 public Void call() throws Exception {
142 for (DOMDataReadWriteTransaction writeTx :transactions) {
143 // Writes /test/outer-list/i in writeTx
144 InstanceIdentifier path = InstanceIdentifier.builder(outerListPath(i))
145 .node(TestModel.INNER_LIST_QNAME).build();
146 writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
147 for (int j = 0; j < innerNum; j++) {
148 InstanceIdentifier innerPath = InstanceIdentifier.builder(path)
149 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
154 ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
163 measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
165 public Void call() throws Exception {
166 List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
167 new Callable<List<ListenableFuture<?>>>() {
169 public List<ListenableFuture<?>> call() throws Exception {
170 List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
171 for (DOMDataReadWriteTransaction tx :transactions) {
172 builder.add(tx.commit());
177 Futures.allAsList(allFutures).get();
182 final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
184 public DOMDataReadTransaction call() throws Exception {
185 return domBroker.newReadOnlyTransaction();
191 measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
193 public Void call() throws Exception {
194 for (int i = 0; i < txNum; i++) {
195 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
197 assertTrue("outerList/" + i, potential.get().isPresent());
203 measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
205 public Void call() throws Exception {
206 for (int i = 0; i < txNum; i++) {
207 for (int j = 0; j < innerNum; j++) {
208 InstanceIdentifier path = InstanceIdentifier
209 .builder(outerListPath(i))
211 .node(TestModel.INNER_LIST_QNAME)
212 .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
214 ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
215 assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
223 private void measureOneTransactionTopContainer() throws Exception {
225 final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
227 public DOMDataReadWriteTransaction call() throws Exception {
228 return domBroker.newReadWriteTransaction();
232 measure("Txs:1 Write", new Callable<Void>() {
234 public Void call() throws Exception {
235 writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
236 writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
237 ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
242 measure("Txs:1 Reads:1", new Callable<Void>() {
244 public Void call() throws Exception {
245 // Reads /test in writeTx
246 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
247 TestModel.TEST_PATH);
248 assertTrue(writeTxContainer.get().isPresent());
253 measure("Txs:1 Reads:1", new Callable<Void>() {
255 public Void call() throws Exception {
256 // Reads /test in writeTx
257 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
258 TestModel.TEST_PATH);
259 assertTrue(writeTxContainer.get().isPresent());
264 measure("Txs:1 Submit, Finish", new Callable<Void>() {
266 public Void call() throws Exception {
267 measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
269 public ListenableFuture<?> call() throws Exception {
270 return writeTx.commit();