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