From eb09e65f927f2dfa44ab34f3eddd33a8363da437 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Mon, 10 Jun 2024 16:02:43 +0200 Subject: [PATCH] latency --- .../impl/openweathermap/OpenWeatherMapProvider.java | 1 - .../java/eu/m724/wtapi/thunder/ThunderProvider.java | 10 ++++++++++ .../impl/blitzortung/BlitzortungProvider.java | 13 +++++++++++++ .../java/eu/m724/wtapi/thunder/BlitzortungTest.java | 9 +++++---- .../eu/m724/wtapi/thunder/MockThunderProvider.java | 5 +++++ .../eu/m724/wtapi/thunder/ThunderProviderTest.java | 7 +++++-- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/m724/wtapi/provider/impl/openweathermap/OpenWeatherMapProvider.java b/src/main/java/eu/m724/wtapi/provider/impl/openweathermap/OpenWeatherMapProvider.java index 1af1cac..e6e4e51 100644 --- a/src/main/java/eu/m724/wtapi/provider/impl/openweathermap/OpenWeatherMapProvider.java +++ b/src/main/java/eu/m724/wtapi/provider/impl/openweathermap/OpenWeatherMapProvider.java @@ -9,7 +9,6 @@ import java.net.http.HttpResponse.BodyHandlers; import java.util.ArrayList; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; -import java.util.concurrent.ExecutionException; import com.google.gson.JsonObject; import com.google.gson.JsonParser; diff --git a/src/main/java/eu/m724/wtapi/thunder/ThunderProvider.java b/src/main/java/eu/m724/wtapi/thunder/ThunderProvider.java index 598adb0..18f986c 100644 --- a/src/main/java/eu/m724/wtapi/thunder/ThunderProvider.java +++ b/src/main/java/eu/m724/wtapi/thunder/ThunderProvider.java @@ -7,6 +7,10 @@ import eu.m724.wtapi.provider.exception.ProviderException; public abstract class ThunderProvider { + /** + * initialize and test provider + * @throws ProviderException + */ public abstract void init() throws ProviderException; /** @@ -32,4 +36,10 @@ public abstract class ThunderProvider { * @return delay in ms */ public abstract int getDelay(); + + /** + * this is not {@link/getDelay} + * @return latency to api in ms + */ + public abstract long getLatency(); } diff --git a/src/main/java/eu/m724/wtapi/thunder/impl/blitzortung/BlitzortungProvider.java b/src/main/java/eu/m724/wtapi/thunder/impl/blitzortung/BlitzortungProvider.java index f06d77d..f3679e1 100644 --- a/src/main/java/eu/m724/wtapi/thunder/impl/blitzortung/BlitzortungProvider.java +++ b/src/main/java/eu/m724/wtapi/thunder/impl/blitzortung/BlitzortungProvider.java @@ -15,6 +15,9 @@ public class BlitzortungProvider extends ThunderProvider { private long reconnectPending; private final int delay = 10000; + + private long latency; + private int latencySamplesCount; @Override public void init() throws ProviderException { @@ -75,7 +78,17 @@ public class BlitzortungProvider extends ThunderProvider { } void submitStrike(Coordinates coordinates, long timestamp) { + long now = System.currentTimeMillis(); + long delay = now - timestamp; + + latency += (delay - latency) / ++latencySamplesCount; + strikes.add(new TimedStrike(timestamp, coordinates)); } + @Override + public long getLatency() { + return latency; + } + } diff --git a/src/test/java/eu/m724/wtapi/thunder/BlitzortungTest.java b/src/test/java/eu/m724/wtapi/thunder/BlitzortungTest.java index 65f56e1..e154aaa 100644 --- a/src/test/java/eu/m724/wtapi/thunder/BlitzortungTest.java +++ b/src/test/java/eu/m724/wtapi/thunder/BlitzortungTest.java @@ -9,14 +9,13 @@ import eu.m724.wtapi.thunder.impl.blitzortung.BlitzortungProvider; public class BlitzortungTest { @Test - public void lightningMapsTest() throws InterruptedException { + public void blitzortungTest() throws InterruptedException { ArrayList coordinatesList = new ArrayList<>(); ThunderProvider provider = new BlitzortungProvider(); provider.registerStrikeHandler(coordinates -> { coordinatesList.add(coordinates); - System.out.printf("%f %f\n", coordinates.latitude, coordinates.longitude); }); provider.init(); @@ -25,12 +24,14 @@ public class BlitzortungTest { for (int i=0; i < 100; i++) { provider.tick(); + int size = coordinatesList.size(); + if (size > 0) + System.out.printf("Last from tick: %f %f (total %d)\n", coordinatesList.get(size-1).latitude, coordinatesList.get(size-1).longitude, size); Thread.sleep(100); } System.out.printf("Strikes in the last 10s: %d\n", coordinatesList.size()); - if (coordinatesList.size() > 0) // lightningmaps can be unreliable - System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude); + System.out.printf("Latency: %dms\n", provider.getLatency()); } } diff --git a/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java b/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java index 4976d5b..5cdb950 100644 --- a/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java +++ b/src/test/java/eu/m724/wtapi/thunder/MockThunderProvider.java @@ -69,4 +69,9 @@ public class MockThunderProvider extends ThunderProvider { return 10000; } + @Override + public long getLatency() { + return 690; + } + } diff --git a/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java b/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java index dce3efa..f1670ce 100644 --- a/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java +++ b/src/test/java/eu/m724/wtapi/thunder/ThunderProviderTest.java @@ -22,13 +22,16 @@ public class ThunderProviderTest { for (int i=0; i < 50; i++) { provider.tick(); + int size = coordinatesList.size(); + if (size > 0) + System.out.printf("Last from tick: %f %f (total %d)\n", coordinatesList.get(size-1).latitude, coordinatesList.get(size-1).longitude, size); Thread.sleep(50); } provider.stop(); - System.out.printf("Strikes in the last 5s: %d\n", coordinatesList.size()); - System.out.printf("%f %f", coordinatesList.get(0).latitude, coordinatesList.get(0).longitude); + System.out.printf("Strikes in the last 2.5s: %d\n", coordinatesList.size()); + System.out.printf("Latency: %dms\n", provider.getLatency()); assert coordinatesList.size() == 25; }