Skip to content

Commit

Permalink
test(map): added more benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
HotPotatoC committed Dec 23, 2021
1 parent d5117b5 commit f24dbc6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
55 changes: 52 additions & 3 deletions datastructure/map_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,30 @@ func Benchmark_Get(b *testing.B) {

func Benchmark_Delete(b *testing.B) {
hmap := datastructure.NewMap()
hmap.Store(datastructure.NewItem("key", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hello", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hallo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hbllo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hxllo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hllo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("heeeello", []byte("value"), 0))

benchmarks := []struct {
name string
pattern string
}{
{"All", "*"},
{"AllMixed", "h*llo"},
{"OneChar", "h?llo"},
{"Range", "h[a-e]llo"},
{"Mixed", "?[a-e]*"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
hmap.Delete("key")
for _, v := range benchmarks {
b.Run(v.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
hmap.Delete(v.pattern)
}
})
}
}

Expand All @@ -49,3 +69,32 @@ func Benchmark_Keys(b *testing.B) {
hmap.Keys()
}
}

func Benchmark_KeysWithPattern(b *testing.B) {
hmap := datastructure.NewMap()
hmap.Store(datastructure.NewItem("hello", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hallo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hbllo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hxllo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("hllo", []byte("value"), 0))
hmap.Store(datastructure.NewItem("heeeello", []byte("value"), 0))

benchmarks := []struct {
name string
pattern string
}{
{"All", "*"},
{"AllMixed", "h*llo"},
{"OneChar", "h?llo"},
{"Range", "h[a-e]llo"},
{"Mixed", "?[a-e]*"},
}
b.ResetTimer()
for _, v := range benchmarks {
b.Run(v.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
hmap.KeysWithPattern(v.pattern)
}
})
}
}
1 change: 1 addition & 0 deletions datastructure/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Test_KeysWithPattern(t *testing.T) {
{"*", []string{"hello", "hallo", "hbllo", "hllo", "hxllo", "heeeeello"}},
{"h[a-e]llo", []string{"hello", "hallo", "hbllo"}},
{"h?llo", []string{"hello", "hallo", "hbllo", "hxllo"}},
{"?[a-e]*", []string{"hello", "hallo", "hbllo", "heeeeello"}},
}

for _, tt := range tc {
Expand Down

0 comments on commit f24dbc6

Please sign in to comment.