...

Source file src/github.com/ory/x/configx/koanf_test.go

Documentation: github.com/ory/x/configx

     1  package configx
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/spf13/pflag"
    11  
    12  	"github.com/dgraph-io/ristretto"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func newKoanf(schemaPath string, configPaths []string, modifiers ...OptionModifier) (*Provider, error) {
    17  	schema, err := ioutil.ReadFile(schemaPath)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	f := pflag.NewFlagSet("config", pflag.ContinueOnError)
    23  	f.StringSliceP("config", "c", configPaths, "")
    24  
    25  	modifiers = append(modifiers, WithFlags(f))
    26  	k, err := New(schema, modifiers...)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	return k, nil
    32  }
    33  
    34  func setEnvs(t testing.TB, envs [][2]string) {
    35  	for _, v := range envs {
    36  		require.NoError(t, os.Setenv(v[0], v[1]))
    37  	}
    38  	t.Cleanup(func() {
    39  		for _, v := range envs {
    40  			_ = os.Unsetenv(v[0])
    41  		}
    42  	})
    43  }
    44  
    45  func BenchmarkKoanf(b *testing.B) {
    46  	setEnvs(b, [][2]string{{"MUTATORS_HEADER_ENABLED", "true"}})
    47  	schemaPath := path.Join("stub/benchmark/schema.config.json")
    48  	k, err := newKoanf(schemaPath, []string{"stub/benchmark/benchmark.yaml"})
    49  	require.NoError(b, err)
    50  
    51  	keys := k.Koanf.Keys()
    52  	numKeys := len(keys)
    53  
    54  	b.Run("cache=false", func(b *testing.B) {
    55  		var key string
    56  
    57  		b.ResetTimer()
    58  		for i := 0; i < b.N; i++ {
    59  			key = keys[i%numKeys]
    60  
    61  			if k.Koanf.Get(key) == nil {
    62  				b.Fatalf("cachedFind returned a nil value for key: %s", key)
    63  			}
    64  		}
    65  	})
    66  
    67  	b.Run("cache=true", func(b *testing.B) {
    68  		for i, c := range []*ristretto.Config{
    69  			{
    70  				NumCounters: int64(numKeys),
    71  				MaxCost:     500000,
    72  				BufferItems: 64,
    73  			},
    74  			{
    75  				NumCounters: int64(numKeys * 10),
    76  				MaxCost:     1000000,
    77  				BufferItems: 64,
    78  			},
    79  			{
    80  				NumCounters: int64(numKeys * 10),
    81  				MaxCost:     5000000,
    82  				BufferItems: 64,
    83  			},
    84  		} {
    85  			cache, err := ristretto.NewCache(c)
    86  			require.NoError(b, err)
    87  
    88  			b.Run(fmt.Sprintf("config=%d", i), func(b *testing.B) {
    89  				var key string
    90  				var found bool
    91  				var val interface{}
    92  
    93  				b.ResetTimer()
    94  				for i := 0; i < b.N; i++ {
    95  					key = keys[i%numKeys]
    96  
    97  					if val, found = cache.Get(key); !found {
    98  						val = k.Koanf.Get(key)
    99  						_ = cache.Set(key, val, 0)
   100  					}
   101  
   102  					if val == nil {
   103  						b.Fatalf("cachedFind returned a nil value for key: %s", key)
   104  					}
   105  				}
   106  			})
   107  		}
   108  	})
   109  }
   110  

View as plain text