Skip to content

Commit 74031b0

Browse files
committed
fix: benchmark test
1 parent 0f2adfb commit 74031b0

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

mapkeyvalue_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,7 @@ func ExampleNewMapKeyValue_struct() {
18361836
func BenchmarkMapKeyValue_Set_int_int(b *testing.B) {
18371837
kv := NewMapKeyValue[int, int](WithCapacity(kvSize))
18381838

1839+
b.ResetTimer()
18391840
for i := 0; i < b.N; i++ {
18401841
kv.Set(rand.Intn(kvSize), rand.Intn(kvSize))
18411842
}
@@ -1850,6 +1851,7 @@ func BenchmarkMapKeyValue_Get_int_int(b *testing.B) {
18501851
func BenchmarkMapKeyValue_Set_Get_int_int(b *testing.B) {
18511852
kv := NewMapKeyValue[int, int](WithCapacity(kvSize))
18521853

1854+
b.ResetTimer()
18531855
for i := 0; i < b.N; i++ {
18541856
kv.Set(rand.Intn(kvSize), rand.Intn(kvSize))
18551857
}
@@ -1862,6 +1864,7 @@ func BenchmarkMapKeyValue_Set_Get_int_int(b *testing.B) {
18621864
func BenchmarkMapKeyValue_Set_string_string(b *testing.B) {
18631865
kv := NewMapKeyValue[string, string](WithCapacity(kvSize))
18641866

1867+
b.ResetTimer()
18651868
for i := 0; i < b.N; i++ {
18661869
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
18671870
kv.Set(keyval, keyval)
@@ -1878,6 +1881,7 @@ func BenchmarkMapKeyValue_Get_string_string(b *testing.B) {
18781881
func BenchmarkMapKeyValue_Set_Get_string_string(b *testing.B) {
18791882
kv := NewMapKeyValue[string, string](WithCapacity(kvSize))
18801883

1884+
b.ResetTimer()
18811885
for i := 0; i < b.N; i++ {
18821886
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
18831887
kv.Set(keyval, keyval)
@@ -1892,6 +1896,7 @@ func BenchmarkMapKeyValue_Set_Get_string_string(b *testing.B) {
18921896
func BenchmarkMapKeyValue_Set_string_struct(b *testing.B) {
18931897
kv := NewMapKeyValue[string, TestStruct](WithCapacity(kvSize))
18941898

1899+
b.ResetTimer()
18951900
for i := 0; i < b.N; i++ {
18961901
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
18971902
s := TestStruct{
@@ -1914,6 +1919,7 @@ func BenchmarkMapKeyValue_Get_string_struct(b *testing.B) {
19141919
func BenchmarkMapKeyValue_Set_Get_string_struct(b *testing.B) {
19151920
kv := NewMapKeyValue[string, TestStruct](WithCapacity(kvSize))
19161921

1922+
b.ResetTimer()
19171923
for i := 0; i < b.N; i++ {
19181924
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
19191925
s := TestStruct{
@@ -1936,9 +1942,9 @@ func BenchmarkMapKeyValue_Set_Get_string_struct_concurrent(b *testing.B) {
19361942

19371943
var wg sync.WaitGroup
19381944

1939-
wg.Add(1)
1940-
go func() {
1941-
for i := 0; i < b.N; i++ {
1945+
for i := 0; i < b.N; i++ {
1946+
wg.Add(1)
1947+
go func() {
19421948
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
19431949
s := TestStruct{
19441950
a: keyval,
@@ -1947,19 +1953,19 @@ func BenchmarkMapKeyValue_Set_Get_string_struct_concurrent(b *testing.B) {
19471953
d: rand.Float64(),
19481954
}
19491955
kv.Set(keyval, s)
1950-
}
1951-
}()
1956+
}()
1957+
wg.Done()
1958+
}
19521959

1953-
wg.Add(1)
1954-
go func() {
1955-
for i := 0; i < b.N; i++ {
1956-
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
1957-
kv.Get(keyval)
1958-
}
1959-
}()
1960+
for i := 0; i < b.N; i++ {
1961+
wg.Add(1)
19601962

1961-
wg.Done()
1962-
wg.Done()
1963+
keyval := fmt.Sprintf("%x", md5.Sum([]byte(strconv.Itoa(rand.Intn(kvSize)))))
1964+
go func() {
1965+
kv.Get(keyval)
1966+
}()
1967+
wg.Done()
1968+
}
19631969

19641970
wg.Wait()
19651971
}

0 commit comments

Comments
 (0)