From 15ad5a157f18d13090ae18ddef29ed2a0aa4696b Mon Sep 17 00:00:00 2001 From: Matteo Pace Date: Tue, 5 Nov 2024 11:13:39 +0000 Subject: [PATCH] chore: minor fixes, false positives ratio printed (#390) * things * fix test * move to seconds --- internal/quantitative/leipzig/corpus.go | 7 +++++-- internal/quantitative/local_engine.go | 4 +++- internal/quantitative/runner.go | 8 ++++---- internal/quantitative/stats.go | 5 +++-- internal/quantitative/stats_test.go | 10 +++++----- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/quantitative/leipzig/corpus.go b/internal/quantitative/leipzig/corpus.go index de57e5d..19887bb 100644 --- a/internal/quantitative/leipzig/corpus.go +++ b/internal/quantitative/leipzig/corpus.go @@ -9,6 +9,7 @@ import ( "os" "path" "path/filepath" + "strings" "github.com/hashicorp/go-getter" "github.com/rs/zerolog/log" @@ -48,11 +49,13 @@ type LeipzigCorpus struct { } func (c *LeipzigCorpus) regenerateFileNames() { + size := strings.ToUpper(c.size) + c.corpusFile = fmt.Sprintf("%s_%s_%s_%s.%s", - c.lang, c.source, c.year, c.size, + c.lang, c.source, c.year, size, defaultCorpusExt) c.filename = fmt.Sprintf("%s_%s_%s_%s-%s", - c.lang, c.source, c.year, c.size, + c.lang, c.source, c.year, size, defaultCorpusType) } diff --git a/internal/quantitative/local_engine.go b/internal/quantitative/local_engine.go index 89142c2..9ada2d8 100644 --- a/internal/quantitative/local_engine.go +++ b/internal/quantitative/local_engine.go @@ -165,9 +165,11 @@ func getMatchedRules(tx types.Transaction) map[int]string { } var logData strings.Builder for i, matchData := range rule.MatchedDatas() { - logData.WriteString(" chain#") + logData.WriteString(" #") logData.WriteString(strconv.Itoa(i)) logData.WriteString(": ") + logData.WriteString(matchData.Key()) + logData.WriteString(":") logData.WriteString(matchData.Value()) } matchedRules[id] = logData.String() diff --git a/internal/quantitative/runner.go b/internal/quantitative/runner.go index a773765..8465113 100644 --- a/internal/quantitative/runner.go +++ b/internal/quantitative/runner.go @@ -158,13 +158,13 @@ func doEngineCall(engine LocalEngine, payload corpus.Payload, specificRule int, // append the line to the false positives log.Trace().Msgf("False positive with string: %s", payload) log.Trace().Msgf("=> rules matched: %+v", matchedRules) - for rule, data := range matchedRules { + for ruleId, data := range matchedRules { // check if we only want to show false positives for a specific rule - if wantSpecificRuleResults(specificRule, rule) { + if wantSpecificRuleResults(specificRule, ruleId) { continue } - stats.addFalsePositive(rule) - log.Debug().Msgf("**> rule %d with payload %d => %s", rule, payload.LineNumber(), data) + stats.addFalsePositive(ruleId) + log.Debug().Msgf("**> rule %d with payload %d => %s", ruleId, payload.LineNumber(), data) } } } diff --git a/internal/quantitative/stats.go b/internal/quantitative/stats.go index a455b94..aecbdd4 100644 --- a/internal/quantitative/stats.go +++ b/internal/quantitative/stats.go @@ -61,7 +61,8 @@ func (s *QuantitativeRunStats) printSummary(out *output.Output) { // Print the sorted map for _, rule := range rules { count := s.falsePositivesPerRule[rule] - out.Println(" %d: %d false positives", rule, count) + perRuleRatio := float64(count) / float64(s.count_) + out.Println(" %d: %d false positives. FP Ratio: %d/%d = %.4f", rule, count, count, s.count_, perRuleRatio) } } } else { @@ -107,7 +108,7 @@ func (s *QuantitativeRunStats) MarshalJSON() ([]byte, error) { // Custom marshaling logic here return json.Marshal(map[string]interface{}{ "count": s.count_, - "totalTime": s.totalTime, + "totalTimeSeconds": s.totalTime.Seconds(), "falsePositives": s.falsePositives, "falsePositivesPerRule": s.falsePositivesPerRule, }) diff --git a/internal/quantitative/stats_test.go b/internal/quantitative/stats_test.go index 68ed2fa..96841d8 100644 --- a/internal/quantitative/stats_test.go +++ b/internal/quantitative/stats_test.go @@ -62,22 +62,22 @@ func (s *statsTestSuite) TestQuantitativeRunStats_MarshalJSON() { name: "Test 1", fields: fields{ count_: 1, - totalTime: 1, + totalTime: time.Second, falsePositives: 1, falsePositivesPerRule: map[int]int{920010: 1}, }, - want: []byte(`{"count":1,"falsePositives":1,"falsePositivesPerRule":{"920010":1},"totalTime":1}`), + want: []byte(`{"count":1,"falsePositives":1,"falsePositivesPerRule":{"920010":1},"totalTimeSeconds":1}`), wantErr: false, }, { name: "Test 2", fields: fields{ count_: 2, - totalTime: 2, + totalTime: time.Second * 2, falsePositives: 2, falsePositivesPerRule: map[int]int{933100: 2}, }, - want: []byte(`{"count":2,"falsePositives":2,"falsePositivesPerRule":{"933100":2},"totalTime":2}`), + want: []byte(`{"count":2,"falsePositives":2,"falsePositivesPerRule":{"933100":2},"totalTimeSeconds":2}`), wantErr: false, }, } @@ -128,7 +128,7 @@ func (s *statsTestSuite) TestQuantitativeRunStats_printSummary() { s.Require().Equal(q.FalsePositives(), 1) q.printSummary(out) - s.Require().Equal("Run 1 payloads in 0s\nTotal False positive ratio: 1/1 = 1.0000\nFalse positives per rule id:\n 920100: 1 false positives\n", b.String()) + s.Require().Equal("Run 1 payloads in 0s\nTotal False positive ratio: 1/1 = 1.0000\nFalse positives per rule id:\n 920100: 1 false positives. FP Ratio: 1/1 = 1.0000\n", b.String()) } func TestAddFalsePositiveRace(t *testing.T) {