Skip to content

Commit

Permalink
Basic benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidDev committed Jan 17, 2024
1 parent 2e9c89b commit ec46468
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ hs_err_pid*
/build-tools/build
/*.txt
/*.out
/jmh-result*.json

# Editors
*.iml
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/cc/tweaked/cobalt/benchmark/BenchmarkFast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cc.tweaked.cobalt.benchmark;

import org.openjdk.jmh.results.format.ResultFormatType;
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 org.openjdk.jmh.runner.options.TimeValue;

/**
* Runs all benchmarks with a minimal number of warmup runs and forks, allowing for quick testing.
*/
public final class BenchmarkFast {
private BenchmarkFast() {
}

public static void main(String... args) throws RunnerException {
Options opts = new OptionsBuilder()
.include("cc.tweaked.cobalt.benchmark.*")
.warmupIterations(1)
.measurementIterations(2)
.measurementTime(TimeValue.milliseconds(3000))
.jvmArgsPrepend("-server")
.resultFormat(ResultFormatType.JSON)
.forks(1)
.build();
new Runner(opts).run();
}
}
29 changes: 29 additions & 0 deletions src/test/java/cc/tweaked/cobalt/benchmark/BenchmarkFull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cc.tweaked.cobalt.benchmark;

import org.openjdk.jmh.results.format.ResultFormatType;
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 org.openjdk.jmh.runner.options.TimeValue;

/**
* Runs all benchmarks within the {@code cc.tweaked.cobalt.benchmark} package.
*/
public final class BenchmarkFull {
private BenchmarkFull() {
}

public static void main(String... args) throws RunnerException {
Options opts = new OptionsBuilder()
.include("cc.tweaked.cobalt.benchmark.*")
.warmupIterations(3)
.measurementIterations(5)
.measurementTime(TimeValue.milliseconds(3000))
.jvmArgsPrepend("-server")
.resultFormat(ResultFormatType.JSON)
.forks(3)
.build();
new Runner(opts).run();
}
}
23 changes: 23 additions & 0 deletions src/test/java/cc/tweaked/cobalt/benchmark/FibLoop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cc.tweaked.cobalt.benchmark;

import org.openjdk.jmh.annotations.*;
import org.squiddev.cobalt.ValueFactory;

import java.util.concurrent.TimeUnit;

/**
* Evaluates the nth Fibonacci number using a loop.
*/
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class FibLoop extends LuaBenchmark {
public FibLoop() {
super("""
local n = ...
local a, b = 1, 1 for i = 1, n do a, b = a + b, a end
return a
""", state -> ValueFactory.valueOf(200)
);
}
}
25 changes: 25 additions & 0 deletions src/test/java/cc/tweaked/cobalt/benchmark/FibRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cc.tweaked.cobalt.benchmark;

import org.openjdk.jmh.annotations.*;
import org.squiddev.cobalt.ValueFactory;

import java.util.concurrent.TimeUnit;

/**
* Evaluates the nth Fibonacci number using a recursive function.
*/
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class FibRecursive extends LuaBenchmark {
public FibRecursive() {
super("""
local function fib(n)
if n <= 2 then return 1 end
return fib(n - 1) + fib(n - 2)
end
return fib(...)
""", state -> ValueFactory.valueOf(10)
);
}
}
47 changes: 47 additions & 0 deletions src/test/java/cc/tweaked/cobalt/benchmark/LuaBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cc.tweaked.cobalt.benchmark;

import org.openjdk.jmh.annotations.*;
import org.squiddev.cobalt.LuaError;
import org.squiddev.cobalt.LuaState;
import org.squiddev.cobalt.LuaThread;
import org.squiddev.cobalt.Varargs;
import org.squiddev.cobalt.compiler.CompileException;
import org.squiddev.cobalt.compiler.LoadState;
import org.squiddev.cobalt.function.LuaFunction;
import org.squiddev.cobalt.lib.CoreLibraries;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public abstract class LuaBenchmark {
private final String program;
private final Function<LuaState, Varargs> argsFactory;

private LuaState state;
private LuaFunction function;
private Varargs args;

protected LuaBenchmark(String program, Function<LuaState, Varargs> argsFactory) {
this.program = program;
this.argsFactory = argsFactory;
}

@Setup(Level.Iteration)
public final void setup() throws LuaError, CompileException {
state = LuaState.builder().build();
CoreLibraries.standardGlobals(state);

args = argsFactory.apply(state);
function = LoadState.load(state, new ByteArrayInputStream(program.getBytes(StandardCharsets.UTF_8)), "=input", state.globals());
}

@Benchmark
public final Varargs run() throws Exception {
return LuaThread.runMain(state, function, args);
}
}
28 changes: 28 additions & 0 deletions src/test/java/cc/tweaked/cobalt/benchmark/SumPairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cc.tweaked.cobalt.benchmark;

import org.openjdk.jmh.annotations.*;
import org.squiddev.cobalt.LuaTable;
import org.squiddev.cobalt.ValueFactory;

import java.util.concurrent.TimeUnit;

/**
* Creates a table mapping strings to numbers, then loops over adding them all up.
*/
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class SumPairs extends LuaBenchmark {
public SumPairs() {
super("""
local tbl = ...
local sum = 0
for k, v in pairs(tbl) do sum = sum + v end
return sum
""", state -> {
var tbl = new LuaTable();
for (int i = 1; i < 256; i++) tbl.rawset("key_" + i, ValueFactory.valueOf(i));
return tbl;
});
}
}

0 comments on commit ec46468

Please sign in to comment.