Simple test application for load testing distributed consensus

Change-Id: Ifd99bfc6fc608c5719e21ea9699db63f1c42f2a0
diff --git a/apps/test/loadtest/pom.xml b/apps/test/loadtest/pom.xml
new file mode 100644
index 0000000..e0a85a6
--- /dev/null
+++ b/apps/test/loadtest/pom.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016 Open Networking Laboratory
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-apps-test</artifactId>
+        <version>1.5.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-loadtest</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Distributed consensus load test application</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.loadtest</onos.app.name>
+        <onos.app.category>test</onos.app.category>
+        <onos.app.url>http://onosproject.org</onos.app.url>
+        <onos.app.readme>Distributed consensus load test application.</onos.app.readme>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+            <classifier>tests</classifier>
+        </dependency>
+
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+
+    </dependencies>
+
+</project>
diff --git a/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java b/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java
new file mode 100644
index 0000000..b142282
--- /dev/null
+++ b/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.loadtest;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.onlab.util.Tools.get;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.Dictionary;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.store.service.AsyncAtomicCounter;
+import org.onosproject.store.service.StorageService;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import com.google.common.util.concurrent.RateLimiter;
+
+/**
+ * Simple application for load testing distributed consensus.
+ * <p>
+ * This application simply increments as {@link AsyncAtomicCounter} at a configurable rate.
+ */
+@Component(immediate = true)
+@Service(value = DistributedConsensusLoadTest.class)
+public class DistributedConsensusLoadTest {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService configService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected StorageService storageService;
+
+    private ApplicationId appId;
+
+    private AtomicBoolean stopped = new AtomicBoolean(false);
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    private static final int DEFAULT_RATE = 100;
+
+    @Property(name = "rate", intValue = DEFAULT_RATE,
+            label = "Total number of increments per second to the atomic counter")
+    protected int rate = 0;
+
+    private AtomicLong lastValue = new AtomicLong(0);
+    private AtomicLong lastLoggedTime = new AtomicLong(0);
+    private AsyncAtomicCounter counter;
+    private ExecutorService testExecutor = Executors.newSingleThreadExecutor();
+
+    @Activate
+    public void activate(ComponentContext context) {
+        configService.registerProperties(getClass());
+        appId = coreService.registerApplication("org.onosproject.loadtest");
+        log.info("Started with {}", appId);
+        counter = storageService.atomicCounterBuilder()
+                                .withName("onos-app-loadtest-counter")
+                                .build();
+        modified(null);
+    }
+
+    private void startTest() {
+        stopped.set(false);
+        RateLimiter limiter = RateLimiter.create(rate);
+        Semaphore s = new Semaphore(100);
+        while (!stopped.get()) {
+            limiter.acquire();
+            s.acquireUninterruptibly();
+            counter.incrementAndGet().whenComplete((r, e) -> {
+                s.release();
+                long delta = System.currentTimeMillis() - lastLoggedTime.get();
+                if (e == null) {
+                    if (delta > 1000) {
+                        long tps = (long) ((r - lastValue.get()) * 1000.0) / delta;
+                        lastValue.set(r);
+                        lastLoggedTime.set(System.currentTimeMillis());
+                        log.info("Rate: {}", tps);
+                    }
+                }
+            });
+        }
+    }
+
+    private void stopTest() {
+        stopped.set(true);
+    }
+
+    @Deactivate
+    public void deactivate(ComponentContext context) {
+        configService.unregisterProperties(getClass(), false);
+        stopTest();
+        testExecutor.shutdown();
+        log.info("Stopped");
+    }
+
+    @Modified
+    public void modified(ComponentContext context) {
+        int newRate = DEFAULT_RATE;
+        if (context != null) {
+            Dictionary properties = context.getProperties();
+            try {
+                String s = get(properties, "rate");
+                newRate = isNullOrEmpty(s)
+                        ? rate : Integer.parseInt(s.trim());
+            } catch (Exception e) {
+                return;
+            }
+        }
+        if (newRate != rate) {
+            log.info("Rate changed to {}", newRate);
+            rate = newRate;
+            stopTest();
+            testExecutor.execute(this::startTest);
+        }
+    }
+}
diff --git a/apps/test/loadtest/src/main/java/org/onosproject/loadtest/package-info.java b/apps/test/loadtest/src/main/java/org/onosproject/loadtest/package-info.java
new file mode 100644
index 0000000..25e39bb
--- /dev/null
+++ b/apps/test/loadtest/src/main/java/org/onosproject/loadtest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Simple application for load testing distributed consensus.
+ */
+package org.onosproject.loadtest;
diff --git a/apps/test/pom.xml b/apps/test/pom.xml
index 7f0b8ff..2a9027a 100644
--- a/apps/test/pom.xml
+++ b/apps/test/pom.xml
@@ -33,6 +33,7 @@
 
     <modules>
         <module>election</module>
+        <module>loadtest</module>
         <module>intent-perf</module>
         <module>messaging-perf</module>
         <module>demo</module>