Simplify code using Java 8 features
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMBrokerPerformanceTest.java
index 181396fc884699e07eef9e0778e1ce3655d5e721..72a3c2e962d3f9e3837b6c577f1c85f833271344 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.controller.md.sal.dom.broker.impl;
 
 import static org.junit.Assert.assertEquals;
@@ -5,11 +13,16 @@ import static org.junit.Assert.assertTrue;
 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
 
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Executors;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -25,253 +38,183 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
-
 public class DOMBrokerPerformanceTest {
 
-    private static final Logger log = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DOMBrokerPerformanceTest.class);
 
-    private static NormalizedNode<?, ?> outerList(final int i) {
-        return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i);
+    private static NormalizedNode<?, ?> outerList(final int index) {
+        return ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index);
     }
 
-    private static YangInstanceIdentifier outerListPath(final int i) {
+    private static YangInstanceIdentifier outerListPath(final int index) {
         return YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)//
-                .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, i) //
+                .nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, index) //
                 .build();
     }
 
     private SchemaContext schemaContext;
-    private DOMDataBrokerImpl domBroker;
+    private AbstractDOMDataBroker domBroker;
 
     private static <V> V measure(final String name, final Callable<V> callable) throws Exception {
         // TODO Auto-generated method stub
-        log.debug("Measurement:{} Start", name);
+        LOG.debug("Measurement:{} Start", name);
         long startNano = System.nanoTime();
         try {
             return callable.call();
         } finally {
             long endNano = System.nanoTime();
-            log.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
+            LOG.info("Measurement:\"{}\" Time:{} ms", name, (endNano - startNano) / 1000000.0d);
         }
     }
 
     @Before
     public void setupStore() {
-       InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
-       InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
+        InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
+        InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
         schemaContext = TestModel.createTestContext();
 
         operStore.onGlobalContextUpdated(schemaContext);
         configStore.onGlobalContextUpdated(schemaContext);
 
-        ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
+        ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore>builder() //
                 .put(CONFIGURATION, configStore) //
                 .put(OPERATIONAL, operStore) //
                 .build();
         ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
-        domBroker = new DOMDataBrokerImpl(stores, executor);
+        domBroker = new SerializedDOMDataBroker(stores, executor);
     }
 
     @Test
     public void testPerformance() throws Exception {
-        measure("Test Suite (all tests)", new Callable<Void>() {
-
-            @Override
-            public Void call() throws Exception {
-                smallTestSuite(10, 1000);
-                //smallTestSuite(10, 100);
-                smallTestSuite(100, 100);
-                //smallTestSuite(100, 100);
-                //smallTestSuite(1000, 10);
-                smallTestSuite(1000, 10);
-                //smallTestSuite(1000, 1000);
-                return null;
-            }
+        measure("Test Suite (all tests)", (Callable<Void>) () -> {
+            smallTestSuite(10, 1000);
+            //smallTestSuite(10, 100);
+            smallTestSuite(100, 100);
+            //smallTestSuite(100, 100);
+            //smallTestSuite(1000, 10);
+            smallTestSuite(1000, 10);
+            //smallTestSuite(1000, 1000);
+            return null;
         });
     }
 
     private void smallTestSuite(final int txNum, final int innerListWriteNum) throws Exception {
-        measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", new Callable<Void>() {
-
-            @Override
-            public Void call() throws Exception {
-                measureOneTransactionTopContainer();
-                measureSeparateWritesOneLevel(txNum, innerListWriteNum);
-                return null;
-            }
+        measure("TestSuite (Txs:" + txNum + " innerWrites:" + innerListWriteNum + ")", (Callable<Void>) () -> {
+            measureOneTransactionTopContainer();
+            measureSeparateWritesOneLevel(txNum, innerListWriteNum);
+            return null;
         });
     }
 
     private void measureSeparateWritesOneLevel(final int txNum, final int innerNum) throws Exception {
-        final List<DOMDataReadWriteTransaction> transactions = measure("Txs:"+ txNum + " Allocate",
-                new Callable<List<DOMDataReadWriteTransaction>>() {
-                    @Override
-                    public List<DOMDataReadWriteTransaction> call() throws Exception {
-                        List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
-                        for (int i = 0; i < txNum; i++) {
-                            DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
-                            builder.add(writeTx);
-                        }
-                        return builder;
-                    }
-                });
+        final List<DOMDataReadWriteTransaction> transactions = measure("Txs:" + txNum + " Allocate", () -> {
+            List<DOMDataReadWriteTransaction> builder = new ArrayList<>(txNum);
+            for (int i = 0; i < txNum; i++) {
+                DOMDataReadWriteTransaction writeTx = domBroker.newReadWriteTransaction();
+                builder.add(writeTx);
+            }
+            return builder;
+        });
         assertEquals(txNum, transactions.size());
-        measure("Txs:"+ txNum + " Writes:1", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                int i = 0;
-                for (DOMDataReadWriteTransaction writeTx :transactions) {
-                    // Writes /test/outer-list/i in writeTx
-                    writeTx.put(OPERATIONAL, outerListPath(i), outerList(i));
-                    i++;
-                }
-                return null;
+        measure("Txs:" + txNum + " Writes:1", (Callable<Void>) () -> {
+            int index = 0;
+            for (DOMDataReadWriteTransaction writeTx : transactions) {
+                // Writes /test/outer-list/i in writeTx
+                writeTx.put(OPERATIONAL, outerListPath(index), outerList(index));
+                index++;
             }
+            return null;
         });
 
-        measure("Txs:"+ txNum +  " Writes:" + innerNum, new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                int i = 0;
-                for (DOMDataReadWriteTransaction writeTx :transactions) {
-                    // Writes /test/outer-list/i in writeTx
-                    YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
-                            .node(TestModel.INNER_LIST_QNAME).build();
-                    writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
-                    for (int j = 0; j < innerNum; j++) {
-                        YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
-                                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
-                                .build();
-                        writeTx.put(
-                                OPERATIONAL,
-                                innerPath,
-                                ImmutableNodes.mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME,
-                                        String.valueOf(j)));
-                    }
-                    i++;
+        measure("Txs:" + txNum + " Writes:" + innerNum, (Callable<Void>) () -> {
+            int index = 0;
+            for (DOMDataReadWriteTransaction writeTx : transactions) {
+                // Writes /test/outer-list/i in writeTx
+                YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(index))
+                        .node(TestModel.INNER_LIST_QNAME).build();
+                writeTx.put(OPERATIONAL, path, ImmutableNodes.mapNodeBuilder(TestModel.INNER_LIST_QNAME).build());
+                for (int j = 0; j < innerNum; j++) {
+                    YangInstanceIdentifier innerPath = YangInstanceIdentifier.builder(path)
+                            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)).build();
+                    writeTx.put(OPERATIONAL, innerPath, ImmutableNodes
+                            .mapEntry(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)));
                 }
-                return null;
+                index++;
             }
+            return null;
         });
 
-        measure("Txs:" + txNum + " Submit, Finish", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                List<ListenableFuture<?>> allFutures = measure(txNum + " Submits",
-                        new Callable<List<ListenableFuture<?>>>() {
-                            @Override
-                            public List<ListenableFuture<?>> call() throws Exception {
-                                List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
-                                for (DOMDataReadWriteTransaction tx :transactions) {
-                                    builder.add(tx.submit());
-                                }
-                                return builder;
-                            }
-                        });
-                Futures.allAsList(allFutures).get();
-                return null;
-            }
+        measure("Txs:" + txNum + " Submit, Finish", (Callable<Void>) () -> {
+            List<ListenableFuture<?>> allFutures = measure(txNum + " Submits", () -> {
+                List<ListenableFuture<?>> builder = new ArrayList<>(txNum);
+                for (DOMDataReadWriteTransaction tx : transactions) {
+                    builder.add(tx.submit());
+                }
+                return builder;
+            });
+            Futures.allAsList(allFutures).get();
+            return null;
         });
 
-        final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate", new Callable<DOMDataReadTransaction>() {
-            @Override
-            public DOMDataReadTransaction call() throws Exception {
-                return domBroker.newReadOnlyTransaction();
-
-            }
-        });
+        final DOMDataReadTransaction readTx = measure("Txs:1 (ro), Allocate",
+                                                      (Callable<DOMDataReadTransaction>) () -> domBroker
+                                                              .newReadOnlyTransaction());
 
 
-        measure("Txs:1 (ro) Reads:" + txNum + " (1-level)" , new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                for (int i = 0; i < txNum; i++) {
-                    ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL,
-                            outerListPath(i));
-                    assertTrue("outerList/" + i, potential.get().isPresent());
-                }
-                return null;
+        measure("Txs:1 (ro) Reads:" + txNum + " (1-level)", (Callable<Void>) () -> {
+            for (int i = 0; i < txNum; i++) {
+                ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, outerListPath(i));
+                assertTrue("outerList/" + i, potential.get().isPresent());
             }
+            return null;
         });
 
