|
| 1 | +package jsondb |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/BlackVectorOps/semantic_firewall/v3/pkg/detection" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAddSignatures(t *testing.T) { |
| 11 | + s := NewScanner() |
| 12 | + sigs := []detection.Signature{ |
| 13 | + {Name: "Sig1"}, |
| 14 | + {Name: "Sig2"}, |
| 15 | + } |
| 16 | + if err := s.AddSignatures(sigs); err != nil { |
| 17 | + t.Fatalf("AddSignatures failed: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + if len(s.db.Signatures) != 2 { |
| 21 | + t.Errorf("Expected 2 signatures, got %d", len(s.db.Signatures)) |
| 22 | + } |
| 23 | + if s.db.Signatures[0].Name != "Sig1" { |
| 24 | + t.Errorf("Expected Sig1, got %s", s.db.Signatures[0].Name) |
| 25 | + } |
| 26 | + // Verify IDs were generated |
| 27 | + if s.db.Signatures[0].ID == "" { |
| 28 | + t.Error("ID should have been generated for Sig1") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +const count = 10000 |
| 33 | + |
| 34 | +func BenchmarkAddSignatureLoop(b *testing.B) { |
| 35 | + sigs := make([]detection.Signature, count) |
| 36 | + for i := 0; i < count; i++ { |
| 37 | + sigs[i] = detection.Signature{ |
| 38 | + ID: fmt.Sprintf("SIG-%d", i), |
| 39 | + Name: fmt.Sprintf("Signature %d", i), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + b.ResetTimer() |
| 44 | + for i := 0; i < b.N; i++ { |
| 45 | + s := NewScanner() |
| 46 | + s.db.Signatures = make([]detection.Signature, 0, count) |
| 47 | + |
| 48 | + for j := 0; j < count; j++ { |
| 49 | + _ = s.AddSignature(&sigs[j]) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func BenchmarkAddSignaturesBatch(b *testing.B) { |
| 55 | + sigs := make([]detection.Signature, count) |
| 56 | + for i := 0; i < count; i++ { |
| 57 | + sigs[i] = detection.Signature{ |
| 58 | + ID: fmt.Sprintf("SIG-%d", i), |
| 59 | + Name: fmt.Sprintf("Signature %d", i), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + b.ResetTimer() |
| 64 | + for i := 0; i < b.N; i++ { |
| 65 | + s := NewScanner() |
| 66 | + s.db.Signatures = make([]detection.Signature, 0, count) |
| 67 | + _ = s.AddSignatures(sigs) |
| 68 | + } |
| 69 | +} |
0 commit comments