|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | + |
| 15 | +func printToStderr(_ value: String) { |
| 16 | + fputs(value, stderr) |
| 17 | +} |
| 18 | + |
| 19 | +extension Double { |
| 20 | + func round(toDecimalDigits decimalDigits: Int) -> Double { |
| 21 | + return (self * pow(10, Double(decimalDigits))).rounded() / pow(10, Double(decimalDigits)) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Given a performance measurement output, extract the actual measurement into a dictionary. |
| 26 | +/// |
| 27 | +/// We expect every measurement to be on its own line with the name of the measurement on the left side of a colon and |
| 28 | +/// the measurement on the right side of the colon. |
| 29 | +/// |
| 30 | +/// For example, the output could be: |
| 31 | +/// ``` |
| 32 | +/// Instructions executed for test case A: 123456789 |
| 33 | +/// Instructions executed for test case B: 2345678 |
| 34 | +/// Code Size: 34567 |
| 35 | +/// ``` |
| 36 | +func extractMeasurements(output: String) -> [String: Double] { |
| 37 | + var measurements: [String: Double] = [:] |
| 38 | + for line in output.split(separator: "\n") { |
| 39 | + guard let colonPosition = line.lastIndex(of: ":") else { |
| 40 | + printToStderr( |
| 41 | + "Ignoring following measurement line because it doesn't contain a colon: \(line)" |
| 42 | + ) |
| 43 | + continue |
| 44 | + } |
| 45 | + let beforeColon = String(line[..<colonPosition]).trimmingCharacters(in: .whitespacesAndNewlines) |
| 46 | + let afterColon = String(line[line.index(after: colonPosition)...]).trimmingCharacters( |
| 47 | + in: .whitespacesAndNewlines |
| 48 | + ) |
| 49 | + guard let value = Double(afterColon) else { |
| 50 | + printToStderr( |
| 51 | + "Ignoring following measurement line because the value can't be parsed as a Double: \(line)" |
| 52 | + ) |
| 53 | + continue |
| 54 | + } |
| 55 | + measurements[beforeColon] = value |
| 56 | + } |
| 57 | + return measurements |
| 58 | +} |
| 59 | + |
| 60 | +func run( |
| 61 | + baselinePerformanceOutput: String, |
| 62 | + changedPerformanceOutput: String, |
| 63 | + sensitivityPercentage: Double |
| 64 | +) -> (output: String, hasDetectedSignificantChange: Bool) { |
| 65 | + let baselineMeasurements = extractMeasurements(output: baselinePerformanceOutput) |
| 66 | + let changedMeasurements = extractMeasurements(output: changedPerformanceOutput) |
| 67 | + |
| 68 | + var hasDetectedSignificantChange = false |
| 69 | + var output = "" |
| 70 | + for (measurementName, baselineValue) in baselineMeasurements.sorted(by: { $0.key < $1.key }) { |
| 71 | + guard let changedValue = changedMeasurements[measurementName] else { |
| 72 | + output += "🛑 \(measurementName) not present after changes\n" |
| 73 | + continue |
| 74 | + } |
| 75 | + let differencePercentage = (changedValue - baselineValue) / baselineValue * 100 |
| 76 | + let rawMeasurementsText = "(baseline: \(baselineValue), after changes: \(changedValue))" |
| 77 | + if differencePercentage < -sensitivityPercentage { |
| 78 | + output += |
| 79 | + "🎉 \(measurementName) improved by \(-differencePercentage.round(toDecimalDigits: 3))% \(rawMeasurementsText)\n" |
| 80 | + hasDetectedSignificantChange = true |
| 81 | + } else if differencePercentage > sensitivityPercentage { |
| 82 | + output += |
| 83 | + "⚠️ \(measurementName) regressed by \(differencePercentage.round(toDecimalDigits: 3))% \(rawMeasurementsText)\n" |
| 84 | + hasDetectedSignificantChange = true |
| 85 | + } else { |
| 86 | + output += |
| 87 | + "➡️ \(measurementName) did not change significantly with \(differencePercentage.round(toDecimalDigits: 3))% \(rawMeasurementsText)\n" |
| 88 | + } |
| 89 | + } |
| 90 | + return (output, hasDetectedSignificantChange) |
| 91 | +} |
| 92 | + |
| 93 | +guard CommandLine.arguments.count > 3 else { |
| 94 | + printToStderr( |
| 95 | + """ |
| 96 | + Usage: compare-performance-measurements.swift <BASELINE> <WITH_CHANGES> <SENSITIVITY> |
| 97 | +
|
| 98 | + Where BASELINE and WITH_CHANGES are strings containing the performance measurements, with each measurement on a |
| 99 | + separate line with the name of the measurement on the left side of a colon and the measurement on the right side of |
| 100 | + the colon. |
| 101 | +
|
| 102 | + For example, the baseline could be. |
| 103 | + ``` |
| 104 | + Instructions executed for test case A: 123456789 |
| 105 | + Instructions executed for test case B: 2345678 |
| 106 | + Code Size: 34567 |
| 107 | + ``` |
| 108 | +
|
| 109 | + Sensitivity is the percentage after which a change should be considered meaningful. Eg. specify 0.5 to report a |
| 110 | + significant performance change if any of the measurements changed by more than 0.5% (either improved or regressed). |
| 111 | + """ |
| 112 | + ) |
| 113 | + exit(1) |
| 114 | +} |
| 115 | + |
| 116 | +let baselinePerformanceOutput = CommandLine.arguments[1] |
| 117 | +let changedPerformanceOutput = CommandLine.arguments[2] |
| 118 | +let sensitivityPercentage = Double(CommandLine.arguments[3]) |
| 119 | + |
| 120 | +guard let sensitivityPercentage else { |
| 121 | + printToStderr("Sensitivity was not a valid Double value") |
| 122 | + exit(1) |
| 123 | +} |
| 124 | + |
| 125 | +let (output, hasDetectedSignificantChange) = run( |
| 126 | + baselinePerformanceOutput: baselinePerformanceOutput, |
| 127 | + changedPerformanceOutput: changedPerformanceOutput, |
| 128 | + sensitivityPercentage: sensitivityPercentage |
| 129 | +) |
| 130 | + |
| 131 | +print(output) |
| 132 | +if hasDetectedSignificantChange { |
| 133 | + exit(1) |
| 134 | +} |
0 commit comments