Cache of the valid channel name regex

* Added a Benchmark to verify the performance
* Now it is 10 times faster
This commit is contained in:
claudemiro
2016-01-23 22:25:14 -02:00
parent 56da1dea01
commit 517462a7af
3 changed files with 21 additions and 11 deletions
+8 -10
View File
@@ -14,6 +14,12 @@ import (
"regexp"
)
var validChannelName *regexp.Regexp
func init() {
validChannelName = regexp.MustCompile("^[A-Za-z0-9_\\-=@,.;]+$")
}
// HashMAC Calculates the MAC signing with the given key and returns the hexadecimal encoded Result
func HashMAC(message, key []byte) string {
mac := hmac.New(sha256.New, key)
@@ -25,18 +31,10 @@ func HashMAC(message, key []byte) string {
// GenerateSessionID Generate a new random Hash
func GenerateSessionID() string {
MAX := math.MaxInt64
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
return fmt.Sprintf("%d.%d", rand.Intn(math.MaxInt64), rand.Intn(math.MaxInt64))
}
// IsChannelNameValid Verify if the channel name is valid
func IsChannelNameValid(channelName string) bool {
matched, err := regexp.MatchString("^[A-Za-z0-9_\\-=@,.;]+$", channelName)
if err == nil && matched {
return true
}
return false
return validChannelName.Match([]byte(channelName))
}
+12
View File
@@ -9,6 +9,18 @@ import (
"testing"
)
func BenchmarkGenerateSession(b *testing.B) {
for i := 0; i < b.N; i++ {
GenerateSessionID()
}
}
func BenchmarkIsChannelNameValid(b *testing.B) {
for i := 0; i < b.N; i++ {
IsChannelNameValid("hello-world")
}
}
func TestGenerateSession(t *testing.T) {
sessionID := GenerateSessionID()