From bb19cf9f5e5c0031a869b674b10b3fed004d5081 Mon Sep 17 00:00:00 2001 From: Roshan Piyush Date: Tue, 5 Nov 2024 15:30:13 +0000 Subject: [PATCH 1/3] Replace sync.Mutex with sync.Map --- internal/corazawaf/rule.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/internal/corazawaf/rule.go b/internal/corazawaf/rule.go index cd6cd6269..211e956a4 100644 --- a/internal/corazawaf/rule.go +++ b/internal/corazawaf/rule.go @@ -578,22 +578,18 @@ func (r *Rule) AddVariableNegation(v variables.RuleVariable, key string) error { } var transformationIDToName = []string{""} -var transformationNameToID = map[string]int{"": 0} -var transformationIDsLock = sync.Mutex{} +var transformationNameToID = sync.Map{} // map[string]int func transformationID(currentID int, transformationName string) int { - transformationIDsLock.Lock() - defer transformationIDsLock.Unlock() - currName := transformationIDToName[currentID] nextName := fmt.Sprintf("%s+%s", currName, transformationName) - if id, ok := transformationNameToID[nextName]; ok { - return id + if id, ok := transformationNameToID.Load(nextName); ok { + return id.(int) } id := len(transformationIDToName) transformationIDToName = append(transformationIDToName, nextName) - transformationNameToID[nextName] = id + transformationNameToID.Store(nextName, id) return id } From 0c57c99086494a8259ce6f05ba4b1d4563cca44d Mon Sep 17 00:00:00 2001 From: Roshan Piyush Date: Wed, 6 Nov 2024 10:40:15 +0000 Subject: [PATCH 2/3] Benchmark AddTransformation --- internal/corazawaf/rule_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/internal/corazawaf/rule_test.go b/internal/corazawaf/rule_test.go index 06d9c219e..739222a62 100644 --- a/internal/corazawaf/rule_test.go +++ b/internal/corazawaf/rule_test.go @@ -384,6 +384,34 @@ func TestAddTransformation(t *testing.T) { } } +func BenchmarkAddTransformationUnique(b *testing.B) { + rule := NewRule() + transformation := func(input string) (string, bool, error) { + return "Test", true, nil + } + for i := 0; i < b.N; i++ { + transformationName := "transformation" + strconv.Itoa(i) + err := rule.AddTransformation(transformationName, transformation) + if err != nil { + b.Fatalf("Failed to add a transformation: %s", err.Error()) + } + } +} + +func BenchmarkAddTransformationSame(b *testing.B) { + transformation := func(input string) (string, bool, error) { + return "Test", true, nil + } + for i := 0; i < b.N; i++ { + rule := NewRule() + transformationName := "transformation" + strconv.Itoa(i) + err := rule.AddTransformation(transformationName, transformation) + if err != nil { + b.Fatalf("Failed to add a transformation: %s", err.Error()) + } + } +} + func TestAddTransformationEmpty(t *testing.T) { rule := NewRule() transformationName := "" From b67ffc19ff60713057f009be2b3d0dd69d8555f2 Mon Sep 17 00:00:00 2001 From: Juan Pablo Tosso Date: Fri, 8 Nov 2024 12:30:49 +0000 Subject: [PATCH 3/3] update benchmark paralelism --- internal/corazawaf/rule_test.go | 34 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/corazawaf/rule_test.go b/internal/corazawaf/rule_test.go index 739222a62..01e1fb4ad 100644 --- a/internal/corazawaf/rule_test.go +++ b/internal/corazawaf/rule_test.go @@ -385,31 +385,37 @@ func TestAddTransformation(t *testing.T) { } func BenchmarkAddTransformationUnique(b *testing.B) { - rule := NewRule() transformation := func(input string) (string, bool, error) { return "Test", true, nil } - for i := 0; i < b.N; i++ { - transformationName := "transformation" + strconv.Itoa(i) - err := rule.AddTransformation(transformationName, transformation) - if err != nil { - b.Fatalf("Failed to add a transformation: %s", err.Error()) + b.ResetTimer() + b.RunParallel(func(p *testing.PB) { + rule := NewRule() + for p.Next() { + transformationName := "transformation" + b.Name() + err := rule.AddTransformation(transformationName, transformation) + if err != nil { + b.Fatalf("Failed to add a transformation: %s", err.Error()) + } } - } + }) } func BenchmarkAddTransformationSame(b *testing.B) { transformation := func(input string) (string, bool, error) { return "Test", true, nil } - for i := 0; i < b.N; i++ { - rule := NewRule() - transformationName := "transformation" + strconv.Itoa(i) - err := rule.AddTransformation(transformationName, transformation) - if err != nil { - b.Fatalf("Failed to add a transformation: %s", err.Error()) + b.ResetTimer() + b.RunParallel(func(p *testing.PB) { + for p.Next() { + rule := NewRule() + transformationName := "transformation" + err := rule.AddTransformation(transformationName, transformation) + if err != nil { + b.Fatalf("Failed to add a transformation: %s", err.Error()) + } } - } + }) } func TestAddTransformationEmpty(t *testing.T) {