-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathjunit_update.go
More file actions
114 lines (92 loc) · 3.19 KB
/
junit_update.go
File metadata and controls
114 lines (92 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/xml"
"os"
"strconv"
)
var junitUpdateOldGlob string
var junitUpdateNewGlob string
var junitUpdateOutPath string
const slidingWindowOldWeight = 0.9
// applySlidingWindow applies an exponential moving average to smooth out timing fluctuations
// Uses exponential moving average: oldWeight * old + (1 - oldWeight) * new
// This gives more weight to historical data while still incorporating recent changes
func applySlidingWindow(oldTime, newTime float64) float64 {
return slidingWindowOldWeight*oldTime + (1-slidingWindowOldWeight)*newTime
}
// updateJUnitTimings merges old and new JUnit timings using a sliding window algorithm
func updateJUnitTimings() {
if junitUpdateOldGlob == "" || junitUpdateNewGlob == "" || junitUpdateOutPath == "" {
fatalMsg("junit-update requires -junit-update, -junit-new, and -junit-out flags\n")
}
// Load old timings
oldTimings := loadJUnitTimingsFromGlob(junitUpdateOldGlob)
printMsg("loaded %d test files from old timings\n", len(oldTimings))
// Load new timings
newTimings := loadJUnitTimingsFromGlob(junitUpdateNewGlob)
printMsg("loaded %d test files from new timings\n", len(newTimings))
// Merge timings using sliding window algorithm
mergedTimings := make(map[string]float64)
// Process all tests from new timings
for file, newTime := range newTimings {
oldTime, exists := oldTimings[file]
if exists {
// Test exists in both: apply sliding window
mergedTimings[file] = applySlidingWindow(oldTime, newTime)
} else {
// Test not in old: use new timing
mergedTimings[file] = newTime
}
}
// Tests not in new are automatically excluded (not added to mergedTimings)
printMsg("merged %d test files (removed tests not in new, used sliding window for existing tests)\n", len(mergedTimings))
// Write output JUnit XML
writeJUnitXML(mergedTimings, junitUpdateOutPath)
printMsg("wrote updated timings to %s\n", junitUpdateOutPath)
}
// writeJUnitXML writes test timings to a JUnit XML file
func writeJUnitXML(timings map[string]float64, outputPath string) {
// JUnit XML structure for writing (with testsuite root element)
type testCase struct {
File string `xml:"file,attr"`
Time string `xml:"time,attr"`
}
type testSuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
TestCases []testCase `xml:"testcase"`
}
// Convert map to slice for consistent output
testCases := make([]testCase, 0, len(timings))
for file, time := range timings {
// Format as decimal without scientific notation
timeStr := strconv.FormatFloat(time, 'f', -1, 64)
testCases = append(testCases, testCase{
File: file,
Time: timeStr,
})
}
suite := testSuite{
Name: "rspec",
Tests: len(testCases),
TestCases: testCases,
}
// Create output file
file, err := os.Create(outputPath)
if err != nil {
fatalMsg("failed to create output file: %v\n", err)
}
defer file.Close()
// Write XML header
file.WriteString(xml.Header)
// Create encoder
encoder := xml.NewEncoder(file)
encoder.Indent("", " ")
// Encode XML
err = encoder.Encode(suite)
if err != nil {
fatalMsg("failed to encode JUnit XML: %v\n", err)
}
file.WriteString("\n")
}