github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/config/config_bench_test.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"runtime"
     6  	"testing"
     7  )
     8  
     9  func setupConfigBench(b *testing.B) {
    10  	Clear()
    11  	if err := LoadFrom("config.test.json"); err != nil {
    12  		panic(err)
    13  	}
    14  	runtime.GC()
    15  	b.ReportAllocs()
    16  	b.ResetTimer()
    17  }
    18  
    19  func BenchmarkValidateInt(b *testing.B) {
    20  	entry := &Entry{1.0, reflect.Int, []interface{}{}}
    21  	config := object{"number": entry}
    22  	b.ReportAllocs()
    23  	b.ResetTimer()
    24  	for n := 0; n < b.N; n++ {
    25  		config.validate("")
    26  		entry.Value = 1.0
    27  	}
    28  }
    29  
    30  func BenchmarkSetString(b *testing.B) {
    31  	setupConfigBench(b)
    32  	for n := 0; n < b.N; n++ {
    33  		Set("app.name", "my awesome app")
    34  	}
    35  }
    36  
    37  func BenchmarkGet(b *testing.B) {
    38  	setupConfigBench(b)
    39  	for n := 0; n < b.N; n++ {
    40  		Get("app.name")
    41  	}
    42  }
    43  
    44  func BenchmarkSetInt(b *testing.B) {
    45  	setupConfigBench(b)
    46  	for n := 0; n < b.N; n++ {
    47  		Set("server.port", 8080)
    48  	}
    49  }