github.com/ipld/go-ipld-prime@v0.21.0/node/tests/mapBenchmarks_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/ipld/go-ipld-prime/must"
     8  )
     9  
    10  // This is analogous to the 'MapStrInt_3n' suite of benchmarks,
    11  // but against a golang native map in regular go code,
    12  // for getting a baseline impression to compare other things against.
    13  func BenchmarkMapStrInt_3n_BaselineNativeMapAssignSimpleKeys(b *testing.B) {
    14  	for i := 0; i < b.N; i++ {
    15  		x := make(map[string]int, 3)
    16  		x["whee"] = 1
    17  		x["woot"] = 2
    18  		x["waga"] = 3
    19  		sink = x
    20  	}
    21  }
    22  
    23  func BenchmarkMapStrInt_3n_BaselineJsonUnmarshalMapSimpleKeys(b *testing.B) {
    24  	for i := 0; i < b.N; i++ {
    25  		x := make(map[string]int, 3)
    26  		must.NotError(json.Unmarshal([]byte(`{"whee":1,"woot":2,"waga":3}`), &x))
    27  		sink = x
    28  	}
    29  }
    30  
    31  func BenchmarkMapStrInt_3n_BaselineJsonMarshalMapSimpleKeys(b *testing.B) {
    32  	x := map[string]int{"whee": 1, "woot": 2, "waga": 3}
    33  	for i := 0; i < b.N; i++ {
    34  		bs, err := json.Marshal(x)
    35  		must.NotError(err)
    36  		sink = bs
    37  	}
    38  }
    39  
    40  var (
    41  	sink_s string
    42  	sink_i int64
    43  )
    44  
    45  func BenchmarkMapStrInt_3n_BaselineNativeMapIterationSimpleKeys(b *testing.B) {
    46  	x := make(map[string]int64, 3)
    47  	x["whee"] = 1
    48  	x["woot"] = 2
    49  	x["waga"] = 3
    50  	sink = x
    51  	b.ResetTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		for k, v := range x {
    54  			sink_s = k
    55  			sink_i = v
    56  		}
    57  	}
    58  }
    59  
    60  // n25 -->
    61  
    62  func BenchmarkMapStrInt_25n_BaselineNativeMapAssignSimpleKeys(b *testing.B) {
    63  	for i := 0; i < b.N; i++ {
    64  		x := make(map[string]int64, 25)
    65  		for i := 1; i <= 25; i++ {
    66  			x[tableStrInt[i-1].s] = tableStrInt[i-1].i
    67  		}
    68  		sink = x
    69  	}
    70  }
    71  
    72  func BenchmarkMapStrInt_25n_BaselineNativeMapIterationSimpleKeys(b *testing.B) {
    73  	x := make(map[string]int64, 25)
    74  	for i := 1; i <= 25; i++ {
    75  		x[tableStrInt[i-1].s] = tableStrInt[i-1].i
    76  	}
    77  	sink = x
    78  	b.ResetTimer()
    79  	for i := 0; i < b.N; i++ {
    80  		for k, v := range x {
    81  			sink_s = k
    82  			sink_i = v
    83  		}
    84  	}
    85  }