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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.dsbenchmark.rev150105.test.exec.OuterList;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class DsbenchmarkProvider implements DsbenchmarkService, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(DsbenchmarkProvider.class);
55 private static final InstanceIdentifier<TestExec> TEST_EXEC_IID =
56 InstanceIdentifier.builder(TestExec.class).build();
57 private static final InstanceIdentifier<TestStatus> TEST_STATUS_IID =
58 InstanceIdentifier.builder(TestStatus.class).build();
60 private final AtomicReference<ExecStatus> execStatus = new AtomicReference<>(ExecStatus.Idle);
61 private final DsbenchmarkListenerProvider listenerProvider = new DsbenchmarkListenerProvider();
62 private final DOMDataBroker domDataBroker; // Async DOM Broker for use with all DOM operations
63 private final DataBroker dataBroker; // Async Binding-Aware Broker for use in tx chains
65 private long testsCompleted = 0;
67 public DsbenchmarkProvider(final DOMDataBroker domDataBroker, final DataBroker dataBroker) {
68 this.domDataBroker = domDataBroker;
69 this.dataBroker = dataBroker;
72 @SuppressWarnings("checkstyle:illegalCatch")
74 listenerProvider.setDataBroker(dataBroker);
77 // We want to set the initial operation status so users can detect we are ready to start test.
78 setTestOperData(this.execStatus.get(), testsCompleted);
79 } catch (final Exception e) {
80 // TODO: Use a singleton service to make sure the initial write is performed only once.
81 LOG.warn("Working around Bugs 8829 and 6793 by ignoring exception from setTestOperData", e);
84 LOG.info("DsbenchmarkProvider initiated");
89 LOG.info("DsbenchmarkProvider closed");
93 public ListenableFuture<RpcResult<CleanupStoreOutput>> cleanupStore(final CleanupStoreInput input) {
95 LOG.debug("Data Store cleaned up");
96 return Futures.immediateFuture(RpcResultBuilder.success(new CleanupStoreOutputBuilder().build()).build());
100 @SuppressWarnings("checkstyle:illegalCatch")
101 public ListenableFuture<RpcResult<StartTestOutput>> startTest(final StartTestInput input) {
102 LOG.info("Starting the data store benchmark test, input: {}", input);
104 // Check if there is a test in progress
105 if (execStatus.compareAndSet(ExecStatus.Idle, ExecStatus.Executing) == false) {
106 LOG.info("Test in progress");
107 return RpcResultBuilder.success(new StartTestOutputBuilder()
108 .setStatus(StartTestOutput.Status.TESTINPROGRESS)
109 .build()).buildFuture();
112 // Cleanup data that may be left over from a previous test run
115 // Get the appropriate writer based on operation type and data format
116 DatastoreAbstractWriter dsWriter = getDatastoreWriter(input);
118 // Create listeners on OPERATIONAL and CONFIG test data subtrees
119 listenerProvider.createAndRegisterListeners(input.getListeners().intValue());
122 long startTime = System.nanoTime();
123 dsWriter.createList();
124 long endTime = System.nanoTime();
125 final long listCreateTime = (endTime - startTime) / 1000;
127 // Run the test and measure the execution time
130 startTime = System.nanoTime();
131 dsWriter.executeList();
132 endTime = System.nanoTime();
133 execTime = (endTime - startTime) / 1000;
135 this.testsCompleted++;
137 } catch (final Exception e) {
138 LOG.error("Test error: {}", e.toString());
139 execStatus.set(ExecStatus.Idle);
140 return RpcResultBuilder.success(new StartTestOutputBuilder()
141 .setStatus(StartTestOutput.Status.FAILED)
142 .build()).buildFuture();
145 LOG.info("Test finished");
146 setTestOperData(ExecStatus.Idle, testsCompleted);
147 execStatus.set(ExecStatus.Idle);
149 // Get the number of data change events and cleanup the data change listeners
150 long numDataChanges = listenerProvider.getDataChangeCount();
151 long numEvents = listenerProvider.getEventCountAndDestroyListeners();
153 StartTestOutput output = new StartTestOutputBuilder()
154 .setStatus(StartTestOutput.Status.OK)
155 .setListBuildTime(listCreateTime)
156 .setExecTime(execTime)
157 .setTxOk(Uint32.valueOf(dsWriter.getTxOk()))
158 .setNtfOk(Uint32.valueOf(numEvents))
159 .setDataChangeEventsOk(Uint32.valueOf(numDataChanges))
160 .setTxError(Uint32.valueOf(dsWriter.getTxError()))
163 return RpcResultBuilder.success(output).buildFuture();
166 private void setTestOperData(final ExecStatus sts, final long tstCompl) {
167 TestStatus status = new TestStatusBuilder()
169 .setTestsCompleted(Uint32.valueOf(tstCompl))
172 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
173 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_STATUS_IID, status);
177 } catch (final InterruptedException | ExecutionException e) {
178 throw new IllegalStateException(e);
181 LOG.debug("DataStore test oper status populated: {}", status);
184 private void cleanupTestStore() {
185 TestExec data = new TestExecBuilder()
186 .setOuterList(Collections.<OuterList>emptyList())
189 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
190 tx.put(LogicalDatastoreType.CONFIGURATION, TEST_EXEC_IID, data);
193 LOG.debug("DataStore config test data cleaned up");
194 } catch (final InterruptedException | ExecutionException e) {
195 LOG.info("Failed to cleanup DataStore configtest data");
196 throw new IllegalStateException(e);
199 tx = dataBroker.newWriteOnlyTransaction();
200 tx.put(LogicalDatastoreType.OPERATIONAL, TEST_EXEC_IID, data);
203 LOG.debug("DataStore operational test data cleaned up");
204 } catch (final InterruptedException | ExecutionException e) {
205 LOG.info("Failed to cleanup DataStore operational test data");
206 throw new IllegalStateException(e);
211 private DatastoreAbstractWriter getDatastoreWriter(final StartTestInput input) {
213 final DatastoreAbstractWriter retVal;
215 StartTestInput.TransactionType txType = input.getTransactionType();
216 StartTestInput.Operation oper = input.getOperation();
217 StartTestInput.DataFormat dataFormat = input.getDataFormat();
218 StartTestInput.DataStore dataStore = input.getDataStore();
219 int outerListElem = input.getOuterElements().intValue();
220 int innerListElem = input.getInnerElements().intValue();
221 int writesPerTx = input.getPutsPerTx().intValue();
224 if (txType == StartTestInput.TransactionType.SIMPLETX) {
225 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
226 if (StartTestInput.Operation.DELETE == oper) {
227 retVal = new SimpletxBaDelete(this.dataBroker, outerListElem,
228 innerListElem,writesPerTx, dataStore);
229 } else if (StartTestInput.Operation.READ == oper) {
230 retVal = new SimpletxBaRead(this.dataBroker, outerListElem,
231 innerListElem, writesPerTx, dataStore);
233 retVal = new SimpletxBaWrite(this.dataBroker, oper, outerListElem,
234 innerListElem, writesPerTx, dataStore);
237 if (StartTestInput.Operation.DELETE == oper) {
238 retVal = new SimpletxDomDelete(this.domDataBroker, outerListElem,
239 innerListElem, writesPerTx, dataStore);
240 } else if (StartTestInput.Operation.READ == oper) {
241 retVal = new SimpletxDomRead(this.domDataBroker, outerListElem,
242 innerListElem, writesPerTx, dataStore);
244 retVal = new SimpletxDomWrite(this.domDataBroker, oper, outerListElem,
245 innerListElem, writesPerTx, dataStore);
249 if (dataFormat == StartTestInput.DataFormat.BINDINGAWARE) {
250 if (StartTestInput.Operation.DELETE == oper) {
251 retVal = new TxchainBaDelete(this.dataBroker, outerListElem,
252 innerListElem, writesPerTx, dataStore);
253 } else if (StartTestInput.Operation.READ == oper) {
254 retVal = new TxchainBaRead(this.dataBroker, outerListElem,
255 innerListElem,writesPerTx, dataStore);
257 retVal = new TxchainBaWrite(this.dataBroker, oper, outerListElem,
258 innerListElem, writesPerTx, dataStore);
261 if (StartTestInput.Operation.DELETE == oper) {
262 retVal = new TxchainDomDelete(this.domDataBroker, outerListElem,
263 innerListElem, writesPerTx, dataStore);
264 } else if (StartTestInput.Operation.READ == oper) {
265 retVal = new TxchainDomRead(this.domDataBroker, outerListElem,
266 innerListElem, writesPerTx, dataStore);
269 retVal = new TxchainDomWrite(this.domDataBroker, oper, outerListElem,
270 innerListElem,writesPerTx, dataStore);
275 execStatus.set(ExecStatus.Idle);