diff --git a/jmh-benchmarks/src/jmh/java/net/minestom/jmh/tag/TagWriteBenchmark.java b/jmh-benchmarks/src/jmh/java/net/minestom/jmh/tag/TagWriteBenchmark.java new file mode 100644 index 000000000..d64e01254 --- /dev/null +++ b/jmh-benchmarks/src/jmh/java/net/minestom/jmh/tag/TagWriteBenchmark.java @@ -0,0 +1,56 @@ +package net.minestom.jmh.tag; + +import net.minestom.server.tag.Tag; +import net.minestom.server.tag.TagHandler; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound; +import org.openjdk.jmh.annotations.*; + +import java.util.concurrent.ConcurrentHashMap; +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 TagWriteBenchmark { + static final Tag TAG = Tag.String("key"); + + TagHandler tagHandler; + Tag secondTag; + + MutableNBTCompound compound; + + @Setup + public void setup() { + // Tag benchmark + this.tagHandler = TagHandler.newHandler(); + tagHandler.setTag(TAG, "value"); + secondTag = Tag.String("key"); + // NBT benchmark + this.compound = new MutableNBTCompound(new ConcurrentHashMap<>()); + compound.set("key", NBT.String("value")); + } + + @Benchmark + public void writeConstantTag() { + tagHandler.setTag(TAG, "value"); + } + + @Benchmark + public void writeDifferentTag() { + tagHandler.setTag(secondTag, "value"); + } + + @Benchmark + public void writeNewTag() { + tagHandler.setTag(Tag.String("key"), "value"); + } + + @Benchmark + public void writeConstantTagFromCompound() { + compound.setString("key", "value"); + } +} diff --git a/jmh-benchmarks/src/jmh/java/net/minestom/jmh/tag/TagWritePathBenchmark.java b/jmh-benchmarks/src/jmh/java/net/minestom/jmh/tag/TagWritePathBenchmark.java new file mode 100644 index 000000000..9b001d524 --- /dev/null +++ b/jmh-benchmarks/src/jmh/java/net/minestom/jmh/tag/TagWritePathBenchmark.java @@ -0,0 +1,39 @@ +package net.minestom.jmh.tag; + +import net.minestom.server.tag.Tag; +import net.minestom.server.tag.TagHandler; +import org.openjdk.jmh.annotations.*; + +import java.util.ArrayList; +import java.util.List; +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 TagWritePathBenchmark { + @Param({"0", "1", "2", "3"}) + public int scope; + + TagHandler tagHandler; + Tag tag; + + @Setup + public void setup() { + this.tagHandler = TagHandler.newHandler(); + + List path = new ArrayList<>(scope); + for (int i = 0; i < scope; i++) path.add("key" + i); + this.tag = Tag.String("key").path(path.toArray(String[]::new)); + + tagHandler.setTag(tag, "value"); + } + + @Benchmark + public void write() { + tagHandler.setTag(tag, "value"); + } +}