Skip to content

Commit 96f78d2

Browse files
committed
Show more decimal places
1 parent 97a6b31 commit 96f78d2

File tree

2 files changed

+57
-112
lines changed

2 files changed

+57
-112
lines changed

tools/flakeguard/reports/data.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package reports
22

33
import (
44
"fmt"
5-
"math"
65
"sort"
76
"strings"
87
"time"
@@ -121,6 +120,7 @@ type SplunkTestResultEvent struct {
121120

122121
func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
123122
var runs, passes, fails, skips, panickedTests, racedTests, flakyTests, skippedTests int
123+
124124
for _, result := range tests {
125125
runs += result.Runs
126126
passes += result.Successes
@@ -143,40 +143,33 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
143143
}
144144
}
145145

146-
// Calculate the PASS ratio (as a float) and then build a string
147-
passPercentageStr := "100.00%" // default if runs == 0
148-
146+
// Calculate the raw pass ratio
147+
passPercentage := 100.0
149148
if runs > 0 {
150-
rawPassRatio := (float64(passes) / float64(runs)) * 100
151-
// Round to 8 decimal places to avoid floating-point quirks
152-
passPercentage := math.Floor(rawPassRatio*1e8) / 1e8
153-
154-
// If there's any failure, never show 100.00%
155-
if fails > 0 {
156-
passPercentage = math.Min(passPercentage, 99.99)
157-
}
158-
159-
passPercentageStr = fmt.Sprintf("%.2f%%", passPercentage)
149+
passPercentage = (float64(passes) / float64(runs)) * 100
160150
}
161151

162-
// Calculate the FLAKE ratio (as a float) and then build a string
163-
flakePercentage := 0.0
164-
flakeTestRatioStr := "0.00%" // default if totalTests == 0
165-
152+
// Calculate the raw flake ratio
166153
totalTests := len(tests)
154+
flakePercentage := 0.0
167155
if totalTests > 0 {
168-
rawFlakeRatio := (float64(flakyTests) / float64(totalTests)) * 100
169-
170-
// If non-zero but < 0.01, show "< 0.01%"
171-
if rawFlakeRatio > 0 && rawFlakeRatio < 0.01 {
172-
flakeTestRatioStr = "< 0.01%"
173-
} else {
174-
flakePercentage = math.Floor(rawFlakeRatio*100) / 100
175-
flakeTestRatioStr = fmt.Sprintf("%.2f%%", flakePercentage)
176-
}
156+
flakePercentage = (float64(flakyTests) / float64(totalTests)) * 100
177157
}
178158

179-
// 3) Build the SummaryData with the final strings
159+
// Helper function to convert a float ratio into a trimmed string
160+
formatRatio := func(val float64) string {
161+
// Format with 5 decimal places
162+
s := fmt.Sprintf("%.4f", val)
163+
// Trim trailing zeros
164+
s = strings.TrimRight(s, "0")
165+
// Trim trailing '.' if needed (in case we have an integer)
166+
s = strings.TrimRight(s, ".")
167+
return s + "%"
168+
}
169+
170+
passRatioStr := formatRatio(passPercentage)
171+
flakeTestRatioStr := formatRatio(flakePercentage)
172+
180173
return SummaryData{
181174
TotalTests: totalTests,
182175
PanickedTests: panickedTests,
@@ -189,8 +182,7 @@ func GenerateSummaryData(tests []TestResult, maxPassRatio float64) SummaryData {
189182
FailedRuns: fails,
190183
SkippedRuns: skips,
191184

192-
// Use the final passPercentageStr
193-
PassRatio: passPercentageStr,
185+
PassRatio: passRatioStr,
194186
MaxPassRatio: maxPassRatio,
195187
}
196188
}

tools/flakeguard/reports/data_test.go

Lines changed: 35 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ func TestGenerateSummaryData(t *testing.T) {
2828
PanickedTests: 0,
2929
RacedTests: 0,
3030
FlakyTests: 0,
31-
FlakyTestRatio: "0.00%", // no flaky tests
31+
FlakyTestRatio: "0%", // no flaky tests
3232
TotalRuns: 15,
3333
PassedRuns: 15,
3434
FailedRuns: 0,
3535
SkippedRuns: 0,
36-
// No failures => truly 100.00%
37-
PassRatio: "100.00%",
38-
MaxPassRatio: 1.0,
36+
PassRatio: "100%",
37+
MaxPassRatio: 1.0,
3938
},
4039
},
4140
{
@@ -51,14 +50,14 @@ func TestGenerateSummaryData(t *testing.T) {
5150
PanickedTests: 0,
5251
RacedTests: 0,
5352
FlakyTests: 2,
54-
// 2/3 => 66.666...% => rounds to 66.66%
55-
FlakyTestRatio: "66.66%",
53+
// 2/3 => 66.666...%
54+
FlakyTestRatio: "66.6667%",
5655
TotalRuns: 19,
5756
PassedRuns: 15,
5857
FailedRuns: 4, // total failures
5958
SkippedRuns: 0,
60-
// 15/19 => ~78.947... => rounds to 78.95%
61-
PassRatio: "78.95%",
59+
// 15/19 => ~78.947...
60+
PassRatio: "78.9474%",
6261
MaxPassRatio: 0.9,
6362
},
6463
},
@@ -75,14 +74,14 @@ func TestGenerateSummaryData(t *testing.T) {
7574
PanickedTests: 1,
7675
RacedTests: 1,
7776
FlakyTests: 2,
78-
// 2/3 => ~66.666... => "66.66%"
79-
FlakyTestRatio: "66.66%",
77+
// 2/3 => ~66.666...
78+
FlakyTestRatio: "66.6667%",
8079
TotalRuns: 18,
8180
PassedRuns: 17,
8281
FailedRuns: 1,
8382
SkippedRuns: 0,
84-
// 17/18 => ~94.444... => "94.44%"
85-
PassRatio: "94.44%",
83+
// 17/18 => ~94.444...
84+
PassRatio: "94.4444%",
8685
MaxPassRatio: 1.0,
8786
},
8887
},
@@ -95,13 +94,13 @@ func TestGenerateSummaryData(t *testing.T) {
9594
PanickedTests: 0,
9695
RacedTests: 0,
9796
FlakyTests: 0,
98-
FlakyTestRatio: "0.00%", // no tests => 0.00%
97+
FlakyTestRatio: "0%", // no tests => 0%
9998
TotalRuns: 0,
10099
PassedRuns: 0,
101100
FailedRuns: 0,
102101
SkippedRuns: 0,
103-
// With zero runs, we default passRatio to "100.00%"
104-
PassRatio: "100.00%",
102+
// With zero runs, we default passRatio to "100%"
103+
PassRatio: "100%",
105104
MaxPassRatio: 1.0,
106105
},
107106
},
@@ -116,13 +115,13 @@ func TestGenerateSummaryData(t *testing.T) {
116115
TotalTests: 2,
117116
PanickedTests: 0,
118117
RacedTests: 0,
119-
FlakyTests: 1, // second test has ratio=0.7 => "flaky"
120-
FlakyTestRatio: "50.00%", // 1 out of 2 => 50.00%
118+
FlakyTests: 1, // second test has ratio=0.7 => "flaky"
119+
FlakyTestRatio: "50%", // 1 out of 2 => 50%
121120
TotalRuns: 10,
122121
PassedRuns: 7,
123122
FailedRuns: 3,
124123
SkippedRuns: 1, // from first test
125-
PassRatio: "70.00%",
124+
PassRatio: "70%",
126125
MaxPassRatio: 0.8,
127126
},
128127
},
@@ -138,14 +137,14 @@ func TestGenerateSummaryData(t *testing.T) {
138137
TotalTests: 3,
139138
PanickedTests: 0,
140139
RacedTests: 0,
141-
FlakyTests: 1, // last test has ratio=0.5 => "flaky"
142-
FlakyTestRatio: "33.33%", // 1 out of 3 => 33.333... => 33.33%
143-
TotalRuns: 14, // 10 + 4
144-
PassedRuns: 11, // 9 + 2
145-
FailedRuns: 3, // 1 + 2
146-
SkippedRuns: 1, // from first test
147-
// 11/14 => 78.5714... => "78.57%"
148-
PassRatio: "78.57%",
140+
FlakyTests: 1, // last test has ratio=0.5 => "flaky"
141+
FlakyTestRatio: "33.3333%", // 1 out of 3 => 33.333...
142+
TotalRuns: 14, // 10 + 4
143+
PassedRuns: 11, // 9 + 2
144+
FailedRuns: 3, // 1 + 2
145+
SkippedRuns: 1, // from first test
146+
// 11/14 => 78.5714...
147+
PassRatio: "78.5714%",
149148
MaxPassRatio: 0.85,
150149
},
151150
},
@@ -174,63 +173,17 @@ func TestGenerateSummaryData(t *testing.T) {
174173
}(),
175174
maxPassRatio: 1.0,
176175
expected: SummaryData{
177-
TotalTests: 9999,
178-
PanickedTests: 0,
179-
RacedTests: 0,
180-
FlakyTests: 1,
181-
// ratio = 1/9999 => ~0.00010001 => rounds to 0.01%
176+
TotalTests: 9999,
177+
PanickedTests: 0,
178+
RacedTests: 0,
179+
FlakyTests: 1,
182180
FlakyTestRatio: "0.01%",
183-
// total runs => (9998 stable * 10 each) + 2 = 99,982
184-
TotalRuns: (9998 * 10) + 2,
185-
// total passes => (9998 stable * 10) + 1 success in flaky = 99,981
186-
PassedRuns: (9998 * 10) + 1,
187-
FailedRuns: 1,
188-
SkippedRuns: 0,
189-
// 1 failure => we do NOT show 100% => we cap at 99.99%
190-
PassRatio: "99.99%",
191-
MaxPassRatio: 1.0,
192-
},
193-
},
194-
{
195-
name: "Tiny flake ratio below threshold",
196-
testResults: func() []TestResult {
197-
// 100,001 total:
198-
// - 100,000 stable => pass=1.0
199-
// - 1 flaky => pass=0.5
200-
const total = 100001
201-
tests := make([]TestResult, total)
202-
for i := 0; i < total-1; i++ {
203-
tests[i] = TestResult{
204-
PassRatio: 1.0,
205-
Runs: 10,
206-
Successes: 10,
207-
}
208-
}
209-
tests[total-1] = TestResult{
210-
PassRatio: 0.5, // 1 success, 1 fail
211-
Runs: 2,
212-
Successes: 1,
213-
Failures: 1,
214-
}
215-
return tests
216-
}(),
217-
maxPassRatio: 1.0,
218-
expected: SummaryData{
219-
TotalTests: 100001,
220-
PanickedTests: 0,
221-
RacedTests: 0,
222-
FlakyTests: 1,
223-
// 1 / 100001 => ~0.000009999 => definitely < 0.01%
224-
FlakyTestRatio: "< 0.01%",
225-
// total runs => (100,000 stable * 10) + 2 = 1,000,002
226-
TotalRuns: (100000 * 10) + 2,
227-
// passes => stable=1,000,000 + 1 success from flaky=1 => 1,000,001
228-
PassedRuns: 100000*10 + 1,
229-
FailedRuns: 1,
230-
SkippedRuns: 0,
231-
// again 1 failure => cap pass ratio => "99.99%"
232-
PassRatio: "99.99%",
233-
MaxPassRatio: 1.0,
181+
TotalRuns: (9998 * 10) + 2,
182+
PassedRuns: (9998 * 10) + 1,
183+
FailedRuns: 1,
184+
SkippedRuns: 0,
185+
PassRatio: "99.999%",
186+
MaxPassRatio: 1.0,
234187
},
235188
},
236189
}

0 commit comments

Comments
 (0)