-        measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                for (int i = 0; i < txNum; i++) {
-                    for (int j = 0; j < innerNum; j++) {
-                        YangInstanceIdentifier path = YangInstanceIdentifier
-                                .builder(outerListPath(i))
-                                //
-                                .node(TestModel.INNER_LIST_QNAME)
-                                .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j))
-                                .build();
-                        ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
-                        assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
-                    }
+        measure("Txs:1 (ro) Reads:" + txNum * innerNum + " (2-level)", (Callable<Void>) () -> {
+            for (int i = 0; i < txNum; i++) {
+                for (int j = 0; j < innerNum; j++) {
+                    YangInstanceIdentifier path = YangInstanceIdentifier.builder(outerListPath(i))
+                            //
+                            .node(TestModel.INNER_LIST_QNAME)
+                            .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, String.valueOf(j)).build();
+                    ListenableFuture<Optional<NormalizedNode<?, ?>>> potential = readTx.read(OPERATIONAL, path);
+                    assertTrue("outer-list/" + i + "/inner-list/" + j, potential.get().isPresent());
                 }
-                return null;
             }
+            return null;
         });
     }
 
     private void measureOneTransactionTopContainer() throws Exception {
 
-        final DOMDataReadWriteTransaction writeTx = measure("Txs:1 Allocate", new Callable<DOMDataReadWriteTransaction>() {
-            @Override
-            public DOMDataReadWriteTransaction call() throws Exception {
-                return domBroker.newReadWriteTransaction();
-            }
-        });
+        final DOMDataReadWriteTransaction writeTx =
+                measure("Txs:1 Allocate", () -> domBroker.newReadWriteTransaction());
 
-        measure("Txs:1 Write", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
-                writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
+        measure("Txs:1 Write", (Callable<Void>) () -> {
+            writeTx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+            writeTx.put(OPERATIONAL, TestModel.OUTER_LIST_PATH,
                         ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
-                return null;
-            }
+            return null;
         });
 
-        measure("Txs:1 Reads:1", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                // Reads /test in writeTx
-                ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
-                        TestModel.TEST_PATH);
-                assertTrue(writeTxContainer.get().isPresent());
-                return null;
-            }
+        measure("Txs:1 Reads:1", (Callable<Void>) () -> {
+            // Reads /test in writeTx
+            ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx
+                    .read(OPERATIONAL, TestModel.TEST_PATH);
+            assertTrue(writeTxContainer.get().isPresent());
+            return null;
         });
 
-        measure("Txs:1 Reads:1", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                // Reads /test in writeTx
-                ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(OPERATIONAL,
-                        TestModel.TEST_PATH);
-                assertTrue(writeTxContainer.get().isPresent());
-                return null;
-            }
+        measure("Txs:1 Reads:1", (Callable<Void>) () -> {
+            // Reads /test in writeTx
+            ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx
+                    .read(OPERATIONAL, TestModel.TEST_PATH);
+            assertTrue(writeTxContainer.get().isPresent());
+            return null;
         });
 
-        measure("Txs:1 Submit, Finish", new Callable<Void>() {
-            @Override
-            public Void call() throws Exception {
-                measure("Txs:1 Submit", new Callable<ListenableFuture<?>>() {
-                    @Override
-                    public ListenableFuture<?> call() throws Exception {
-                        return writeTx.submit();
-                    }
-                }).get();
-                return null;
-            }
+        measure("Txs:1 Submit, Finish", (Callable<Void>) () -> {
+            measure("Txs:1 Submit", (Callable<ListenableFuture<?>>) writeTx::submit).get();
+            return null;
         });
     }
 }