From 992d0560b6378bbe2ec0f03ef373d45dc0fb0b00 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Tue, 2 Feb 2021 14:27:55 +0800 Subject: [PATCH] =?UTF-8?q?WAF=E5=A2=9E=E5=8A=A0=E8=8B=A5=E5=B9=B2?= =?UTF-8?q?=E4=B8=AA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/waf/utils/utils_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/internal/waf/utils/utils_test.go b/internal/waf/utils/utils_test.go index 653b7ee..991ac85 100644 --- a/internal/waf/utils/utils_test.go +++ b/internal/waf/utils/utils_test.go @@ -3,9 +3,11 @@ package utils import ( "net/http" "regexp" + "runtime" "strconv" "strings" "testing" + "time" ) func TestMatchStringCache(t *testing.T) { @@ -38,7 +40,17 @@ func TestMatchRemoteCache(t *testing.T) { } } +func TestMatchBytesCache_WithoutCache(t *testing.T) { + data := []byte(strings.Repeat("HELLO", 512)) + regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) + before := time.Now() + t.Log(regex.Match(data)) + t.Log(time.Since(before).Seconds()*1000, "ms") +} + func BenchmarkMatchStringCache(b *testing.B) { + runtime.GOMAXPROCS(1) + data := strings.Repeat("HELLO", 512) regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) _ = MatchStringCache(regex, data) @@ -49,6 +61,8 @@ func BenchmarkMatchStringCache(b *testing.B) { } func BenchmarkMatchStringCache_WithoutCache(b *testing.B) { + runtime.GOMAXPROCS(1) + data := strings.Repeat("HELLO", 512) regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) @@ -56,3 +70,14 @@ func BenchmarkMatchStringCache_WithoutCache(b *testing.B) { _ = regex.MatchString(data) } } + +func BenchmarkMatchBytesCache_WithoutCache(b *testing.B) { + runtime.GOMAXPROCS(1) + + data := []byte(strings.Repeat("HELLO", 512)) + regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) + + for i := 0; i < b.N; i++ { + _ = regex.Match(data) + } +}