Skip to content

Commit

Permalink
Show more decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcl committed Jan 8, 2025
1 parent 97a6b31 commit 96f78d2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 112 deletions.
52 changes: 22 additions & 30 deletions tools/flakeguard/reports/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package reports

import (
"fmt"
"math"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -121,6 +120,7 @@ type SplunkTestResultEvent struct {

func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
var runs, passes, fails, skips, panickedTests, racedTests, flakyTests, skippedTests int

for _, result := range tests {
runs += result.Runs
passes += result.Successes
Expand All @@ -143,40 +143,33 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
}
}

// Calculate the PASS ratio (as a float) and then build a string
passPercentageStr := "100.00%" // default if runs == 0

// Calculate the raw pass ratio
passPercentage := 100.0
if runs > 0 {
rawPassRatio := (float64(passes) / float64(runs)) * 100
// Round to 8 decimal places to avoid floating-point quirks
passPercentage := math.Floor(rawPassRatio*1e8) / 1e8

// If there's any failure, never show 100.00%
if fails > 0 {
passPercentage = math.Min(passPercentage, 99.99)
}

passPercentageStr = fmt.Sprintf("%.2f%%", passPercentage)
passPercentage = (float64(passes) / float64(runs)) * 100
}

// Calculate the FLAKE ratio (as a float) and then build a string
flakePercentage := 0.0
flakeTestRatioStr := "0.00%" // default if totalTests == 0

// Calculate the raw flake ratio
totalTests := len(tests)
flakePercentage := 0.0
if totalTests > 0 {
rawFlakeRatio := (float64(flakyTests) / float64(totalTests)) * 100

// If non-zero but < 0.01, show "< 0.01%"
if rawFlakeRatio > 0 && rawFlakeRatio < 0.01 {
flakeTestRatioStr = "< 0.01%"
} else {
flakePercentage = math.Floor(rawFlakeRatio*100) / 100
flakeTestRatioStr = fmt.Sprintf("%.2f%%", flakePercentage)
}
flakePercentage = (float64(flakyTests) / float64(totalTests)) * 100
}

// 3) Build the SummaryData with the final strings
// Helper function to convert a float ratio into a trimmed string
formatRatio := func(val float64) string {
// Format with 5 decimal places
s := fmt.Sprintf("%.4f", val)
// Trim trailing zeros
s = strings.TrimRight(s, "0")
// Trim trailing '.' if needed (in case we have an integer)
s = strings.TrimRight(s, ".")
return s + "%"
}

passRatioStr := formatRatio(passPercentage)
flakeTestRatioStr := formatRatio(flakePercentage)

return SummaryData{
TotalTests: totalTests,
PanickedTests: panickedTests,
Expand All @@ -189,8 +182,7 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
FailedRuns: fails,
SkippedRuns: skips,

// Use the final passPercentageStr
PassRatio: passPercentageStr,
PassRatio: passRatioStr,
MaxPassRatio: maxPassRatio,
}
}
Expand Down
117 changes: 35 additions & 82 deletions tools/flakeguard/reports/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ func TestGenerateSummaryData(t *testing.T) {
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 0,
FlakyTestRatio: "0.00%", // no flaky tests
FlakyTestRatio: "0%", // no flaky tests
TotalRuns: 15,
PassedRuns: 15,
FailedRuns: 0,
SkippedRuns: 0,
// No failures => truly 100.00%
PassRatio: "100.00%",
MaxPassRatio: 1.0,
PassRatio: "100%",
MaxPassRatio: 1.0,
},
},
{
Expand All @@ -51,14 +50,14 @@ func TestGenerateSummaryData(t *testing.T) {
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 2,
// 2/3 => 66.666...% => rounds to 66.66%
FlakyTestRatio: "66.66%",
// 2/3 => 66.666...%
FlakyTestRatio: "66.6667%",
TotalRuns: 19,
PassedRuns: 15,
FailedRuns: 4, // total failures
SkippedRuns: 0,
// 15/19 => ~78.947... => rounds to 78.95%
PassRatio: "78.95%",
// 15/19 => ~78.947...
PassRatio: "78.9474%",
MaxPassRatio: 0.9,
},
},
Expand All @@ -75,14 +74,14 @@ func TestGenerateSummaryData(t *testing.T) {
PanickedTests: 1,
RacedTests: 1,
FlakyTests: 2,
// 2/3 => ~66.666... => "66.66%"
FlakyTestRatio: "66.66%",
// 2/3 => ~66.666...
FlakyTestRatio: "66.6667%",
TotalRuns: 18,
PassedRuns: 17,
FailedRuns: 1,
SkippedRuns: 0,
// 17/18 => ~94.444... => "94.44%"
PassRatio: "94.44%",
// 17/18 => ~94.444...
PassRatio: "94.4444%",
MaxPassRatio: 1.0,
},
},
Expand All @@ -95,13 +94,13 @@ func TestGenerateSummaryData(t *testing.T) {
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 0,
FlakyTestRatio: "0.00%", // no tests => 0.00%
FlakyTestRatio: "0%", // no tests => 0%
TotalRuns: 0,
PassedRuns: 0,
FailedRuns: 0,
SkippedRuns: 0,
// With zero runs, we default passRatio to "100.00%"
PassRatio: "100.00%",
// With zero runs, we default passRatio to "100%"
PassRatio: "100%",
MaxPassRatio: 1.0,
},
},
Expand All @@ -116,13 +115,13 @@ func TestGenerateSummaryData(t *testing.T) {
TotalTests: 2,
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 1, // second test has ratio=0.7 => "flaky"
FlakyTestRatio: "50.00%", // 1 out of 2 => 50.00%
FlakyTests: 1, // second test has ratio=0.7 => "flaky"
FlakyTestRatio: "50%", // 1 out of 2 => 50%
TotalRuns: 10,
PassedRuns: 7,
FailedRuns: 3,
SkippedRuns: 1, // from first test
PassRatio: "70.00%",
PassRatio: "70%",
MaxPassRatio: 0.8,
},
},
Expand All @@ -138,14 +137,14 @@ func TestGenerateSummaryData(t *testing.T) {
TotalTests: 3,
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 1, // last test has ratio=0.5 => "flaky"
FlakyTestRatio: "33.33%", // 1 out of 3 => 33.333... => 33.33%
TotalRuns: 14, // 10 + 4
PassedRuns: 11, // 9 + 2
FailedRuns: 3, // 1 + 2
SkippedRuns: 1, // from first test
// 11/14 => 78.5714... => "78.57%"
PassRatio: "78.57%",
FlakyTests: 1, // last test has ratio=0.5 => "flaky"
FlakyTestRatio: "33.3333%", // 1 out of 3 => 33.333...
TotalRuns: 14, // 10 + 4
PassedRuns: 11, // 9 + 2
FailedRuns: 3, // 1 + 2
SkippedRuns: 1, // from first test
// 11/14 => 78.5714...
PassRatio: "78.5714%",
MaxPassRatio: 0.85,
},
},
Expand Down Expand Up @@ -174,63 +173,17 @@ func TestGenerateSummaryData(t *testing.T) {
}(),
maxPassRatio: 1.0,
expected: SummaryData{
TotalTests: 9999,
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 1,
// ratio = 1/9999 => ~0.00010001 => rounds to 0.01%
TotalTests: 9999,
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 1,
FlakyTestRatio: "0.01%",
// total runs => (9998 stable * 10 each) + 2 = 99,982
TotalRuns: (9998 * 10) + 2,
// total passes => (9998 stable * 10) + 1 success in flaky = 99,981
PassedRuns: (9998 * 10) + 1,
FailedRuns: 1,
SkippedRuns: 0,
// 1 failure => we do NOT show 100% => we cap at 99.99%
PassRatio: "99.99%",
MaxPassRatio: 1.0,
},
},
{
name: "Tiny flake ratio below threshold",
testResults: func() []TestResult {
// 100,001 total:
// - 100,000 stable => pass=1.0
// - 1 flaky => pass=0.5
const total = 100001
tests := make([]TestResult, total)
for i := 0; i < total-1; i++ {
tests[i] = TestResult{
PassRatio: 1.0,
Runs: 10,
Successes: 10,
}
}
tests[total-1] = TestResult{
PassRatio: 0.5, // 1 success, 1 fail
Runs: 2,
Successes: 1,
Failures: 1,
}
return tests
}(),
maxPassRatio: 1.0,
expected: SummaryData{
TotalTests: 100001,
PanickedTests: 0,
RacedTests: 0,
FlakyTests: 1,
// 1 / 100001 => ~0.000009999 => definitely < 0.01%
FlakyTestRatio: "< 0.01%",
// total runs => (100,000 stable * 10) + 2 = 1,000,002
TotalRuns: (100000 * 10) + 2,
// passes => stable=1,000,000 + 1 success from flaky=1 => 1,000,001
PassedRuns: 100000*10 + 1,
FailedRuns: 1,
SkippedRuns: 0,
// again 1 failure => cap pass ratio => "99.99%"
PassRatio: "99.99%",
MaxPassRatio: 1.0,
TotalRuns: (9998 * 10) + 2,
PassedRuns: (9998 * 10) + 1,
FailedRuns: 1,
SkippedRuns: 0,
PassRatio: "99.999%",
MaxPassRatio: 1.0,
},
},
}
Expand Down

0 comments on commit 96f78d2

Please sign in to comment.