554325746@qq.com
2019-06-22 7546564c06db3535f54babee714583f59dec6ccb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package shardmap
 
import (
    "github.com/goinbox/crypto"
    "github.com/goinbox/gomisc"
 
    "strconv"
    "sync"
    "testing"
)
 
var shardMap *ShardMap
var syncMap *sync.Map
var simpleMap map[string]int64
 
func init() {
    shardMap = New(32)
    syncMap = new(sync.Map)
    simpleMap = make(map[string]int64)
}
 
func TestSetGet(t *testing.T) {
    for i := 0; i < 10000; i++ {
        key := getIntMd5(i)
        shardMap.Set(key, i)
 
        v, ok := shardMap.Get(key)
        if !ok || v != i {
            t.Error(v, ok)
        }
    }
}
 
func TestWalkDel(t *testing.T) {
    shardMap.Walk(func(k string, v interface{}) {
        //t.Log(k, v)
 
        shardMap.Del(k)
 
        _, ok := shardMap.Get(k)
        if ok {
            t.Error(v, ok)
        }
    })
}
 
func BenchmarkShardMapWrite(b *testing.B) {
    b.RunParallel(func(pb *testing.PB) {
        for pb.Next() {
            ki := gomisc.RandByTime(nil)
            k := strconv.FormatInt(ki, 10)
            shardMap.Set(k, ki)
        }
    })
}
 
func getIntMd5(i int) string {
    return crypto.Md5String([]byte(strconv.Itoa(i)))
}
 
func BenchmarkSyncMapWrite(b *testing.B) {
    b.RunParallel(func(pb *testing.PB) {
        for pb.Next() {
            ki := gomisc.RandByTime(nil)
            k := strconv.FormatInt(ki, 10)
            syncMap.Store(k, ki)
        }
    })
}
 
func BenchmarkSimpleMapWrite(b *testing.B) {
    lock := new(sync.Mutex)
 
    b.RunParallel(func(pb *testing.PB) {
        for pb.Next() {
            ki := gomisc.RandByTime(nil)
            k := strconv.FormatInt(ki, 10)
            lock.Lock()
            simpleMap[k] = ki
            lock.Unlock()
        }
    })
}