Bug-4234 - Add count field to cars stress-test RPC. 19/26419/2
authorShaleen Saxena <ssaxena@brocade.com>
Wed, 2 Sep 2015 17:19:51 +0000 (13:19 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Thu, 10 Sep 2015 14:41:02 +0000 (10:41 -0400)
Added a 'count' field to cars stress-test RPC. The test will stop
after the 'count' cars have been created. If count is zero, then
stress-test will continue till stop-stress-test rpc is given.

Also added some null checks for missing input fields. If rate is
zero, then an error is returned.

Change-Id: Id313f9094e8ca887993e4e8911d0a86b64db7303
Signed-off-by: Shaleen Saxena <ssaxena@brocade.com>
(cherry picked from commit 3abc8b4d3379109bd12aadbc01c0b134cbd268c5)

opendaylight/md-sal/samples/clustering-test-app/model/src/main/yang/car.yang
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/CarProvider.java

index f421133c620c20bbeebe1358ddeacd3ba955c819..eb02be7f0f1220c725346210d285aafa192a9ef8 100644 (file)
@@ -65,6 +65,13 @@ module car {
             leaf rate {
               type uint16;
             }
+
+            leaf count {
+              type uint16;
+              default 0;
+              description "Number of cars to create. Zero implies unlimited cars; use
+                           stop-stress-test rpc to stop the test.";
+            }
         }
     }
     
index 2a068af313e3d813644218080e0323ca0d6a1eeb..4b5819f81e25a5d5ae66e83b2862e6a28cd463e1 100644 (file)
@@ -9,10 +9,12 @@ package org.opendaylight.controller.clustering.it.provider;
 
 import com.google.common.base.Stopwatch;
 import com.google.common.util.concurrent.Futures;
+
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicLong;
+
 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;
@@ -25,6 +27,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controll
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.sal.clustering.it.car.rev140818.cars.CarEntryBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.slf4j.Logger;
@@ -58,7 +61,25 @@ public class CarProvider implements CarService {
 
     @Override
     public Future<RpcResult<Void>> stressTest(StressTestInput input) {
-        log.info("stressTest starting : rate: {}", input.getRate());
+        final int inputRate, inputCount;
+
+        // If rate is not provided, or given as zero, then just return.
+        if ((input.getRate() == null) || (input.getRate() == 0)) {
+            log.info("Exiting stress test as no rate is given.");
+            return Futures.immediateFuture(RpcResultBuilder.<Void>failed()
+                                           .withError(ErrorType.PROTOCOL, "invalid rate")
+                                           .build());
+        } else {
+            inputRate = input.getRate();
+        }
+
+        if (input.getCount() != null) {
+            inputCount = input.getCount();
+        } else {
+            inputCount = 0;
+        }
+
+        log.info("Stress test starting : rate: {} count: {}", inputRate, inputCount);
 
         stopThread();
 
@@ -73,7 +94,7 @@ public class CarProvider implements CarService {
         }
 
         stopThread = false;
-        final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / input.getRate();
+        final long sleep = TimeUnit.NANOSECONDS.convert(1000,TimeUnit.MILLISECONDS) / inputRate;
         final Stopwatch sw = Stopwatch.createUnstarted();
         testThread = new Thread() {
             @Override
@@ -97,9 +118,14 @@ public class CarProvider implements CarService {
                     if((count.get() % 1000) == 0) {
                         log.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.info("Stress test thread stopping");
+                log.info("Stress test thread stopping after creating {} cars.", count.get());
             }
         };
         testThread.start();