Skip to content
This repository was archived by the owner on Nov 22, 2025. It is now read-only.

Commit c8bc7aa

Browse files
bibengabibenga
authored andcommitted
update benchmark tests
1 parent 2e7e103 commit c8bc7aa

2 files changed

Lines changed: 53 additions & 19 deletions

File tree

tests/benchmark1_test.go

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,49 @@ import (
99
"testing"
1010
)
1111

12-
// go test ./tests/ -bench=Benchmark -benchtime=5s
12+
// go test -benchmem ./tests/ -bench=Benchmark1
1313

1414
func Benchmark1IntLinkedHashMap(b *testing.B) {
1515
factory := func() orderedmap.Map[int, int] {
1616
return intlinkedhashmap.NewDefault[int, int]()
1717
}
18-
b.Run("Get", func(b *testing.B) { benchmarkMapGet(b, factory) })
19-
b.Run("Modify", func(b *testing.B) { benchmarkMapModify(b, factory) })
20-
b.Run("Put", func(b *testing.B) { benchmarkMapPut(b, factory) })
18+
b.Run("Get", func(b *testing.B) { benchmark1MapGet(b, factory) })
19+
b.Run("Modify", func(b *testing.B) { benchmark1MapModify(b, factory) })
20+
b.Run("Put", func(b *testing.B) { benchmark1MapPut(b, factory) })
21+
b.Run("Iterate", func(b *testing.B) { benchmark1MapIterate(b, factory) })
2122
}
2223

2324
func Benchmark1LinkedHashMap(b *testing.B) {
2425
factory := func() orderedmap.Map[int, int] {
2526
return linkedhashmap.NewDefault[int, int](hash.IntHash32)
2627
}
27-
b.Run("Get", func(b *testing.B) { benchmarkMapGet(b, factory) })
28-
b.Run("Modify", func(b *testing.B) { benchmarkMapModify(b, factory) })
29-
b.Run("Put", func(b *testing.B) { benchmarkMapPut(b, factory) })
28+
b.Run("Get", func(b *testing.B) { benchmark1MapGet(b, factory) })
29+
b.Run("Modify", func(b *testing.B) { benchmark1MapModify(b, factory) })
30+
b.Run("Put", func(b *testing.B) { benchmark1MapPut(b, factory) })
31+
b.Run("Iterate", func(b *testing.B) { benchmark1MapIterate(b, factory) })
3032
}
3133

3234
func Benchmark1LinkedMap(b *testing.B) {
3335
factory := func() orderedmap.Map[int, int] {
3436
return linkedmap.NewDefault[int, int]()
3537
}
36-
b.Run("Get", func(b *testing.B) { benchmarkMapGet(b, factory) })
37-
b.Run("Modify", func(b *testing.B) { benchmarkMapModify(b, factory) })
38-
b.Run("Put", func(b *testing.B) { benchmarkMapPut(b, factory) })
38+
b.Run("Get", func(b *testing.B) { benchmark1MapGet(b, factory) })
39+
b.Run("Modify", func(b *testing.B) { benchmark1MapModify(b, factory) })
40+
b.Run("Put", func(b *testing.B) { benchmark1MapPut(b, factory) })
41+
b.Run("Iterate", func(b *testing.B) { benchmark1MapIterate(b, factory) })
3942
}
4043

4144
func Benchmark1StdMap(b *testing.B) {
42-
b.Run("Get", func(b *testing.B) { benchmarkStdMapGet(b) })
43-
b.Run("Modify", func(b *testing.B) { benchmarkStdMapModify(b) })
44-
b.Run("Put", func(b *testing.B) { benchmarkStdMapPut(b) })
45+
b.Run("Get", func(b *testing.B) { benchmark1StdMapGet(b) })
46+
b.Run("Modify", func(b *testing.B) { benchmark1StdMapModify(b) })
47+
b.Run("Put", func(b *testing.B) { benchmark1StdMapPut(b) })
48+
b.Run("Iterate", func(b *testing.B) { benchmark1StdMapIterate(b) })
4549
}
4650

4751
const mapSize = 256
4852
const iterations = 100000
4953

50-
func benchmarkMapGet(b *testing.B, factory Factory) {
54+
func benchmark1MapGet(b *testing.B, factory Factory) {
5155
m := factory()
5256
for i := range mapSize {
5357
m.Put(i, i)
@@ -62,7 +66,7 @@ func benchmarkMapGet(b *testing.B, factory Factory) {
6266
}
6367
}
6468

65-
func benchmarkStdMapGet(b *testing.B) {
69+
func benchmark1StdMapGet(b *testing.B) {
6670
m := make(map[int]int, mapSize)
6771
for i := range mapSize {
6872
m[i] = i
@@ -77,7 +81,7 @@ func benchmarkStdMapGet(b *testing.B) {
7781
}
7882
}
7983

80-
func benchmarkMapModify(b *testing.B, factory Factory) {
84+
func benchmark1MapModify(b *testing.B, factory Factory) {
8185
m := factory()
8286
sum := 0
8387
for b.Loop() {
@@ -93,7 +97,7 @@ func benchmarkMapModify(b *testing.B, factory Factory) {
9397
}
9498
}
9599

96-
func benchmarkStdMapModify(b *testing.B) {
100+
func benchmark1StdMapModify(b *testing.B) {
97101
m := make(map[int]int, mapSize)
98102
sum := 0
99103
for b.Loop() {
@@ -109,7 +113,7 @@ func benchmarkStdMapModify(b *testing.B) {
109113
}
110114
}
111115

112-
func benchmarkMapPut(b *testing.B, factory Factory) {
116+
func benchmark1MapPut(b *testing.B, factory Factory) {
113117
m := factory()
114118
for b.Loop() {
115119
m.Clear()
@@ -119,7 +123,7 @@ func benchmarkMapPut(b *testing.B, factory Factory) {
119123
}
120124
}
121125

122-
func benchmarkStdMapPut(b *testing.B) {
126+
func benchmark1StdMapPut(b *testing.B) {
123127
m := make(map[int]int, mapSize)
124128
for b.Loop() {
125129
clear(m)
@@ -128,3 +132,31 @@ func benchmarkStdMapPut(b *testing.B) {
128132
}
129133
}
130134
}
135+
136+
func benchmark1MapIterate(b *testing.B, factory Factory) {
137+
m := factory()
138+
for i := range mapSize {
139+
m.Put(i, i)
140+
}
141+
b.ResetTimer()
142+
sum := 0
143+
for b.Loop() {
144+
for _, val := range m.Items() {
145+
sum += val
146+
}
147+
}
148+
}
149+
150+
func benchmark1StdMapIterate(b *testing.B) {
151+
m := make(map[int]int, mapSize)
152+
for i := range mapSize {
153+
m[i] = i
154+
}
155+
b.ResetTimer()
156+
sum := 0
157+
for b.Loop() {
158+
for _, val := range m {
159+
sum += val
160+
}
161+
}
162+
}

tests/benchmark2_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"testing"
1111
)
1212

13+
// go test -benchmem ./tests/ -bench=Benchmark2
14+
1315
func Benchmark2IntLinkedHashMap(b *testing.B) {
1416
factory := func() orderedmap.Map[int, int] {
1517
return intlinkedhashmap.NewDefault[int, int]()

0 commit comments

Comments
 (0)