github.com/better-concurrent/guc@v0.0.0-20190520022744-eb29266403a1/benchmark_test.go (about) 1 package guc 2 3 import ( 4 "fmt" 5 "runtime" 6 "sync" 7 "testing" 8 "time" 9 ) 10 11 type taskConfig struct { 12 readGCount int 13 writeGCount int 14 countPerG int 15 } 16 17 func runGuc(config taskConfig) { 18 cmap := NewConcurrentHashMap(64, 16) 19 endCh := make(chan int) 20 value0 := valueObject{v: "v"} 21 cmap.Store(keyObject2{i: -1}, value0) 22 for i := 0; i < config.writeGCount; i++ { 23 ii := i 24 go func() { 25 begin := config.countPerG 26 beginTime := time.Now() 27 for n := 0; n < begin+config.countPerG; n++ { 28 key := keyObject2{i: n} 29 cmap.Store(key, value0) 30 } 31 endTime := time.Now() 32 fmt.Printf("guc.map %d write cost is %d\n", ii, 33 endTime.Sub(beginTime).Nanoseconds()/1e6) 34 endCh <- 1 35 }() 36 } 37 for i := 0; i < config.readGCount; i++ { 38 ii := i 39 go func() { 40 begin := config.countPerG 41 beginTime := time.Now() 42 for n := 0; n < begin+config.countPerG; n++ { 43 key := keyObject2{i: n} 44 cmap.Load(key) 45 } 46 endTime := time.Now() 47 fmt.Printf("guc.map %d read cost is %d\n", ii, 48 endTime.Sub(beginTime).Nanoseconds()/1e6) 49 endCh <- 1 50 }() 51 } 52 total := config.writeGCount + config.readGCount 53 for i := 0; i < total; i++ { 54 <-endCh 55 } 56 } 57 58 func runSyncMap(config taskConfig) { 59 smap := new(sync.Map) 60 endCh := make(chan int) 61 value0 := valueObject{v: "v"} 62 for i := 0; i < config.writeGCount; i++ { 63 ii := i 64 go func() { 65 begin := config.countPerG 66 beginTime := time.Now() 67 for n := 0; n < begin+config.countPerG; n++ { 68 key := keyObject2{i: n} 69 smap.Store(key, value0) 70 } 71 endTime := time.Now() 72 fmt.Printf("sync.map %d write cost is %d\n", ii, 73 endTime.Sub(beginTime).Nanoseconds()/1e6) 74 endCh <- 1 75 }() 76 } 77 for i := 0; i < config.readGCount; i++ { 78 ii := i 79 go func() { 80 begin := config.countPerG 81 beginTime := time.Now() 82 for n := 0; n < begin+config.countPerG; n++ { 83 key := keyObject2{i: n} 84 smap.Load(key) 85 } 86 endTime := time.Now() 87 fmt.Printf("sync.map %d read cost is %d\n", ii, 88 endTime.Sub(beginTime).Nanoseconds()/1e6) 89 endCh <- 1 90 }() 91 } 92 total := config.writeGCount + config.readGCount 93 for i := 0; i < total; i++ { 94 <-endCh 95 } 96 } 97 98 func TestConcurrentHashMap(t *testing.T) { 99 runtime.GOMAXPROCS(4) 100 readGCount := 4 101 writeGCount := 4 102 countPerG := 1024 * 32 103 config := taskConfig{readGCount: readGCount, writeGCount: writeGCount, countPerG: countPerG} 104 runGuc(config) 105 runSyncMap(config) 106 }