github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/type_bench_test.go (about) 1 package testpb 2 3 import ( 4 "testing" 5 6 "google.golang.org/protobuf/reflect/protoreflect" 7 ) 8 9 func getFastType() (fastReflectionType protoreflect.MessageType) { 10 return (&A{}).ProtoReflect().Type() 11 } 12 13 func getSlowType() protoreflect.MessageType { 14 return (&A{}).slowProtoReflect().Type() 15 } 16 17 func Benchmark_MessageType_New_FR(b *testing.B) { 18 typ := getFastType() // if casted to concrete type, performance goes down to: Benchmark_MessageType_New_FR-12 1000000000 0.2431 ns/op 19 20 b.ResetTimer() 21 22 for i := 0; i < b.N; i++ { 23 _ = typ.New() 24 } 25 } 26 27 func Benchmark_MessageType_New_SR(b *testing.B) { 28 typ := getSlowType() 29 30 b.ResetTimer() 31 for i := 0; i < b.N; i++ { 32 _ = typ.New() 33 } 34 } 35 36 func Benchmark_MessageType_Zero_FR(b *testing.B) { 37 typ := getFastType() 38 39 b.ResetTimer() 40 41 for i := 0; i < b.N; i++ { 42 _ = typ.Zero() 43 } 44 } 45 46 func Benchmark_MessageType_Zero_SR(b *testing.B) { 47 typ := getSlowType() 48 49 b.ResetTimer() 50 for i := 0; i < b.N; i++ { 51 _ = typ.Zero() 52 } 53 } 54 55 func Benchmark_MessageType_Descriptor_FR(b *testing.B) { 56 typ := getFastType() 57 58 b.ResetTimer() 59 60 for i := 0; i < b.N; i++ { 61 _ = typ.Descriptor() 62 } 63 } 64 65 func Benchmark_MessageType_Descriptor_SR(b *testing.B) { 66 typ := getSlowType() 67 68 b.ResetTimer() 69 for i := 0; i < b.N; i++ { 70 _ = typ.Descriptor() 71 } 72 }