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;
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<? extends Event> 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;
}
}