Skip to content

Commit

Permalink
cue/benchmarks: move benchmark code out of testdata
Browse files Browse the repository at this point in the history
bench_test.go used to reside in cue/testdata/benchmarks

This meant that a `go test bench=. ./...` would not run these
benchmarks, because go test never delves inside testdata directories.

- Move bench_test to the cue directory (with package cue_test)
- Make it use filepath.WalkDir (matching cuetxtar)
- Make it use the eval matrices so that the benchmarks get run against
  both the old and new evaluators

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: I85fb895579ad38039eb47176eeb48c1aaceda9ea
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199739
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
cuematthew committed Aug 21, 2024
1 parent 1b01c83 commit d52859e
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions cue/testdata/benchmarks/bench_test.go → cue/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,48 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package benchmarks
package cue_test

import (
"io/fs"
"os"
"path/filepath"
"testing"

"cuelang.org/go/cue"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cuetdtest"
"cuelang.org/go/internal/cuetest"
"cuelang.org/go/internal/cuetxtar"
"golang.org/x/tools/txtar"
)

var (
matrix = cuetdtest.FullMatrix
)

func Benchmark(b *testing.B) {
entries, err := os.ReadDir(".")
if err != nil {
b.Fatal(err)
}
root := "testdata/benchmarks"
err := filepath.WalkDir(root, func(fullpath string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}

for _, entry := range entries {
name := entry.Name()
if entry.IsDir() || filepath.Ext(name) != ".txtar" {
continue
if entry.IsDir() || filepath.Ext(fullpath) != ".txtar" {
return nil
}

a, err := txtar.ParseFile(name)
a, err := txtar.ParseFile(fullpath)
if err != nil {
b.Fatal(err)
return err
}

inst := cuetxtar.Load(a, b.TempDir())[0]
if inst.Err != nil {
b.Fatal(inst.Err)
return inst.Err
}

r := runtime.New()

v, err := r.Build(nil, inst)
if err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -83,19 +86,24 @@ func Benchmark(b *testing.B) {
if err != nil {
b.Fatal(err)
}
os.WriteFile(name, txtar.Format(a), info.Mode())
os.WriteFile(fullpath, txtar.Format(a), info.Mode())
}

b.Run(name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
inst := cue.Build(cuetxtar.Load(a, b.TempDir()))[0]
if inst.Err != nil {
b.Fatal(inst.Err)
}

inst.Value().Validate()
b.Run(entry.Name(), func(b *testing.B) {
for _, m := range matrix {
b.Run(m.Name(), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ctx := m.Context()
value := ctx.BuildInstance(cuetxtar.Load(a, b.TempDir())[0])
value.Validate()
}
})
}
})
return nil
})
if err != nil {
b.Fatal(err)
}
}

0 comments on commit d52859e

Please sign in to comment.