From 2a7b1211bf00aa77742b806bbd3c82a10cd557bb Mon Sep 17 00:00:00 2001 From: yusshu Date: Tue, 26 Dec 2023 15:21:41 -0500 Subject: [PATCH] feat: add comparison benchmark with bedrockk's MoLang and MoonflowerTeam's molang-compiler --- build.gradle.kts | 13 ++++ .../team/unnamed/mocha/CompareBenchmark.java | 75 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/jmh/java/team/unnamed/mocha/CompareBenchmark.java diff --git a/build.gradle.kts b/build.gradle.kts index 0e8bcbe..34f5cdf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,10 +2,19 @@ plugins { `java-library` `maven-publish` id("org.cadixdev.licenser") version "0.6.1" + id("me.champeau.jmh") version "0.7.2" } repositories { mavenCentral() + maven { + url = uri("https://maven.pkg.github.com/bedrockk/MoLang") + credentials { + username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") + password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN") + } + } + maven("https://maven.blamejared.com/") // moonflower's molang-compiler } dependencies { @@ -13,6 +22,10 @@ dependencies { compileOnlyApi("org.jetbrains:annotations:24.0.1") testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") + + // performance comparison with other libraries + jmhImplementation("com.bedrockk:molang:1.0-SNAPSHOT") + jmhImplementation("gg.moonflower:molang-compiler:3.0.4.16") } tasks { diff --git a/src/jmh/java/team/unnamed/mocha/CompareBenchmark.java b/src/jmh/java/team/unnamed/mocha/CompareBenchmark.java new file mode 100644 index 0000000..cda0bf0 --- /dev/null +++ b/src/jmh/java/team/unnamed/mocha/CompareBenchmark.java @@ -0,0 +1,75 @@ +package team.unnamed.mocha; + +import com.bedrockk.molang.MoLang; +import com.bedrockk.molang.parser.Expression; +import com.bedrockk.molang.runtime.MoLangRuntime; +import gg.moonflower.molangcompiler.api.MolangCompiler; +import gg.moonflower.molangcompiler.api.MolangEnvironment; +import gg.moonflower.molangcompiler.api.MolangExpression; +import gg.moonflower.molangcompiler.api.MolangRuntime; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +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 org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import team.unnamed.mocha.runtime.MochaFunction; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +@State(Scope.Benchmark) +@BenchmarkMode(Mode.SampleTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +public class CompareBenchmark { + private MochaEngine mocha; // unnamed's mocha + private MolangEnvironment environment; // moonflower's molang-compiler + private MoLangRuntime mlRuntime; // bedrockk's Molang + + private MochaFunction function; // unnamed's mocha + private MolangExpression mlExpression; // moonflower's molang-compiler + private List mlExpressions; // bedrockk's MoLang + + public static void main(final String[] args) throws RunnerException { + final Options opt = new OptionsBuilder() + .include(CompareBenchmark.class.getSimpleName()) + .forks(1) + .build(); + new Runner(opt).run(); + } + + @Setup(Level.Trial) + public void prepare() throws Exception { + mocha = MochaEngine.createStandard(); + environment = MolangRuntime.runtime().create(); + mlRuntime = MoLang.newRuntime(); + + final String expr = "temp.t = 3; return 3*temp.t*temp.t - 2*temp.t*temp.t*temp.t;"; + + function = mocha.compile(expr); + mlExpression = MolangCompiler.create(MolangCompiler.DEFAULT_FLAGS, getClass().getClassLoader()).compile(expr); + mlExpressions = MoLang.newParser("temp.t = 3; return 3*temp.t*temp.t - 2*temp.t*temp.t*temp.t;").parse(); + } + + @Benchmark + public void bedrockk_MoLang() { + mlRuntime.execute(mlExpressions); + } + + @Benchmark + public void unnamed_mocha() { + function.evaluate(); + } + + @Benchmark + public void moonflower_molang_compiler() throws Exception { + //noinspection OverrideOnly + mlExpression.get(environment); + } +}