github.com/cosmos/cosmos-proto@v1.0.0-beta.3/testpb/map_bench_test.go (about)

     1  package testpb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"google.golang.org/protobuf/reflect/protoreflect"
     7  )
     8  
     9  func newFastMap() protoreflect.Map {
    10  	msg := &A{MAP: map[string]*B{
    11  		"1": &B{X: "a"},
    12  	}}
    13  
    14  	return msg.ProtoReflect().Get(msg.ProtoReflect().Descriptor().Fields().ByName("MAP")).Map()
    15  }
    16  
    17  func newSlowMap() protoreflect.Map {
    18  	msg := &A{MAP: map[string]*B{
    19  		"1": &B{X: "a"},
    20  	}}
    21  
    22  	return msg.slowProtoReflect().Get(msg.ProtoReflect().Descriptor().Fields().ByName("MAP")).Map()
    23  }
    24  
    25  func Benchmark_Map_Get_FR(b *testing.B) {
    26  	m := newFastMap()
    27  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("1"))
    28  
    29  	b.ResetTimer()
    30  
    31  	for i := 0; i < b.N; i++ {
    32  		_ = m.Get(key)
    33  	}
    34  }
    35  
    36  func Benchmark_Map_Get_SR(b *testing.B) {
    37  	m := newSlowMap()
    38  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("1"))
    39  
    40  	b.ResetTimer()
    41  
    42  	for i := 0; i < b.N; i++ {
    43  		_ = m.Get(key)
    44  	}
    45  }
    46  
    47  func Benchmark_Map_Has_FR(b *testing.B) {
    48  	m := newFastMap()
    49  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("1"))
    50  
    51  	b.ResetTimer()
    52  
    53  	for i := 0; i < b.N; i++ {
    54  		_ = m.Has(key)
    55  	}
    56  }
    57  
    58  func Benchmark_Map_Has_SR(b *testing.B) {
    59  	m := newSlowMap()
    60  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("1"))
    61  
    62  	b.ResetTimer()
    63  
    64  	for i := 0; i < b.N; i++ {
    65  		_ = m.Has(key)
    66  	}
    67  }
    68  
    69  func Benchmark_Map_Clear_FR(b *testing.B) {
    70  	m := newFastMap()
    71  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("1"))
    72  
    73  	b.ResetTimer()
    74  
    75  	for i := 0; i < b.N; i++ {
    76  		m.Clear(key)
    77  	}
    78  }
    79  
    80  func Benchmark_Map_Clear_SR(b *testing.B) {
    81  	m := newSlowMap()
    82  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("1"))
    83  
    84  	b.ResetTimer()
    85  
    86  	for i := 0; i < b.N; i++ {
    87  		m.Clear(key)
    88  	}
    89  }
    90  
    91  func Benchmark_Map_Set_FR(b *testing.B) {
    92  	m := newFastMap()
    93  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("2"))
    94  	value := protoreflect.ValueOfMessage((&B{X: "b"}).ProtoReflect())
    95  	b.ResetTimer()
    96  
    97  	for i := 0; i < b.N; i++ {
    98  		m.Set(key, value)
    99  	}
   100  }
   101  
   102  func Benchmark_Map_Set_SR(b *testing.B) {
   103  	m := newSlowMap()
   104  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("2"))
   105  	value := protoreflect.ValueOfMessage((&B{X: "b"}).ProtoReflect())
   106  
   107  	b.ResetTimer()
   108  
   109  	for i := 0; i < b.N; i++ {
   110  		m.Set(key, value)
   111  	}
   112  }
   113  
   114  func Benchmark_Map_Mutable_FR(b *testing.B) {
   115  	m := newFastMap()
   116  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("2"))
   117  	b.ResetTimer()
   118  
   119  	for i := 0; i < b.N; i++ {
   120  		m.Mutable(key)
   121  	}
   122  }
   123  
   124  func Benchmark_Map_Mutable_SR(b *testing.B) {
   125  	m := newSlowMap()
   126  	key := (protoreflect.MapKey)(protoreflect.ValueOfString("2"))
   127  
   128  	b.ResetTimer()
   129  
   130  	for i := 0; i < b.N; i++ {
   131  		m.Mutable(key)
   132  	}
   133  }
   134  
   135  func Benchmark_Map_NewValue_FR(b *testing.B) {
   136  	m := newFastMap()
   137  	b.ResetTimer()
   138  
   139  	for i := 0; i < b.N; i++ {
   140  		_ = m.NewValue()
   141  	}
   142  }
   143  
   144  func Benchmark_Map_NewValue_SR(b *testing.B) {
   145  	m := newSlowMap()
   146  
   147  	b.ResetTimer()
   148  
   149  	for i := 0; i < b.N; i++ {
   150  		_ = m.NewValue()
   151  	}
   152  }
   153  
   154  func Benchmark_Map_Len_FR(b *testing.B) {
   155  	m := newFastMap()
   156  	b.ResetTimer()
   157  
   158  	for i := 0; i < b.N; i++ {
   159  		_ = m.Len()
   160  	}
   161  }
   162  
   163  func Benchmark_Map_Len_SR(b *testing.B) {
   164  	m := newSlowMap()
   165  
   166  	b.ResetTimer()
   167  
   168  	for i := 0; i < b.N; i++ {
   169  		_ = m.Len()
   170  	}
   171  }
   172  
   173  func Benchmark_Map_Range_FR(b *testing.B) {
   174  	msg := &A{MAP: map[string]*B{
   175  		"1":  {X: "a"},
   176  		"2":  {X: "b"},
   177  		"3":  {X: "c"},
   178  		"4":  {X: "d"},
   179  		"5":  {X: "e"},
   180  		"6":  {X: "f"},
   181  		"7":  {X: "g"},
   182  		"8":  {X: "h"},
   183  		"9":  {X: "i"},
   184  		"10": {X: "j"},
   185  	}}
   186  
   187  	m := msg.ProtoReflect().Get(msg.ProtoReflect().Descriptor().Fields().ByName("MAP")).Map()
   188  	for i := 0; i < b.N; i++ {
   189  		m.Range(func(_ protoreflect.MapKey, _ protoreflect.Value) bool {
   190  			return true
   191  		})
   192  	}
   193  }
   194  
   195  func Benchmark_Map_Range_SR(b *testing.B) {
   196  	msg := &A{MAP: map[string]*B{
   197  		"1":  {X: "a"},
   198  		"2":  {X: "b"},
   199  		"3":  {X: "c"},
   200  		"4":  {X: "d"},
   201  		"5":  {X: "e"},
   202  		"6":  {X: "f"},
   203  		"7":  {X: "g"},
   204  		"8":  {X: "h"},
   205  		"9":  {X: "i"},
   206  		"10": {X: "j"},
   207  	}}
   208  
   209  	m := msg.slowProtoReflect().Get(msg.ProtoReflect().Descriptor().Fields().ByName("MAP")).Map()
   210  
   211  	for i := 0; i < b.N; i++ {
   212  		m.Range(func(_ protoreflect.MapKey, _ protoreflect.Value) bool {
   213  			return true
   214  		})
   215  	}
   216  }