From 069067f76dd2fd0a5ebed6cf15c94a6bb10131d0 Mon Sep 17 00:00:00 2001 From: Dirk Schumacher Date: Mon, 13 May 2024 12:29:41 +0200 Subject: [PATCH] Add benchmark tests based on golden files --- tests/golden/benchmark_test.go | 69 ++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/golden/benchmark_test.go diff --git a/tests/golden/benchmark_test.go b/tests/golden/benchmark_test.go new file mode 100644 index 0000000..b3e069d --- /dev/null +++ b/tests/golden/benchmark_test.go @@ -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) + } + } + }) + } +}