From 0bd736d837ffcb642495a974b97160cca48e97c3 Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 5 Jan 2022 02:55:42 +0100 Subject: [PATCH] Bench node children --- .../jmh/event/MultiNodeBenchmark.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 jmh-benchmarks/src/jmh/java/net/minestom/jmh/event/MultiNodeBenchmark.java diff --git a/jmh-benchmarks/src/jmh/java/net/minestom/jmh/event/MultiNodeBenchmark.java b/jmh-benchmarks/src/jmh/java/net/minestom/jmh/event/MultiNodeBenchmark.java new file mode 100644 index 000000000..c5446f49a --- /dev/null +++ b/jmh-benchmarks/src/jmh/java/net/minestom/jmh/event/MultiNodeBenchmark.java @@ -0,0 +1,51 @@ +package net.minestom.jmh.event; + +import net.minestom.server.event.Event; +import net.minestom.server.event.EventNode; +import org.openjdk.jmh.annotations.*; + +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) +@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS) +@Fork(3) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Benchmark) +public class MultiNodeBenchmark { + + @Param({"0", "1", "3", "10"}) + public int children; + + private EventNode node; + + record TestEvent() implements Event { + } + + record TestEvent2() implements Event { + } + + @Setup + public void setup() { + node = EventNode.all("node"); + for (int i = 0; i < children; i++) { + var child = EventNode.all("child-" + i); + child.addListener(TestEvent.class, e -> { + // Empty + }); + + node.addChild(child); + + // Real-world code are very unlikely to use entirely empty nodes. + // This ensures that the handle map is properly lazily initialized to prevent fast exits. + child.addListener(TestEvent2.class, e -> { + // Empty + }).call(new TestEvent2()); + } + } + + @Benchmark + public void call() { + node.call(new TestEvent()); + } +}