Archived
0

Added average overhead metrics

This commit is contained in:
Daniil
2018-07-26 17:18:32 +07:00
parent 8c6c6bde6c
commit b732a0c3d1

View File

@@ -1,5 +1,6 @@
package mc.core.events.async; package mc.core.events.async;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.core.events.Event; import mc.core.events.Event;
@@ -9,13 +10,17 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@SuppressWarnings("Duplicates")
@Slf4j @Slf4j
public class AsyncEventLoop extends AdvancedEventLoop { public class AsyncEventLoop extends AdvancedEventLoop {
private static final double EMAPeriod = (2D / (50D + 1D));
@Getter
private double avgOverhead = 0;
private ExecutorService preEventExecutor = Executors.newSingleThreadExecutor(); private ExecutorService preEventExecutor = Executors.newSingleThreadExecutor();
@Override @Override
public void callEvent(Event event) { public void callEvent(Event event) {
long wholeMethodBenchmark = System.nanoTime();
long asyncExecutionWaitTime = 0, syncExecutionTime = 0, tempTime;
Class<? extends Event> eventType = event.getClass(); Class<? extends Event> eventType = event.getClass();
if (handlers.containsKey(eventType)) { if (handlers.containsKey(eventType)) {
@@ -37,11 +42,13 @@ public class AsyncEventLoop extends AdvancedEventLoop {
} }
// Await for them to complete // Await for them to complete
tempTime = System.nanoTime();
try { try {
latch.await(); latch.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
asyncExecutionWaitTime = System.nanoTime() - tempTime;
// Synchronously invoke EventHandlers with // Synchronously invoke EventHandlers with
// data obtained from EventBatch // data obtained from EventBatch
@@ -49,16 +56,28 @@ public class AsyncEventLoop extends AdvancedEventLoop {
ExecutorLink link = handlerList.get(i); ExecutorLink link = handlerList.get(i);
if (!link.isIgnoreCancelled() || !event.isCanceled()) { if (!link.isIgnoreCancelled() || !event.isCanceled()) {
try { try {
tempTime = System.nanoTime();
if (link.getResultInjection() != null) if (link.getResultInjection() != null)
link.getMethod().invoke(link.getObject(), event, eventBatch.getInjectionObject(i)); link.getMethod().invoke(link.getObject(), event, eventBatch.getInjectionObject(i));
else else
link.getMethod().invoke(link.getObject(), event); link.getMethod().invoke(link.getObject(), event);
syncExecutionTime += System.nanoTime() - tempTime;
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
log.error("Exception caught while attempting to dispatch {}.", eventType.getSimpleName(), 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;
} }
} }