Improve segmented journal actor metrics
[controller.git] / benchmark / dsbenchmark / src / main / java / org / opendaylight / dsbenchmark / simpletx / SimpletxBaWrite.java
1 /*
2  * Copyright (c) 2015 Cisco Systems and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.dsbenchmark.simpletx;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.dsbenchmark.BaListBuilder;
15 import org.opendaylight.dsbenchmark.DatastoreAbstractWriter;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput.DataStore;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class SimpletxBaWrite extends DatastoreAbstractWriter {
26     private static final Logger LOG = LoggerFactory.getLogger(SimpletxBaWrite.class);
27
28     private final DataBroker dataBroker;
29     private List<OuterList> list = null;
30
31     public SimpletxBaWrite(final DataBroker dataBroker, final StartTestInput.Operation oper,
32             final int outerListElem, final int innerListElem, final long writesPerTx, final DataStore dataStore) {
33         super(oper, outerListElem, innerListElem, writesPerTx, dataStore);
34         this.dataBroker = requireNonNull(dataBroker);
35         LOG.debug("Created SimpletxBaWrite");
36     }
37
38     @Override
39     public void createList() {
40         list = BaListBuilder.buildOuterList(outerListElem, innerListElem);
41     }
42
43     @Override
44     public void executeList() {
45         final var dsType = getDataStoreType();
46
47         var tx = dataBroker.newWriteOnlyTransaction();
48         long writeCnt = 0;
49
50         for (var element : list) {
51             final var iid = InstanceIdentifier.create(TestExec.class).child(OuterList.class, element.key());
52             if (oper == StartTestInput.Operation.PUT) {
53                 tx.put(dsType, iid, element);
54             } else {
55                 tx.merge(dsType, iid, element);
56             }
57
58             writeCnt++;
59
60             if (writeCnt == writesPerTx) {
61                 try {
62                     tx.commit().get();
63                     txOk++;
64                 } catch (final InterruptedException | ExecutionException e) {
65                     LOG.error("Transaction failed", e);
66                     txError++;
67                 }
68                 tx = dataBroker.newWriteOnlyTransaction();
69
70                 writeCnt = 0;
71             }
72         }
73
74         if (writeCnt != 0) {
75             try {
76                 tx.commit().get();
77             } catch (final InterruptedException | ExecutionException e) {
78                 LOG.error("Transaction failed", e);
79             }
80         }
81     }
82 }