...

Source file src/cuelang.org/go/cue/testdata/benchmarks/bench_test.go

Documentation: cuelang.org/go/cue/testdata/benchmarks

     1  // Copyright 2020 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package benchmarks
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/internal/core/eval"
    24  	"cuelang.org/go/internal/core/runtime"
    25  	"cuelang.org/go/internal/cuetest"
    26  	"cuelang.org/go/internal/cuetxtar"
    27  	"golang.org/x/tools/txtar"
    28  )
    29  
    30  func Benchmark(b *testing.B) {
    31  	entries, err := os.ReadDir(".")
    32  	if err != nil {
    33  		b.Fatal(err)
    34  	}
    35  
    36  	for _, entry := range entries {
    37  		name := entry.Name()
    38  		if entry.IsDir() || filepath.Ext(name) != ".txtar" {
    39  			continue
    40  		}
    41  
    42  		a, err := txtar.ParseFile(name)
    43  		if err != nil {
    44  			b.Fatal(err)
    45  		}
    46  
    47  		inst := cuetxtar.Load(a, b.TempDir())[0]
    48  		if inst.Err != nil {
    49  			b.Fatal(inst.Err)
    50  		}
    51  
    52  		r := runtime.New()
    53  
    54  		v, err := r.Build(nil, inst)
    55  		if err != nil {
    56  			b.Fatal(err)
    57  		}
    58  		e := eval.New(r)
    59  		ctx := e.NewContext(v)
    60  		v.Finalize(ctx)
    61  
    62  		if cuetest.UpdateGoldenFiles {
    63  			const statsFile = "stats.txt"
    64  			var stats txtar.File
    65  			var statsPos int
    66  			for i, f := range a.Files {
    67  				if f.Name == statsFile {
    68  					stats = f
    69  					statsPos = i
    70  					break
    71  				}
    72  			}
    73  			if stats.Name == "" {
    74  				// At stats.txt as the first file.
    75  				a.Files = append([]txtar.File{{
    76  					Name: statsFile,
    77  				}}, a.Files...)
    78  			}
    79  
    80  			a.Files[statsPos].Data = []byte(ctx.Stats().String() + "\n\n")
    81  
    82  			info, err := entry.Info()
    83  			if err != nil {
    84  				b.Fatal(err)
    85  			}
    86  			os.WriteFile(name, txtar.Format(a), info.Mode())
    87  		}
    88  
    89  		b.Run(name, func(b *testing.B) {
    90  			b.ReportAllocs()
    91  			for i := 0; i < b.N; i++ {
    92  				inst := cue.Build(cuetxtar.Load(a, b.TempDir()))[0]
    93  				if inst.Err != nil {
    94  					b.Fatal(inst.Err)
    95  				}
    96  
    97  				inst.Value().Validate()
    98  			}
    99  		})
   100  	}
   101  }
   102  

View as plain text