-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark tests based on golden files
- Loading branch information
1 parent
018a994
commit 069067f
Showing
1 changed file
with
69 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 |
---|---|---|
@@ -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) | ||
} | ||
} | ||
}) | ||
} | ||
} |