2 * Copyright (c) 2015 Cisco Systems 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
8 package org.opendaylight.dsbenchmark;
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.atomic.AtomicReference;
15 import org.opendaylight.dsbenchmark.listener.DsbenchmarkListenerProvider;
16 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaDelete;
17 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaRead;
18 import org.opendaylight.dsbenchmark.simpletx.SimpletxBaWrite;
19 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomDelete;
20 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomRead;
21 import org.opendaylight.dsbenchmark.simpletx.SimpletxDomWrite;
22 import org.opendaylight.dsbenchmark.txchain.TxchainBaDelete;
23 import org.opendaylight.dsbenchmark.txchain.TxchainBaRead;
24 import org.opendaylight.dsbenchmark.txchain.TxchainBaWrite;
25 import org.opendaylight.dsbenchmark.txchain.TxchainDomDelete;
26 import org.opendaylight.dsbenchmark.txchain.TxchainDomRead;
27 import org.opendaylight.dsbenchmark.txchain.TxchainDomWrite;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.WriteTransaction;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.CleanupStoreOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.DsbenchmarkService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.StartTestOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExec;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestExecBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatus.ExecStatus;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.TestStatusBuilder;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.opendaylight.yangtools.yang.common.Uint32;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
53 private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
54 private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
55 InstanceIdentifier.builder(TestExec.class).build();
56 private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
57 InstanceIdentifier.builder(TestStatus.class).build();
59 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
60 private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
61 private final DOMDataBroker domDataBroker; // Async DOM Broker for use with all DOM operations
62 private final DataBroker dataBroker; // Async Binding-Aware Broker for use in tx chains
64 private long testsCompleted = 0;
66 public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker dataBroker) {
67 this.domDataBroker = domDataBroker;
68 this.dataBroker = dataBroker;
71 @SuppressWarnings("checkstyle:illegalCatch")
73 listenerProvider.setDataBroker(dataBroker);
76 // We want to set the initial operation status so users can detect we are ready to start test.
77 setTestOperData(this.execStatus.get(), testsCompleted);
78 } catch (final Exception e) {
79 // TODO: Use a singleton service to make sure the initial write is performed only once.
80 LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData", e);
83 LOG.info("DsbenchmarkProvider initiated");
88 LOG.info("DsbenchmarkProvider closed");
92 public ListenableFuture<RpcResult<CleanupStoreOutput>> cleanupStore(final CleanupStoreInput input) {
94 LOG.debug("Data Store cleaned up");
95 return Futures.immediateFuture(RpcResultBuilder.success(new CleanupStoreOutputBuilder().build()).build());
99 @SuppressWarnings("checkstyle:illegalCatch")
100 public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
101 LOG.info("Starting the data store benchmark test, input: {}", input);
103 // Check if there is a test in progress
104 if (execStatus.compareAndSet(ExecStatus.Idle, ExecStatus.Executing) == false) {
105 LOG.info("Test in progress");
106 return RpcResultBuilder.success(new StartTestOutputBuilder()
107 .setStatus(StartTestOutput.Status.TESTINPROGRESS)
108 .build()).buildFuture();
111 // Cleanup data that may be left over from a previous test run
114 // Get the appropriate writer based on operation type and data format
115 DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
117 // Create listeners on OPERATIONAL and CONFIG test data subtrees
118 listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
121 long startTime = System.nanoTime();
122 dsWriter.createList();
123 long endTime = System.nanoTime();
124 final long listCreateTime = (endTime - startTime) / 1000;
126 // Run the test and measure the execution time
129 startTime = System.nanoTime();
130 dsWriter.executeList();
131 endTime = System.nanoTime();
132 execTime = (endTime - startTime) / 1000;
134 this.testsCompleted++;
136 } catch (final Exception e) {
137 LOG.error("Test error: {}", e.toString());
138 execStatus.set(ExecStatus.Idle);
139 return RpcResultBuilder.success(new StartTestOutputBuilder()
140 .setStatus(StartTestOutput.Status.FAILED)
141 .build()).buildFuture();
144 LOG.info("Test finished");
145 setTestOperData(ExecStatus.Idle, testsCompleted);
146 execStatus.set(ExecStatus.Idle);
148 // Get the number of data change events and cleanup the data change listeners
149 long numDataChanges = listenerProvider.getDataChangeCount();
150 long numEvents = listenerProvider.getEventCountAndDestroyListeners();
152 StartTestOutput output = new StartTestOutputBuilder()
153 .setStatus(StartTestOutput.Status.OK)
154 .setListBuildTime(listCreateTime)
155 .setExecTime(execTime)
156 .setTxOk(Uint32.valueOf(dsWriter.getTxOk()))
157 .setNtfOk(Uint32.valueOf(numEvents))
158 .setDataChangeEventsOk(Uint32.valueOf(numDataChanges))
159 .setTxError(Uint32.valueOf(dsWriter.getTxError()))
162 return RpcResultBuilder.success(output).buildFuture();
165 private void setTestOperData(final ExecStatus sts, final long tstCompl) {
166 TestStatus status = new TestStatusBuilder()
168 .setTestsCompleted(Uint32.valueOf(tstCompl))
171 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
172 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
176 } catch (final InterruptedException | ExecutionException e) {
177 throw new IllegalStateException(e);
180 LOG.debug("DataStore test oper status populated: {}", status);
183 private void cleanupTestStore() {
184 TestExec data = new TestExecBuilder().setOuterList(Collections.emptyMap()).build();
186 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
187 tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
190 LOG.debug("DataStore config test data cleaned up");
191 } catch (final InterruptedException | ExecutionException e) {
192 LOG.info("Failed to cleanup DataStore configtest data");
193 throw new IllegalStateException(e);
196 tx = dataBroker.newWriteOnlyTransaction();
197 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
200 LOG.debug("DataStore operational test data cleaned up");
201 } catch (final InterruptedException | ExecutionException e) {
202 LOG.info("Failed to cleanup DataStore operational test data");
203 throw new IllegalStateException(e);
208 private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
210 final DatastoreAbstractWriter retVal;
212 StartTestInput.TransactionType txType = input.getTransactionType();
213 StartTestInput.Operation oper = input.getOperation();
214 StartTestInput.DataFormat dataFormat = input.getDataFormat();
215 StartTestInput.DataStore dataStore = input.getDataStore();
216 int outerListElem = input.getOuterElements().intValue();
217 int innerListElem = input.getInnerElements().intValue();
218 int writesPerTx = input.getPutsPerTx().intValue();
221 if (txType == StartTestInput.TransactionType.SIMPLETX) {
222 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
223 if (StartTestInput.Operation.DELETE == oper) {
224 retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
225 innerListElem,writesPerTx, dataStore);
226 } else if (StartTestInput.Operation.READ == oper) {
227 retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
228 innerListElem, writesPerTx, dataStore);
230 retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
231 innerListElem, writesPerTx, dataStore);
234 if (StartTestInput.Operation.DELETE == oper) {
235 retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
236 innerListElem, writesPerTx, dataStore);
237 } else if (StartTestInput.Operation.READ == oper) {
238 retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
239 innerListElem, writesPerTx, dataStore);
241 retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
242 innerListElem, writesPerTx, dataStore);
246 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
247 if (StartTestInput.Operation.DELETE == oper) {
248 retVal = new TxchainBaDelete(this.dataBroker, outerListElem,
249 innerListElem, writesPerTx, dataStore);
250 } else if (StartTestInput.Operation.READ == oper) {
251 retVal = new TxchainBaRead(this.dataBroker, outerListElem,
252 innerListElem,writesPerTx, dataStore);
254 retVal = new TxchainBaWrite(this.dataBroker, oper, outerListElem,
255 innerListElem, writesPerTx, dataStore);
258 if (StartTestInput.Operation.DELETE == oper) {
259 retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
260 innerListElem, writesPerTx, dataStore);
261 } else if (StartTestInput.Operation.READ == oper) {
262 retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
263 innerListElem, writesPerTx, dataStore);
266 retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
267 innerListElem,writesPerTx, dataStore);
272 execStatus.set(ExecStatus.Idle);