github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/omap/osmaps/osmaps_benchm_test.go (about) 1 // +build benchm 2 // go test -tags=benchm -bench=. -benchtime=6ms 3 4 package osmaps 5 6 import ( 7 "fmt" 8 "math/rand" 9 "testing" 10 ) 11 12 var keys2 []string 13 14 const cval = "v" 15 16 func init() { 17 18 rand.Seed(12345 + 2) 19 keys2 = make([]string, 2*1000*1000) 20 keys2 = make([]string, 22*1000) 21 22 for i := 0; i < len(keys2); i++ { 23 sz := 5 + rand.Intn(10) 24 b := make([]byte, sz) 25 for j := 0; j < sz; j++ { 26 b[j] = byte(97 + rand.Intn(24)) 27 } 28 keys2[i] = string(b) 29 } 30 } 31 32 type Map2 map[string]string 33 34 func (m Map2) Set(key string, value string) { 35 m[key] = value 36 } 37 func (m Map2) Get(key string) string { 38 return m[key] 39 } 40 41 func DISABLED_BenchmarkMapSet(b *testing.B) { 42 m := make(Map2) 43 for i := 0; i < b.N; i++ { 44 offs := i % len(keys2) 45 m.Set(keys2[offs], cval) 46 } 47 } 48 49 func BenchmarkOSMSet(b *testing.B) { 50 t := New() 51 b.ResetTimer() 52 for i := 0; i < b.N; i++ { 53 offs := i % len(keys2) 54 t.Set(keys2[offs], cval) 55 } 56 57 // 58 fmt.Printf("\n\n Histo:\n") 59 sum := 0 60 popul := 0 61 alreadyDone := make(map[*node]bool, 0) 62 for i := len(ndStat) - 1; i >= 0; i-- { 63 mpNodes := ndStat[i] 64 if len(mpNodes) == 0 { 65 continue 66 } 67 fmt.Printf("%2v: %2v\t", i, len(mpNodes)) 68 for nd1, _ := range mpNodes { 69 if alreadyDone[nd1] { 70 // fmt.Printf("skipp ") 71 continue 72 } 73 sum++ 74 fmt.Printf("%2v ", len(nd1.kv)) 75 popul += len(nd1.kv) 76 alreadyDone[nd1] = true 77 } 78 fmt.Printf("\n") 79 } 80 fmt.Printf("%2v nodes total - %v\n", sum, popul) 81 82 } 83 84 func DISABLED_BenchmarkMapGet(b *testing.B) { 85 m := make(Map2) 86 for i := 0; i < b.N; i++ { 87 offs := i % len(keys2) 88 m.Set(keys2[offs], keys2[offs]) 89 } 90 b.ResetTimer() 91 for i := 0; i < b.N; i++ { 92 m.Get(keys2[i%len(keys2)]) 93 } 94 } 95 96 func DISABLED_BenchmarkOSMGet(b *testing.B) { 97 t := New() 98 for i := 0; i < b.N; i++ { 99 offs := i % len(keys2) 100 t.Set(keys2[offs], keys2[offs]) 101 } 102 b.ResetTimer() 103 for i := 0; i < b.N; i++ { 104 t.Get(keys2[i%len(keys2)]) 105 } 106 }