1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package bexpr
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func benchRegistry(b *testing.B, registry Registry) {
rtype := derefType(reflect.TypeOf((*testNestedTypes)(nil)))
b.ResetTimer()
for n := 0; n < b.N; n++ {
registry.GetFieldConfigurations(rtype)
}
}
// Used as a bit of a baseline of what using a registry buys us
func BenchmarkNilRegistry(b *testing.B) {
benchRegistry(b, NilRegistry)
}
// Used to show the speedup for repeated usage of the same type
// Additionally this ensures that we are not regenerating fields
// because if we were we wouldn't see the speedup over the nil
// registry
func BenchmarkRegistry(b *testing.B) {
benchRegistry(b, NewSyncRegistry())
}
// This test is mostly to ensure that the SyncRegistry will return
// the same FieldConfigurations as calling GenerateFieldConfigurations
// or the internal generateFieldConfigurations. Then it ensures
// that the fields are stored in the internal map
func TestSyncRegistry(t *testing.T) {
var ttype *testNestedTypes
direct, err := GenerateFieldConfigurations(ttype)
require.NoError(t, err)
registry := NewSyncRegistry()
rtype := derefType(reflect.TypeOf(ttype))
fromRegistry, err := registry.GetFieldConfigurations(rtype)
require.NoError(t, err)
validateFieldConfigurations(t, direct, fromRegistry)
fields, ok := registry.configurations[rtype]
require.True(t, ok)
validateFieldConfigurations(t, fromRegistry, fields)
}
// This just tests that the NilRegistry is a true pass through
// to the GenerateFieldConfigurations function
func TestNilRegistry(t *testing.T) {
var ttype *testNestedTypes
direct, err := GenerateFieldConfigurations(ttype)
require.NoError(t, err)
rtype := derefType(reflect.TypeOf(ttype))
fromRegistry, err := NilRegistry.GetFieldConfigurations(rtype)
require.NoError(t, err)
validateFieldConfigurations(t, direct, fromRegistry)
}
|