feat: add benchmark target to Makefile #14
70
cmd/lambda/benchmark_test.go
Normal file
70
cmd/lambda/benchmark_test.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/internal/config"
|
||||||
|
"git.maximhutz.com/max/lambda/internal/engine"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/convert"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Helper function to run a single sample through the lambda interpreter.
|
||||||
|
func runSample(samplePath string) error {
|
||||||
|
// Read the sample file.
|
||||||
|
input, err := os.ReadFile(samplePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse code into syntax tree.
|
||||||
|
ast, err := saccharine.Parse(string(input))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile expression to lambda calculus.
|
||||||
|
compiled := convert.SaccharineToLambda(ast)
|
||||||
|
|
||||||
|
// Create minimal config for benchmarking.
|
||||||
|
cfg := &config.Config{
|
||||||
|
Source: config.StringSource{Data: ""},
|
||||||
|
Destination: config.StdoutDestination{},
|
||||||
|
Profile: "",
|
||||||
|
Explanation: false,
|
||||||
|
Statistics: false,
|
||||||
|
Verbose: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and run the engine.
|
||||||
|
process := engine.New(cfg, &compiled)
|
||||||
|
process.Run()
|
||||||
|
|
||||||
|
// Get final result (to ensure it's not optimized away).
|
||||||
|
_ = lambda.Stringify(compiled)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Benchmark all samples using sub-benchmarks.
|
||||||
|
func BenchmarkSamples(b *testing.B) {
|
||||||
|
samples := map[string]string{
|
||||||
|
"Church": "../../samples/church.txt",
|
||||||
|
"Fast": "../../samples/fast.txt",
|
||||||
|
"Saccharine": "../../samples/saccharine.txt",
|
||||||
|
"Simple": "../../samples/simple.txt",
|
||||||
|
"Thunk": "../../samples/thunk.txt",
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, path := range samples {
|
||||||
|
b.Run(name, func(b *testing.B) {
|
||||||
|
for b.Loop() {
|
||||||
|
if err := runSample(path); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user