Skip to content

Commit

Permalink
Add custom timings for events, tasks and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 30, 2017
1 parent 0a031f5 commit 03567f1
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ You can either install the standalone helper plugin on your server, or shade the
<dependency>
<groupId>me.lucko</groupId>
<artifactId>helper</artifactId>
<version>1.6.4</version>
<version>1.6.5</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -590,7 +590,7 @@ repositories {
}
dependencies {
compile ("me.lucko:helper:1.6.4")
compile ("me.lucko:helper:1.6.5")
compile ("me.lucko:helper-sql:1.0.0")
compile ("me.lucko:helper-redis:1.0.1")
}
Expand Down
2 changes: 1 addition & 1 deletion helper-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<dependency>
<groupId>me.lucko</groupId>
<artifactId>helper</artifactId>
<version>1.6.4</version>
<version>1.6.5</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion helper-sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<dependency>
<groupId>me.lucko</groupId>
<artifactId>helper</artifactId>
<version>1.6.4</version>
<version>1.6.5</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
15 changes: 14 additions & 1 deletion helper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<artifactId>helper</artifactId>
<packaging>jar</packaging>
<version>1.6.4</version>
<version>1.6.5</version>

<name>helper</name>
<description>A utility to reduce boilerplate code in Bukkit plugins.</description>
Expand Down Expand Up @@ -74,8 +74,15 @@
<artifactSet>
<includes>
<include>com.flowpowered:flow-math</include>
<include>co.aikar:minecraft-timings</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>co.aikar.timings.lib</pattern>
<shadedPattern>me.lucko.helper.timings</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -134,6 +141,12 @@
<version>1.0.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>co.aikar</groupId>
<artifactId>minecraft-timings</artifactId>
<version>1.0.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
Expand Down
23 changes: 20 additions & 3 deletions helper/src/main/java/me/lucko/helper/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.ImmutableList;

