From b732a0c3d10792a7eb3f790f74ecb5743acd8763 Mon Sep 17 00:00:00 2001 From: Daniil Date: Thu, 26 Jul 2018 17:18:32 +0700 Subject: [PATCH] Added average overhead metrics --- .../mc/core/events/async/AsyncEventLoop.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/event-loop/src/main/java/mc/core/events/async/AsyncEventLoop.java b/event-loop/src/main/java/mc/core/events/async/AsyncEventLoop.java index 4a6a1e6..9a0b190 100644 --- a/event-loop/src/main/java/mc/core/events/async/AsyncEventLoop.java +++ b/event-loop/src/main/java/mc/core/events/async/AsyncEventLoop.java @@ -1,5 +1,6 @@ package mc.core.events.async; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import mc.core.events.Event; @@ -9,13 +10,17 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -@SuppressWarnings("Duplicates") @Slf4j public class AsyncEventLoop extends AdvancedEventLoop { + private static final double EMAPeriod = (2D / (50D + 1D)); + @Getter + private double avgOverhead = 0; private ExecutorService preEventExecutor = Executors.newSingleThreadExecutor(); @Override public void callEvent(Event event) { + long wholeMethodBenchmark = System.nanoTime(); + long asyncExecutionWaitTime = 0, syncExecutionTime = 0, tempTime; Class eventType = event.getClass(); if (handlers.containsKey(eventType)) { @@ -37,11 +42,13 @@ public class AsyncEventLoop extends AdvancedEventLoop { } // Await for them to complete + tempTime = System.nanoTime(); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } + asyncExecutionWaitTime = System.nanoTime() - tempTime; // Synchronously invoke EventHandlers with // data obtained from EventBatch @@ -49,16 +56,28 @@ public class AsyncEventLoop extends AdvancedEventLoop { ExecutorLink link = handlerList.get(i); if (!link.isIgnoreCancelled() || !event.isCanceled()) { try { + tempTime = System.nanoTime(); if (link.getResultInjection() != null) link.getMethod().invoke(link.getObject(), event, eventBatch.getInjectionObject(i)); else link.getMethod().invoke(link.getObject(), event); + syncExecutionTime += System.nanoTime() - tempTime; } catch (IllegalAccessException | InvocationTargetException e) { log.error("Exception caught while attempting to dispatch {}.", eventType.getSimpleName(), e); } } } } + + wholeMethodBenchmark = System.nanoTime() - wholeMethodBenchmark; + long overhead = wholeMethodBenchmark - asyncExecutionWaitTime - syncExecutionTime; + + // Now we calculate exponential moving average of + // overhead timings + if (avgOverhead == 0) + avgOverhead = overhead; + else + avgOverhead = (overhead - avgOverhead) * EMAPeriod + avgOverhead; } }