github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/omap/osmap/osmap_benchm_test.go (about)

     1  package osmap
     2  
     3  // go test -bench=. -benchtime=6ms
     4  
     5  import (
     6  	"math/rand"
     7  	"testing"
     8  )
     9  
    10  var keys2 []string
    11  
    12  func init() {
    13  
    14  	rand.Seed(12345)
    15  	keys2 = make([]string, 50*1000*1000)
    16  
    17  	for i := 0; i < len(keys2); i++ {
    18  		sz := 4 + rand.Intn(5)
    19  		b := make([]byte, sz)
    20  		for j := 0; j < sz; j++ {
    21  			b[j] = byte(97 + rand.Intn(24))
    22  		}
    23  		keys2[i] = string(b)
    24  	}
    25  }
    26  
    27  type Map2 map[string]string
    28  
    29  func (m Map2) Set(key string, value string) {
    30  	m[key] = value
    31  }
    32  func (m Map2) Get(key string) string {
    33  	return m[key]
    34  }
    35  
    36  func BenchmarkMapSet(b *testing.B) {
    37  	m := make(Map2)
    38  	for i := 0; i < b.N; i++ {
    39  		offs := i % len(keys2)
    40  		m.Set(keys2[offs], keys2[offs])
    41  	}
    42  }
    43  
    44  func BenchmarkOSMSet(b *testing.B) {
    45  	t := New()
    46  	for i := 0; i < b.N; i++ {
    47  		offs := i % len(keys2)
    48  		t.Set(keys2[offs], keys2[offs])
    49  	}
    50  }
    51  
    52  func BenchmarkMapGet(b *testing.B) {
    53  	m := make(Map2)
    54  	for i := 0; i < b.N; i++ {
    55  		offs := i % len(keys2)
    56  		m.Set(keys2[offs], keys2[offs])
    57  	}
    58  	b.ResetTimer()
    59  	for i := 0; i < b.N; i++ {
    60  		m.Get(keys2[i%len(keys2)])
    61  	}
    62  }
    63  
    64  func BenchmarkOSMGet(b *testing.B) {
    65  	t := New()
    66  	for i := 0; i < b.N; i++ {
    67  		offs := i % len(keys2)
    68  		t.Set(keys2[offs], keys2[offs])
    69  	}
    70  	b.ResetTimer()
    71  	for i := 0; i < b.N; i++ {
    72  		t.Get(keys2[i%len(keys2)])
    73  	}
    74  }