Skip to content

Commit

Permalink
Add benchmark tests based on golden files
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkschumacher committed May 13, 2024
1 parent 018a994 commit 069067f
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/golden/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// © 2019-present nextmv.io inc

package main

import (
"context"
"encoding/json"
"os"
"strings"
"testing"
"time"

"github.com/nextmv-io/nextroute"
"github.com/nextmv-io/nextroute/factory"
"github.com/nextmv-io/nextroute/schema"
"github.com/nextmv-io/sdk/run"
)

func BenchmarkGolden(b *testing.B) {
benchmarkFiles := []string{}
files, err := os.ReadDir("testdata")
if err != nil {
b.Fatal(err)
}
for _, file := range files {
if file.IsDir() {
continue
}
if strings.HasSuffix(file.Name(), ".json") {
benchmarkFiles = append(benchmarkFiles, "testdata/"+file.Name())
}
}
ctx := context.Background()
solveOptions := nextroute.ParallelSolveOptions{
Iterations: 50,
Duration: 10 * time.Second,
ParallelRuns: 1,
StartSolutions: 1,
RunDeterministically: true,
}
for _, file := range benchmarkFiles {
b.Run(file, func(b *testing.B) {
var input schema.Input
data, err := os.ReadFile(file)
if err != nil {
b.Fatal(err)
}
if err := json.Unmarshal(data, &input); err != nil {
b.Fatal(err)
}
model, err := factory.NewModel(input, factory.Options{})
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
solver, err := nextroute.NewParallelSolver(model)
if err != nil {
b.Fatal(err)
}
ctx = context.WithValue(ctx, run.Start, time.Now())
_, err = solver.Solve(ctx, solveOptions)
if err != nil {
b.Fatal(err)
}
}
})
}
}

0 comments on commit 069067f

Please sign in to comment.