import me.lucko.helper.plugin.ExtendedJavaPlugin;
import me.lucko.helper.timings.Timings;
import me.lucko.helper.utils.Color;
import me.lucko.helper.utils.Cooldown;
import me.lucko.helper.utils.CooldownCollection;
Expand All @@ -41,6 +42,9 @@
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import co.aikar.timings.lib.MCTiming;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -586,6 +590,7 @@ public FunctionalCommand handler(CommandHandler<T> handler) {
private static final class FunctionalCommandImpl implements FunctionalCommand, CommandExecutor {
private final ImmutableList<Predicate<CommandContext<?>>> predicates;
private final CommandHandler handler;
private MCTiming timing = null;

private FunctionalCommandImpl(ImmutableList<Predicate<CommandContext<?>>> predicates, CommandHandler handler) {
this.predicates = predicates;
Expand All @@ -607,13 +612,25 @@ public void handle(CommandContext<?> context) {
@Override
public void register(ExtendedJavaPlugin plugin, String... aliases) {
plugin.registerCommand(this, aliases);
timing = Timings.get().of("helper-commands: " + plugin.getName() + " - " + Arrays.toString(aliases));
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
CommandContext<CommandSender> context = new ImmutableCommandContext<>(sender, label, args);
handle(context);
return true;
try {
if (timing != null) {
timing.startTiming();
}

CommandContext<CommandSender> context = new ImmutableCommandContext<>(sender, label, args);
handle(context);
return true;

} finally {
if (timing != null) {
timing.stopTiming();
}
}
}
}

Expand Down
46 changes: 42 additions & 4 deletions helper/src/main/java/me/lucko/helper/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import me.lucko.helper.metadata.Metadata;
import me.lucko.helper.metadata.MetadataKey;
import me.lucko.helper.terminable.Terminable;
import me.lucko.helper.timings.Timings;
import me.lucko.helper.utils.Cooldown;
import me.lucko.helper.utils.CooldownCollection;
import me.lucko.helper.utils.LoaderUtils;
Expand All @@ -51,6 +52,8 @@
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin;

import co.aikar.timings.lib.MCTiming;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -389,7 +392,7 @@ public interface HandlerBuilder<T extends Event> {
* @throws NullPointerException if the handler is null
*/
default Handler<T> handler(Consumer<? super T> handler) {
return handler((h, t) -> handler.accept(t));
return handler(new DelegateBiConsumer<>(handler));
}

/**
Expand Down Expand Up @@ -518,7 +521,7 @@ public interface MergedHandlerBuilder<T> {
* @throws IllegalStateException if no events have been bound to
*/
default MergedHandler<T> handler(Consumer<? super T> handler) {
return handler((h, t) -> handler.accept(t));
return handler(new DelegateBiConsumer<>(handler));
}

/**
Expand Down Expand Up @@ -591,6 +594,14 @@ public interface DefaultFilters {

}

private static String getHandlerName(BiConsumer consumer) {
if (consumer instanceof DelegateBiConsumer) {
return ((DelegateBiConsumer) consumer).getDelegate().getClass().getName();
} else {
return consumer.getClass().getName();
}
}

private static class HandlerImpl<T extends Event> implements Handler<T>, EventExecutor {
private final Class<T> eventClass;
private final EventPriority priority;
Expand All @@ -600,6 +611,7 @@ private static class HandlerImpl<T extends Event> implements Handler<T>, EventEx
private final BiConsumer<? super T, Throwable> exceptionConsumer;
private final List<Predicate<T>> filters;
private final BiConsumer<Handler<T>, ? super T> handler;
private final MCTiming timing;

private final Listener listener = new Listener() {};
private final AtomicLong callCount = new AtomicLong(0);
Expand All @@ -613,6 +625,7 @@ private HandlerImpl(HandlerBuilderImpl<T> builder, BiConsumer<Handler<T>, ? supe
this.exceptionConsumer = builder.exceptionConsumer;
this.filters = ImmutableList.copyOf(builder.filters);
this.handler = handler;
this.timing = Timings.get().of("helper-events: " + getHandlerName(handler));
}

private void register(Plugin plugin) {
Expand Down Expand Up @@ -674,7 +687,10 @@ private boolean tryExpire(Listener listener, HandlerList handlerList) {

private void handle(T e) {
try {
handler.accept(this, e);
try (MCTiming t = timing.startTiming()) {
handler.accept(this, e);
}

callCount.incrementAndGet();
} catch (Throwable t) {
exceptionConsumer.accept(e, t);
Expand Down Expand Up @@ -735,6 +751,7 @@ private static class MergedHandlerImpl<T> implements MergedHandler<T>, EventExec
private final BiConsumer<Event, Throwable> exceptionConsumer;
private final List<Predicate<T>> filters;
private final BiConsumer<MergedHandler<T>, ? super T> handler;
private final MCTiming timing;

private final Listener listener = new Listener() {};
private final AtomicLong callCount = new AtomicLong(0);
Expand All @@ -748,6 +765,7 @@ private MergedHandlerImpl(MergedHandlerBuilderImpl<T> builder, BiConsumer<Merged
this.exceptionConsumer = builder.exceptionConsumer;
this.filters = ImmutableList.copyOf(builder.filters);
this.handler = handler;
this.timing = Timings.get().of("helper-events: " + getHandlerName(handler));
}

private void register(Plugin plugin) {
Expand Down Expand Up @@ -817,7 +835,10 @@ private boolean tryExpire() {

private void handle(Event event, T e) {
try {
handler.accept(this, e);
try (MCTiming t = timing.startTiming()) {
handler.accept(this, e);
}

callCount.incrementAndGet();
} catch (Throwable t) {
exceptionConsumer.accept(event, t);
Expand Down Expand Up @@ -1140,6 +1161,23 @@ public <T extends PlayerEvent> Predicate<T> playerHasMetadata(MetadataKey<?> key
}
}

private static final class DelegateBiConsumer<T, U> implements BiConsumer<T, U> {
private final Consumer<U> delegate;

private DelegateBiConsumer(Consumer<U> delegate) {
this.delegate = delegate;
}

public Consumer<U> getDelegate() {
return delegate;
}

@Override
public void accept(T t, U u) {
delegate.accept(u);
}
}

private Events() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}
Expand Down
Loading

0 comments on commit 03567f1

Please sign in to comment.