Search in sources :

Example 1 with StressTestOutputBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder in project controller by opendaylight.

the class CarProvider method stressTest.

@Override
public ListenableFuture<RpcResult<StressTestOutput>> stressTest(final StressTestInput input) {
    final int inputRate;
    final long inputCount;
    // If rate is not provided, or given as zero, then just return.
    if (input.getRate() == null || input.getRate().toJava() == 0) {
        LOG_PURCHASE_CAR.info("Exiting stress test as no rate is given.");
        return Futures.immediateFuture(RpcResultBuilder.<StressTestOutput>failed().withError(ErrorType.PROTOCOL, "invalid rate").build());
    }
    inputRate = input.getRate().toJava();
    if (input.getCount() != null) {
        inputCount = input.getCount().toJava();
    } else {
        inputCount = 0;
    }
    LOG_PURCHASE_CAR.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
    stopThread();
    // clear counters
    succcessCounter.set(0);
    failureCounter.set(0);
    WriteTransaction tx = dataProvider.newWriteOnlyTransaction();
    InstanceIdentifier<Cars> carsId = InstanceIdentifier.create(Cars.class);
    tx.merge(LogicalDatastoreType.CONFIGURATION, carsId, new CarsBuilder().build());
    try {
        tx.commit().get(5, TimeUnit.SECONDS);
    } catch (TimeoutException | InterruptedException | ExecutionException e) {
        LOG_PURCHASE_CAR.error("Put Cars failed", e);
        return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
    }
    stopThread = false;
    final long sleep = TimeUnit.NANOSECONDS.convert(1000, TimeUnit.MILLISECONDS) / inputRate;
    final Stopwatch sw = Stopwatch.createUnstarted();
    testThread = new Thread(() -> {
        sw.start();
        AtomicLong count = new AtomicLong();
        while (!stopThread) {
            long id = count.incrementAndGet();
            WriteTransaction tx1 = dataProvider.newWriteOnlyTransaction();
            CarEntry car = new CarEntryBuilder().setId(new CarId("car" + id)).build();
            tx1.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.<Cars>builder(Cars.class).child(CarEntry.class, car.key()).build(), car);
            tx1.commit().addCallback(new FutureCallback<CommitInfo>() {

                @Override
                public void onSuccess(final CommitInfo result) {
                    // Transaction succeeded
                    succcessCounter.getAndIncrement();
                }

                @Override
                public void onFailure(final Throwable ex) {
                    // Transaction failed
                    failureCounter.getAndIncrement();
                    LOG_CAR_PROVIDER.error("Put Cars failed", ex);
                }
            }, MoreExecutors.directExecutor());
            try {
                TimeUnit.NANOSECONDS.sleep(sleep);
            } catch (InterruptedException e) {
                break;
            }
            if (count.get() % 1000 == 0) {
                LOG_PURCHASE_CAR.info("Cars created {}, time: {}", count.get(), sw.elapsed(TimeUnit.SECONDS));
            }
            // Check if a count is specified in input and we have created that many cars.
            if (inputCount != 0 && count.get() >= inputCount) {
                stopThread = true;
            }
        }
        LOG_PURCHASE_CAR.info("Stress test thread stopping after creating {} cars.", count.get());
    });
    testThread.start();
    return Futures.immediateFuture(RpcResultBuilder.success(new StressTestOutputBuilder().build()).build());
}
Also used : WriteTransaction(org.opendaylight.mdsal.binding.api.WriteTransaction) Cars(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars) Stopwatch(com.google.common.base.Stopwatch) StopStressTestOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder) StressTestOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder) CarEntry(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) CarId(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId) CarsBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo) ExecutionException(java.util.concurrent.ExecutionException) CarEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 WriteTransaction (org.opendaylight.mdsal.binding.api.WriteTransaction)1 CommitInfo (org.opendaylight.mdsal.common.api.CommitInfo)1 CarId (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarId)1 Cars (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.Cars)1 CarsBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.CarsBuilder)1 StopStressTestOutputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StopStressTestOutputBuilder)1 StressTestOutputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.StressTestOutputBuilder)1 CarEntry (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry)1 CarEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder)1