[ONOS-6350] Transaction performance tests
Change-Id: Ia335c707279466b4104a96a1f7ddbd014cb9f0ef
diff --git a/apps/test/pom.xml b/apps/test/pom.xml
index b2a0e00..01bf97c 100644
--- a/apps/test/pom.xml
+++ b/apps/test/pom.xml
@@ -36,6 +36,7 @@
<module>intent-perf</module>
<module>messaging-perf</module>
<module>flow-perf</module>
+ <module>transaction-perf</module>
<module>demo</module>
<module>distributed-primitives</module>
<module>netcfg-monitor</module>
diff --git a/apps/test/transaction-perf/BUCK b/apps/test/transaction-perf/BUCK
new file mode 100644
index 0000000..7b60d19
--- /dev/null
+++ b/apps/test/transaction-perf/BUCK
@@ -0,0 +1,19 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:org.apache.karaf.shell.console',
+ '//cli:onos-cli',
+ '//utils/rest:onlab-rest',
+ '//lib:javax.ws.rs-api',
+ '//core/store/serializers:onos-core-serializers',
+]
+
+osgi_jar_with_tests (
+ deps = COMPILE_DEPS,
+)
+
+onos_app (
+ title = 'Transaction Performance Test App',
+ category = 'Test',
+ url = 'http://onosproject.org',
+ description = 'Transaction performance test application.',
+)
diff --git a/apps/test/transaction-perf/pom.xml b/apps/test/transaction-perf/pom.xml
new file mode 100644
index 0000000..c7bc4c7
--- /dev/null
+++ b/apps/test/transaction-perf/pom.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present 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.10.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>onos-app-transaction-perf</artifactId>
+ <packaging>bundle</packaging>
+
+ <description>Transaction performance test application</description>
+
+ <properties>
+ <onos.app.name>org.onosproject.transactionperf</onos.app.name>
+ <onos.app.title>Transaction Performance Test App</onos.app.title>
+ <onos.app.category>Test</onos.app.category>
+ <onos.app.url>http://onosproject.org</onos.app.url>
+ <onos.app.readme>Transaction performance test application.</onos.app.readme>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-core-serializers</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <!-- Required for javadoc generation -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/apps/test/transaction-perf/src/main/java/org/onosproject/transactionperf/TransactionPerfApp.java b/apps/test/transaction-perf/src/main/java/org/onosproject/transactionperf/TransactionPerfApp.java
new file mode 100644
index 0000000..50f23cb
--- /dev/null
+++ b/apps/test/transaction-perf/src/main/java/org/onosproject/transactionperf/TransactionPerfApp.java
@@ -0,0 +1,327 @@
+/*
+ * Copyright 2017-present 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.transactionperf;
+
+import java.util.Comparator;
+import java.util.Dictionary;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+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.onlab.util.Tools;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.CommitStatus;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.TransactionContext;
+import org.onosproject.store.service.TransactionalMap;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.apache.felix.scr.annotations.ReferenceCardinality.MANDATORY_UNARY;
+import static org.onlab.util.Tools.get;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Application for measuring transaction performance.
+ */
+@Component(immediate = true, enabled = true)
+@Service(value = TransactionPerfApp.class)
+public class TransactionPerfApp {
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ @Reference(cardinality = MANDATORY_UNARY)
+ protected ClusterService clusterService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ComponentConfigService configService;
+
+ private static final String DEFAULT_MAP_NAME = "transaction-perf";
+ private static final double DEFAULT_READ_PERCENTAGE = .9;
+ private static final int DEFAULT_TOTAL_OPERATIONS = 1000;
+ private static final boolean DEFAULT_WITH_CONTENTION = false;
+ private static final boolean DEFAULT_WITH_RETRIES = false;
+ private static final int DEFAULT_REPORT_INTERVAL_SECONDS = 1;
+ private static final String KEY_PREFIX = "key";
+
+ @Property(name = "mapName", value = DEFAULT_MAP_NAME,
+ label = "The name of the map to use for testing")
+ protected String mapName = DEFAULT_MAP_NAME;
+
+ @Property(name = "readPercentage", doubleValue = DEFAULT_READ_PERCENTAGE,
+ label = "Percentage of reads to perform")
+ protected double readPercentage = DEFAULT_READ_PERCENTAGE;
+
+ @Property(name = "totalOperationsPerTransaction", intValue = DEFAULT_TOTAL_OPERATIONS,
+ label = "Number of operations to perform within each transaction")
+ protected int totalOperationsPerTransaction = DEFAULT_TOTAL_OPERATIONS;
+
+ @Property(name = "withContention", boolValue = DEFAULT_WITH_CONTENTION,
+ label = "Whether to test transactions with contention from all nodes")
+ protected boolean withContention = DEFAULT_WITH_CONTENTION;
+
+ @Property(name = "withRetries", boolValue = DEFAULT_WITH_RETRIES,
+ label = "Whether to retry transactions until success")
+ protected boolean withRetries = DEFAULT_WITH_RETRIES;
+
+ @Property(name = "reportIntervalSeconds", intValue = DEFAULT_REPORT_INTERVAL_SECONDS,
+ label = "The frequency with which to report performance in seconds")
+ protected int reportIntervalSeconds = 1;
+
+ private ExecutorService testRunner =
+ Executors.newSingleThreadExecutor(Tools.groupedThreads("app/transaction-perf-test-runner", ""));
+
+ private ScheduledExecutorService reporter =
+ Executors.newSingleThreadScheduledExecutor(
+ groupedThreads("onos/transaction-perf-test", "reporter"));
+
+ private Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
+
+ private AtomicInteger attempted = new AtomicInteger(0);
+ private AtomicInteger succeeded = new AtomicInteger(0);
+ private AtomicInteger iteration = new AtomicInteger(0);
+
+ @Activate
+ public void activate(ComponentContext context) {
+ configService.registerProperties(getClass());
+ if (isParticipant()) {
+ startTest();
+ reporter.scheduleWithFixedDelay(this::reportPerformance,
+ reportIntervalSeconds,
+ reportIntervalSeconds,
+ TimeUnit.SECONDS);
+ logConfig("Started");
+ }
+ }
+
+ @Modified
+ public void modified(ComponentContext context) {
+ if (context == null) {
+ mapName = DEFAULT_MAP_NAME;
+ readPercentage = DEFAULT_READ_PERCENTAGE;
+ totalOperationsPerTransaction = DEFAULT_TOTAL_OPERATIONS;
+ withContention = DEFAULT_WITH_CONTENTION;
+ withRetries = DEFAULT_WITH_RETRIES;
+ reportIntervalSeconds = DEFAULT_REPORT_INTERVAL_SECONDS;
+ return;
+ }
+
+ Dictionary properties = context.getProperties();
+
+ String newMapName = mapName;
+ double newReadPercentage = readPercentage;
+ int newTotalOperationsPerTransaction = totalOperationsPerTransaction;
+ boolean newWithContention = withContention;
+ boolean newWithRetries = withRetries;
+ int newReportIntervalSeconds = reportIntervalSeconds;
+
+ try {
+ String s;
+
+ s = get(properties, "mapName");
+ if (!isNullOrEmpty(s)) {
+ newMapName = s;
+ }
+
+ s = get(properties, "readPercentage");
+ if (!isNullOrEmpty(s)) {
+ newReadPercentage = Double.parseDouble(s);
+ }
+
+ s = get(properties, "totalOperationsPerTransaction");
+ if (!isNullOrEmpty(s)) {
+ newTotalOperationsPerTransaction = Integer.parseInt(s);
+ }
+
+ s = get(properties, "withContention");
+ if (!isNullOrEmpty(s)) {
+ newWithContention = Boolean.parseBoolean(s);
+ }
+
+ s = get(properties, "withRetries");
+ if (!isNullOrEmpty(s)) {
+ newWithRetries = Boolean.parseBoolean(s);
+ }
+
+ s = get(properties, "reportIntervalSeconds");
+ if (!isNullOrEmpty(s)) {
+ newReportIntervalSeconds = Integer.parseInt(s);
+ }
+ } catch (NumberFormatException | ClassCastException e) {
+ return;
+ }
+
+ boolean modified = newMapName != mapName
+ || newReadPercentage != readPercentage
+ || newTotalOperationsPerTransaction != totalOperationsPerTransaction
+ || newWithContention != withContention
+ || newWithRetries != withRetries
+ || newReportIntervalSeconds != reportIntervalSeconds;
+
+ // If no configuration options have changed, skip restarting the test.
+ if (!modified) {
+ return;
+ }
+
+ mapName = newMapName;
+ readPercentage = newReadPercentage;
+ totalOperationsPerTransaction = newTotalOperationsPerTransaction;
+ withContention = newWithContention;
+ withRetries = newWithRetries;
+ reportIntervalSeconds = newReportIntervalSeconds;
+
+ // Restart the test.
+ stopTest();
+ testRunner = Executors.newSingleThreadExecutor(
+ groupedThreads("app/transaction-perf-test-runner", ""));
+ reporter = Executors.newSingleThreadScheduledExecutor(
+ groupedThreads("onos/transaction-perf-test", "reporter"));
+ startTest();
+ reporter.scheduleWithFixedDelay(this::reportPerformance,
+ reportIntervalSeconds,
+ reportIntervalSeconds,
+ TimeUnit.SECONDS);
+ logConfig("Restarted");
+ }
+
+ @Deactivate
+ public void deactivate(ComponentContext context) {
+ configService.unregisterProperties(getClass(), false);
+ stopTest();
+ log.info("Stopped");
+ }
+
+ private void logConfig(String prefix) {
+ log.info("{} with mapName = {}; readPercentage = {}; totalOperationsPerTransaction = {}; " +
+ "withContention = {}; withRetries = {}; reportIntervalSeconds = {}",
+ prefix, mapName, readPercentage, totalOperationsPerTransaction,
+ withContention, withRetries, reportIntervalSeconds);
+ }
+
+ /**
+ * Returns whether this node is a participant in the test.
+ *
+ * @return whether this node is a participant in the test
+ */
+ private boolean isParticipant() {
+ return withContention || clusterService.getLocalNode().id().equals(clusterService.getNodes().stream()
+ .map(ControllerNode::id)
+ .min(Comparator.naturalOrder()).get());
+ }
+
+ /**
+ * Initializes the map.
+ */
+ private void initializeMap() {
+ TransactionContext context = storageService.transactionContextBuilder().build();
+ context.begin();
+ try {
+ TransactionalMap<String, String> map = context.getTransactionalMap(mapName, serializer);
+ for (int i = 0; i < totalOperationsPerTransaction; i++) {
+ map.put(KEY_PREFIX + i, KEY_PREFIX + i);
+ }
+ context.commit().join();
+ } catch (Exception e) {
+ context.abort();
+ log.warn("An exception occurred during initialization: {}", e);
+ }
+ }
+
+ /**
+ * Starts the test.
+ */
+ private void startTest() {
+ logConfig("Started");
+ initializeMap();
+ runTest(iteration.getAndIncrement());
+ }
+
+ /**
+ * Runs the test.
+ */
+ private void runTest(int iteration) {
+ testRunner.execute(() -> {
+ // Attempt the transaction until successful if retries are enabled.
+ CommitStatus status = null;
+ do {
+ TransactionContext context = storageService.transactionContextBuilder().build();
+ context.begin();
+
+ try {
+ TransactionalMap<String, String> map = context.getTransactionalMap(mapName, serializer);
+ int reads = (int) (totalOperationsPerTransaction * readPercentage);
+ for (int i = 0; i < reads; i++) {
+ map.get(KEY_PREFIX + i);
+ }
+
+ int writes = (int) (totalOperationsPerTransaction * (1 - readPercentage));
+ for (int i = 0; i < writes; i++) {
+ map.put(KEY_PREFIX + i, KEY_PREFIX + iteration + i);
+ }
+
+ status = context.commit().join();
+ attempted.incrementAndGet();
+ } catch (Exception e) {
+ context.abort();
+ log.warn("An exception occurred during a transaction: {}", e);
+ }
+ } while (withRetries && status != CommitStatus.SUCCESS);
+
+ // If the transaction was successful, increment succeeded transactions.
+ if (status == CommitStatus.SUCCESS) {
+ succeeded.incrementAndGet();
+ }
+
+ runTest(this.iteration.getAndIncrement());
+ });
+ }
+
+ /**
+ * Reports transaction performance.
+ */
+ private void reportPerformance() {
+ log.info("Attempted: {} Succeeded: {} Total iterations: {}",
+ attempted.getAndSet(0),
+ succeeded.getAndSet(0),
+ iteration.get());
+ }
+
+ /**
+ * Stops a test.
+ */
+ private void stopTest() {
+ testRunner.shutdown();
+ reporter.shutdown();
+ }
+}
diff --git a/apps/test/transaction-perf/src/main/java/org/onosproject/transactionperf/package-info.java b/apps/test/transaction-perf/src/main/java/org/onosproject/transactionperf/package-info.java
new file mode 100644
index 0000000..5dc3a75
--- /dev/null
+++ b/apps/test/transaction-perf/src/main/java/org/onosproject/transactionperf/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present 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.
+ */
+
+/**
+ * Performance test application for the primitive transactions.
+ */
+package org.onosproject.transactionperf;