-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ hs_err_pid* | |
/build-tools/build | ||
/*.txt | ||
/*.out | ||
/jmh-result*.json | ||
|
||
# Editors | ||
*.iml | ||
|
29 changes: 29 additions & 0 deletions
29
src/test/java/cc/tweaked/cobalt/benchmark/BenchmarkFast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/test/java/cc/tweaked/cobalt/benchmark/BenchmarkFull.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/test/java/cc/tweaked/cobalt/benchmark/FibRecursive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
src/test/java/cc/tweaked/cobalt/benchmark/LuaBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |