From 8ed8f02bee9331901a8b8054f9a41cc7feea8c3e Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 16 Jan 2018 17:10:42 +0100 Subject: [PATCH] Benchmark parsing --- .../benchmarks/ParsingBenchmarks.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/test/java/com/mojang/brigadier/benchmarks/ParsingBenchmarks.java diff --git a/src/test/java/com/mojang/brigadier/benchmarks/ParsingBenchmarks.java b/src/test/java/com/mojang/brigadier/benchmarks/ParsingBenchmarks.java new file mode 100644 index 0000000..1a904ce --- /dev/null +++ b/src/test/java/com/mojang/brigadier/benchmarks/ParsingBenchmarks.java @@ -0,0 +1,118 @@ +package com.mojang.brigadier.benchmarks; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.tree.LiteralCommandNode; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +import java.util.concurrent.TimeUnit; + +import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal; + +@State(Scope.Benchmark) +public class ParsingBenchmarks { + private CommandDispatcher subject; + + @Setup + public void setup() { + subject = new CommandDispatcher<>(); + subject.register( + literal("a") + .then( + literal("1") + .then(literal("i").executes(c -> 0)) + .then(literal("ii").executes(c -> 0)) + ) + .then( + literal("2") + .then(literal("i").executes(c -> 0)) + .then(literal("ii").executes(c -> 0)) + ) + ); + subject.register(literal("b").then(literal("1").executes(c -> 0))); + subject.register(literal("c").executes(c -> 0)); + subject.register(literal("d").requires(s -> false).executes(c -> 0)); + subject.register( + literal("e") + .executes(c -> 0) + .then( + literal("1") + .executes(c -> 0) + .then(literal("i").executes(c -> 0)) + .then(literal("ii").executes(c -> 0)) + ) + ); + subject.register( + literal("f") + .then( + literal("1") + .then(literal("i").executes(c -> 0)) + .then(literal("ii").executes(c -> 0).requires(s -> false)) + ) + .then( + literal("2") + .then(literal("i").executes(c -> 0).requires(s -> false)) + .then(literal("ii").executes(c -> 0)) + ) + ); + subject.register( + literal("g") + .executes(c -> 0) + .then(literal("1").then(literal("i").executes(c -> 0))) + ); + final LiteralCommandNode h = subject.register( + literal("h") + .executes(c -> 0) + .then(literal("1").then(literal("i").executes(c -> 0))) + .then(literal("2").then(literal("i").then(literal("ii").executes(c -> 0)))) + .then(literal("3").executes(c -> 0)) + ); + subject.register( + literal("i") + .executes(c -> 0) + .then(literal("1").executes(c -> 0)) + .then(literal("2").executes(c -> 0)) + ); + subject.register( + literal("j") + .redirect(subject.getRoot()) + ); + subject.register( + literal("k") + .redirect(h) + ); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public void parse_a1i() { + subject.parse("a 1 i", new Object()); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public void parse_c() { + subject.parse("c", new Object()); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public void parse_k1i() { + subject.parse("k 1 i", new Object()); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + public void parse_() { + subject.parse("c", new Object()); + } +}