Skip to content

Commit

Permalink
feat: add comparison benchmark with bedrockk's MoLang and MoonflowerT…
Browse files Browse the repository at this point in the history
…eam's molang-compiler
  • Loading branch information
yusshu committed Dec 26, 2023
1 parent 4416a31 commit 2a7b121
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@ 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 {
api("org.javassist:javassist:3.30.1-GA")
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 {
Expand Down
75 changes: 75 additions & 0 deletions src/jmh/java/team/unnamed/mocha/CompareBenchmark.java
Original file line number Diff line number Diff line change
@@ -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<Expression> 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);
}
}

0 comments on commit 2a7b121

Please sign in to comment.