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 20:57:40 UTC

[incubator-plc4x] 02/11: [plc4j-scraper] Added another test + logback.

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

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

commit 3fabc85b4e341e14f6f8d13780070b938a98ed12
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Sat Nov 24 09:32:33 2018 +0100

    [plc4j-scraper] Added another test + logback.
---
 .../apache/plc4x/java/s7/ManualS7PlcDriverMT.java  | 68 ++++++++++++++--------
 plc4j/utils/scraper/src/test/resources/logback.xml | 36 ++++++++++++
 2 files changed, 80 insertions(+), 24 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 af60176..868b1b8 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
@@ -25,20 +25,20 @@ import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.messages.PlcReadResponse;
 import org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager;
 import org.junit.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
-import java.util.ArrayList;
 import java.util.Locale;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
+import java.util.concurrent.*;
+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() {
@@ -46,30 +46,50 @@ public class ManualS7PlcDriverMT {
 
         DescriptiveStatistics statistics = new DescriptiveStatistics();
         for (int i = 1; i <= 1000; i++) {
+            double timeNs = runSingleRequest(plcDriverManager);
+            statistics.addValue(timeNs);
+        }
 
-            long start = System.nanoTime();
-            try (PlcConnection connection = plcDriverManager.getConnection(CONN_STRING)) {
-                CompletableFuture<? extends PlcReadResponse> future = connection.readRequestBuilder()
-                    .addItem("distance", FIELD_STRING)
-                    .build()
-                    .execute();
+        printStatistics(statistics);
+    }
 
-                PlcReadResponse response = future.get(10, TimeUnit.SECONDS);
+    @ParameterizedTest
+    @ValueSource(ints = {1, 5, 10, 20})
+    public void scheduledLoop(int period) throws InterruptedException {
+        PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
+        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
+        DescriptiveStatistics statistics = new DescriptiveStatistics();
 
-                System.out.println(i + " " + response.getLong("distance"));
-            } catch (Exception e) {
-                e.printStackTrace();
+        int numberOfRuns = 1000;
+        AtomicInteger counter = new AtomicInteger(0);
+        executorService.scheduleAtFixedRate(() -> {
+            // System.out.println("Run: " + counter.get());
+            double timeNs = runSingleRequest(plcDriverManager);
+            statistics.addValue(timeNs);
+            if (counter.getAndIncrement() >= numberOfRuns) {
+                executorService.shutdown();
+                printStatistics(statistics);
             }
-            long end = System.nanoTime();
-            statistics.addValue((double)(end-start));
-        }
+        }, 0, period, TimeUnit.MILLISECONDS);
 
-        printStatistics(statistics);
+        executorService.awaitTermination(100, TimeUnit.SECONDS);
     }
 
-    @Test
-    public void scheduledLoop() {
+    private double runSingleRequest(PlcDriverManager plcDriverManager) {
+        long start = System.nanoTime();
+        try (PlcConnection connection = plcDriverManager.getConnection(CONN_STRING)) {
+            CompletableFuture<? extends PlcReadResponse> future = connection.readRequestBuilder()
+                .addItem("distance", FIELD_STRING)
+                .build()
+                .execute();
 
+            PlcReadResponse response = future.get(10, TimeUnit.SECONDS);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        long end = System.nanoTime();
+        return (double)end-start;
     }
 
     private void printStatistics(DescriptiveStatistics statistics) {
diff --git a/plc4j/utils/scraper/src/test/resources/logback.xml b/plc4j/utils/scraper/src/test/resources/logback.xml
new file mode 100644
index 0000000..c562020
--- /dev/null
+++ b/plc4j/utils/scraper/src/test/resources/logback.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you 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.
+  -->
+<configuration xmlns="http://ch.qos.logback/xml/ns/logback"
+               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+               xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <!-- encoders are assigned the type
+         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
+    <encoder>
+      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <root level="INFO">
+    <appender-ref ref="STDOUT"/>
+  </root>
+
+</configuration>
\ No newline at end of file