You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2018/11/24 09:04:03 UTC

[incubator-plc4x] branch feature/plc4j-scraper updated: [plc4j-scraper] Added a tet with "guarding" runnable that cancels tests and decreases performance massive.

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch feature/plc4j-scraper
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/feature/plc4j-scraper by this push:
     new 0504815  [plc4j-scraper] Added a tet with "guarding" runnable that cancels tests and decreases performance massive.
0504815 is described below

commit 05048154f30d3dfb119a8f9f40eed70d1b1c1c72
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sat Nov 24 10:03:57 2018 +0100

    [plc4j-scraper] Added a tet with "guarding" runnable that cancels tests and decreases performance massive.
---
 .../apache/plc4x/java/s7/ManualS7PlcDriverMT.java  | 60 +++++++++++++++++++---
 1 file changed, 53 insertions(+), 7 deletions(-)

diff --git a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java
index 868b1b8..54770a7 100644
--- a/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java
+++ b/plc4j/utils/scraper/src/test/java/org/apache/plc4x/java/s7/ManualS7PlcDriverMT.java
@@ -22,6 +22,7 @@ package org.apache.plc4x.java.s7;
 import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
 import org.apache.plc4x.java.PlcDriverManager;
 import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.messages.PlcReadResponse;
 import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
 import org.junit.Test;
@@ -34,11 +35,11 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 public class ManualS7PlcDriverMT {
 
-//    public static final String CONN_STRING = "s7://10.10.64.22/0/1";
-//    public static final String FIELD_STRING = "%DB225:DBW0:INT";
+    public static final String CONN_STRING = "s7://10.10.64.22/0/1";
+    public static final String FIELD_STRING = "%DB225:DBW0:INT";
 
-    public static final String CONN_STRING = "s7://10.10.64.20/0/1";
-    public static final String FIELD_STRING = "%DB3:DBD32:DINT";
+//    public static final String CONN_STRING = "s7://10.10.64.20/0/1";
+//    public static final String FIELD_STRING = "%DB3:DBD32:DINT";
 
     @Test
     public void simpleLoop() {
@@ -75,6 +76,50 @@ public class ManualS7PlcDriverMT {
         executorService.awaitTermination(100, TimeUnit.SECONDS);
     }
 
+    @Test
+    public void scheduledCancellingLoop() throws InterruptedException, PlcConnectionException {
+        PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
+        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
+        DescriptiveStatistics statistics = new DescriptiveStatistics();
+
+        final int period = 10;
+        int numberOfRuns = 1000;
+        AtomicInteger counter = new AtomicInteger(0);
+
+        // Warmup
+        plcDriverManager.getConnection(CONN_STRING);
+
+        Runnable iteration = new Runnable() {
+            @Override
+            public void run() {
+                System.out.println("Setting a request / guard...");
+                CompletableFuture<Double> requestFuture = CompletableFuture.supplyAsync(
+                    () -> ManualS7PlcDriverMT.this.runSingleRequest(plcDriverManager)
+                );
+                executorService.schedule(() -> {
+                    if (!requestFuture.isDone()) {
+                        requestFuture.cancel(true);
+                        System.err.println("Cancel a future!");
+                    } else {
+                        System.out.println("Request finished successfully");
+                        try {
+                            statistics.addValue(requestFuture.get());
+                        } catch (InterruptedException | ExecutionException e) {
+                            // do nothing...
+                        }
+                    }
+                    if (counter.getAndIncrement() >= numberOfRuns) {
+                        executorService.shutdown();
+                        ManualS7PlcDriverMT.this.printStatistics(statistics);
+                    }
+                }, period, TimeUnit.MILLISECONDS);
+            }
+        };
+
+        executorService.scheduleAtFixedRate(iteration, 0, period, TimeUnit.MILLISECONDS);
+        executorService.awaitTermination(100, TimeUnit.SECONDS);
+    }
+
     private double runSingleRequest(PlcDriverManager plcDriverManager) {
         long start = System.nanoTime();
         try (PlcConnection connection = plcDriverManager.getConnection(CONN_STRING)) {
@@ -89,12 +134,13 @@ public class ManualS7PlcDriverMT {
             e.printStackTrace();
         }
         long end = System.nanoTime();
-        return (double)end-start;
+        return (double) end - start;
     }
 
     private void printStatistics(DescriptiveStatistics statistics) {
-        System.out.println("Mean response time: " + TimeUnit.NANOSECONDS.toMillis((long)statistics.getMean()) + " ms");
-        System.out.println("Median response time: " + TimeUnit.NANOSECONDS.toMillis((long)statistics.getPercentile(50)) + " ms");
+        System.out.println("Number of responses: " + statistics.getN());
+        System.out.println("Mean response time: " + TimeUnit.NANOSECONDS.toMillis((long) statistics.getMean()) + " ms");
+        System.out.println("Median response time: " + TimeUnit.NANOSECONDS.toMillis((long) statistics.getPercentile(50)) + " ms");
         for (int i = 10; i <= 90; i += 10) {
             System.out.println(String.format(Locale.ENGLISH, "Percentile %3d %%: %5d ms", i, TimeUnit.NANOSECONDS.toMillis((long) statistics.getPercentile(i))));
         }