Created
July 20, 2022 04:15
-
-
Save amyangfei/aa9f3edb2785dafb86296b148da56c9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
var allowList = []string{ | |
"create", | |
"cancel", | |
"update", | |
} | |
var allowMap = map[string]struct{}{ | |
"create": {}, | |
"cancel": {}, | |
"update": {}, | |
} | |
type Searcher interface { | |
Search(method string) bool | |
} | |
type SliceSearcher struct{} | |
func (ss *SliceSearcher) Search(method string) bool { | |
for _, m := range allowList { | |
if m == method { | |
return true | |
} | |
} | |
return false | |
} | |
type MapSearcher struct{} | |
func (ms *MapSearcher) Search(method string) bool { | |
if _, ok := allowMap[method]; ok { | |
return true | |
} | |
return false | |
} | |
func DoSearch(s Searcher) (count int) { | |
if s.Search("create") { | |
count++ | |
} | |
if s.Search("cancel") { | |
count++ | |
} | |
if s.Search("update") { | |
count++ | |
} | |
for i := 0; i < 20; i++ { | |
if s.Search("heartbeat") { | |
count++ | |
} | |
} | |
return | |
} | |
func BenchmarkSliceSearch(b *testing.B) { | |
count := 0 | |
s := &SliceSearcher{} | |
for n := 0; n < b.N; n++ { | |
count += DoSearch(s) | |
} | |
} | |
func BenchmarkMapSearch(b *testing.B) { | |
count := 0 | |
s := &MapSearcher{} | |
for n := 0; n < b.N; n++ { | |
count += DoSearch(s) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment