Added the data store benchmark (dsbenchmark, Bug 4519, https://bugs.opendaylight...
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / simpletx / SimpletxBaWrite.java
diff --git a/benchmark/dsbenchmark/src/main/java/org/opendaylight/dsbenchmark/simpletx/SimpletxBaWrite.java b/benchmark/dsbenchmark/src/main/java/org/opendaylight/dsbenchmark/simpletx/SimpletxBaWrite.java
new file mode 100644 (file)
index 0000000..f811c4b
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2015 Cisco Systems 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.dsbenchmark.simpletx;
+
+import java.util.List;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.dsbenchmark.BaListBuilder;
+import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SimpletxBaWrite extends DatastoreAbstractWriter {
+    private static final Logger LOG = LoggerFactory.getLogger(SimpletxBaWrite.class);
+    private final DataBroker dataBroker;
+    private List<OuterList> list;
+
+    public SimpletxBaWrite(DataBroker dataBroker, StartTestInput.Operation oper,
+            int outerListElem, int innerListElem, long writesPerTx) {
+        super(oper, outerListElem, innerListElem, writesPerTx);
+        this.dataBroker = dataBroker;
+        LOG.info("Created SimpletxBaWrite");
+    }
+
+     @Override
+     public void createList() {
+         list = BaListBuilder.buildOuterList(this.outerListElem, this.innerListElem);
+     }
+
+    @Override
+    public void executeList() {
+        WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
+        long writeCnt = 0;
+
+        for (OuterList element : this.list) {
+            InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class)
+                                                    .child(OuterList.class, element.getKey());
+            if (oper == StartTestInput.Operation.PUT) {
+                tx.put(LogicalDatastoreType.CONFIGURATION, iid, element);
+            } else {
+                tx.merge(LogicalDatastoreType.CONFIGURATION, iid, element);
+            }
+
+            writeCnt++;
+
+            if (writeCnt == writesPerTx) {
+                try {
+                    tx.submit().checkedGet();
+                    txOk++;
+                } catch (TransactionCommitFailedException e) {
+                    LOG.error("Transaction failed: {}", e.toString());
+                    txError++;
+                }
+                tx = dataBroker.newWriteOnlyTransaction();
+                writeCnt = 0;
+            }
+        }
+
+        if (writeCnt != 0) {
+            try {
+                tx.submit().checkedGet();
+            } catch (TransactionCommitFailedException e) {
+                LOG.error("Transaction failed: {}", e.toString());
+            }
+        }
+    }
+
+}