Skip to content

Commit

Permalink
chore: minor fixes, false positives ratio printed (#390)
Browse files Browse the repository at this point in the history
* things

* fix test

* move to seconds
  • Loading branch information
M4tteoP authored Nov 5, 2024
1 parent 18e3472 commit 15ad5a1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
7 changes: 5 additions & 2 deletions internal/quantitative/leipzig/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

"github.com/hashicorp/go-getter"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion internal/quantitative/local_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions internal/quantitative/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
5 changes: 3 additions & 2 deletions internal/quantitative/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
})
Expand Down
10 changes: 5 additions & 5 deletions internal/quantitative/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 15ad5a1

Please sign in to comment.