Skip to content

Commit 5ed689d

Browse files
authored
Update the bin/benchmark script to Swift 6 (#1297)
1 parent f0a3e95 commit 5ed689d

File tree

9 files changed

+73
-30
lines changed

9 files changed

+73
-30
lines changed

bin/benchmark/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.5
1+
// swift-tools-version: 6.0
22
/*
33
This source file is part of the Swift.org open source project
44

@@ -14,7 +14,7 @@ import PackageDescription
1414
let package = Package(
1515
name: "benchmark",
1616
platforms: [
17-
.macOS(.v12)
17+
.macOS(.v13)
1818
],
1919
products: [
2020
.executable(

bin/benchmark/Sources/benchmark/BenchmarkResultSeries.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct BenchmarkResultSeries: Codable, Equatable {
7777
/// The list of metrics gathered in these benchmark runs.
7878
public var metrics: [MetricSeries]
7979

80-
static var empty = BenchmarkResultSeries(platformName: "", timestamp: Date(), doccArguments: [], metrics: [])
80+
static let empty = BenchmarkResultSeries(platformName: "", timestamp: Date(), doccArguments: [], metrics: [])
8181

8282
enum Error: Swift.Error, CustomStringConvertible {
8383
case addedResultHasDifferentConfiguration

bin/benchmark/Sources/benchmark/Commands.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import ArgumentParser
1212
import Foundation
1313

1414
@main
15-
struct BenchmarkCommand: ParsableCommand {
16-
static var configuration = CommandConfiguration(
15+
struct BenchmarkCommand: @MainActor ParsableCommand {
16+
@MainActor
17+
static let configuration = CommandConfiguration(
1718
abstract: "A utility for performing benchmarks for Swift-DocC.",
1819
subcommands: [Measure.self, Diff.self, CompareTo.self, MeasureCommits.self, RenderTrend.self],
1920
defaultSubcommand: Measure.self)
2021
}
2122

2223
let doccProjectRootURL: URL = {
23-
let url = URL(fileURLWithPath: #file)
24+
let url = URL(fileURLWithPath: #filePath)
2425
.deletingLastPathComponent() // Commands.swift
2526
.deletingLastPathComponent() // benchmark
2627
.deletingLastPathComponent() // Sources

bin/benchmark/Sources/benchmark/Diff/DiffAnalysis.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extension DiffResults {
2222
}
2323
}
2424

25+
@MainActor
2526
static func analyze(before beforeMetric: BenchmarkResultSeries.MetricSeries?, after afterMetric: BenchmarkResultSeries.MetricSeries) throws -> DiffResults.MetricAnalysis {
2627
guard let beforeMetric else {
2728
return DiffResults.MetricAnalysis(
@@ -129,6 +130,7 @@ extension DiffResults {
129130
)
130131
}
131132

133+
@MainActor
132134
private static func inputBiasDescription(metric: BenchmarkResultSeries.MetricSeries, sampleName: String, numbers: [Double]) -> String {
133135
// Turn the single metric series into an array of single values metric series to render the trend bars.
134136

@@ -164,6 +166,7 @@ extension DiffResults {
164166
}
165167

166168
extension BenchmarkResultSeries.MetricSeries.ValueSeries {
169+
@MainActor
167170
func formatted() -> String {
168171
switch self {
169172
case .duration(let value):
@@ -190,6 +193,7 @@ extension BenchmarkResultSeries.MetricSeries.ValueSeries {
190193
}
191194

192195
#if os(macOS) || os(iOS)
196+
@MainActor
193197
private let durationFormatter: MeasurementFormatter = {
194198
let fmt = MeasurementFormatter()
195199
fmt.unitStyle = .medium

bin/benchmark/Sources/benchmark/Diff/DiffResultsTable.swift

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,51 @@
1111
import Foundation
1212

1313
struct DiffResultsTable {
14-
static var columns: [(name: String, width: Int)] = [
15-
("Metric", 40),
16-
("Change", 15),
17-
("Before", 20),
18-
("After", 20),
19-
]
20-
static var totalWidth: Int {
21-
return columns.reduce(0, { $0 + $1.width + 3 }) - 1
14+
struct Columns {
15+
typealias Column = (name: String, width: Int)
16+
var data: [Column]
17+
18+
init() {
19+
data = [
20+
("Metric", 40),
21+
("Change", 15),
22+
("Before", 20),
23+
("After", 20),
24+
]
25+
}
26+
27+
var beforeInfo: Column {
28+
get { data[2] }
29+
set { data[2] = newValue }
30+
}
31+
32+
var afterInfo: Column {
33+
get { data[3] }
34+
set { data[3] = newValue }
35+
}
36+
37+
var totalWidth: Int {
38+
data.reduce(0, { $0 + $1.width + 3 }) - 1
39+
}
40+
41+
var names: [String] {
42+
data.map { $0.name }
43+
}
2244
}
2345

2446
private(set) var output: String
25-
init(results: DiffResults) {
47+
init(results: DiffResults, columns: Columns) {
2648
var output = ""
2749

2850
let allWarnings = results.analysis.flatMap { $0.warnings ?? [] }
2951
for warning in allWarnings {
3052
output += "\(warning)\n"
3153
}
3254

33-
output += "\(String(repeating: "", count: Self.totalWidth))\n"
34-
output += Self.formattedRow(columnValues: Self.columns.map { $0.name })
35-
output += "\(String(repeating: "", count: Self.totalWidth))\n"
55+
let totalWidth = columns.totalWidth
56+
output += "\(String(repeating: "", count: totalWidth))\n"
57+
output += Self.formattedRow(columns: columns)
58+
output += "\(String(repeating: "", count: totalWidth))\n"
3659

3760
var footnoteCounter = 0
3861

@@ -61,10 +84,16 @@ struct DiffResultsTable {
6184
footnoteCounter += footnotes.count
6285
}
6386

64-
output += Self.formattedRow(columnValues: [analysis.metricName, change, analysis.before ?? "-", analysis.after], colorInfo: colorInfo)
87+
var analysisColumns = columns
88+
analysisColumns.data[0].name = analysis.metricName
89+
analysisColumns.data[1].name = change
90+
analysisColumns.data[2].name = analysis.before ?? "-"
91+
analysisColumns.data[3].name = analysis.after
92+
93+
output += Self.formattedRow(columns: analysisColumns, colorInfo: colorInfo)
6594
}
6695

67-
output += "\(String(repeating: "", count: Self.totalWidth))\n"
96+
output += "\(String(repeating: "", count: totalWidth))\n"
6897

6998
let allFootnotes = results.analysis.flatMap { $0.footnotes ?? [] }
7099
if !allFootnotes.isEmpty {
@@ -117,9 +146,9 @@ struct DiffResultsTable {
117146
let upTo: String.Index
118147
}
119148

120-
private static func formattedRow(columnValues: [String], colorInfo: [ColumnColorInfo] = []) -> String {
121-
let values: [String] = columnValues.enumerated().map { (index, value) in
122-
let row = value.padding(toLength: Self.columns[index].width, withPad: " ", startingAt: 0)
149+
private static func formattedRow(columns: Columns, colorInfo: [ColumnColorInfo] = []) -> String {
150+
let values: [String] = columns.names.enumerated().map { (index, value) in
151+
let row = value.padding(toLength: columns.data[index].width, withPad: " ", startingAt: 0)
123152
if let colorInfo = colorInfo.first(where: { $0.index == index }) {
124153
return String(row[..<colorInfo.upTo]).colored(colorInfo.color) + String(row[colorInfo.upTo...])
125154
}

bin/benchmark/Sources/benchmark/Subcommands/CommitCommands.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Foundation
1212
import ArgumentParser
1313
import SwiftDocC
1414

15-
struct CompareTo: ParsableCommand {
15+
struct CompareTo: @MainActor ParsableCommand {
1616
@Argument(
1717
help: "The baseline 'commit-ish' to compare the current checkout against."
1818
)
@@ -31,6 +31,7 @@ struct CompareTo: ParsableCommand {
3131
return outputDirectory ?? URL(fileURLWithPath: ".") // fallback to the current directory
3232
}
3333

34+
@MainActor
3435
mutating func run() throws {
3536
let currentDocCExecutable = try MeasureAction.buildDocC(at: doccProjectRootURL)
3637
let currentBenchmarkResult = try MeasureAction.gatherMeasurements(

bin/benchmark/Sources/benchmark/Subcommands/Diff.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftDocC
1414

1515
// For argument parsing and validation
1616

17-
struct Diff: ParsableCommand {
17+
struct Diff: @MainActor ParsableCommand {
1818
@Argument(
1919
help: "The benchmark.json file to treat as the 'before' values in the diff.",
2020
transform: { URL(fileURLWithPath: $0) }
@@ -34,6 +34,7 @@ struct Diff: ParsableCommand {
3434
)
3535
var jsonOutputFile: URL?
3636

37+
@MainActor
3738
mutating func run() throws {
3839
try DiffAction(
3940
beforeFile: beforeFile,
@@ -50,6 +51,7 @@ struct DiffAction {
5051
var afterFile: URL
5152
var jsonOutputFile: URL?
5253

54+
@MainActor
5355
func run() throws {
5456
let beforeMetrics = try JSONDecoder().decode(BenchmarkResultSeries.self, from: Data(contentsOf: beforeFile)).metrics
5557
let afterMetrics = try JSONDecoder().decode(BenchmarkResultSeries.self, from: Data(contentsOf: afterFile)).metrics
@@ -62,10 +64,11 @@ struct DiffAction {
6264
try result.analysis.append(DiffResults.analyze(before: beforeMetric, after: afterMetric))
6365
}
6466

65-
DiffResultsTable.columns[2] = tableColumnInfo(file: beforeFile)
66-
DiffResultsTable.columns[3] = tableColumnInfo(file: afterFile)
67+
var columns = DiffResultsTable.Columns()
68+
columns.beforeInfo = tableColumnInfo(file: beforeFile)
69+
columns.afterInfo = tableColumnInfo(file: afterFile)
6770

68-
let table = DiffResultsTable(results: result)
71+
let table = DiffResultsTable(results: result, columns: columns)
6972
print(table.output)
7073

7174
if let jsonOutputFile {

bin/benchmark/Sources/benchmark/Subcommands/Measure.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct MeasureOptions: ParsableCommand {
5656
}
5757
}
5858

59-
struct Measure: ParsableCommand {
59+
struct Measure: @MainActor ParsableCommand {
6060
@Option(
6161
name: .customLong("output"),
6262
help: "The path to write the output benchmark measurements file.",
@@ -93,6 +93,7 @@ struct Measure: ParsableCommand {
9393
}
9494
}
9595

96+
@MainActor
9697
mutating func run() throws {
9798
try MeasureAction(
9899
repeatCount: measureOptions.repeatCount,
@@ -111,6 +112,7 @@ struct MeasureAction {
111112
var doccConvertCommand: [String]
112113
var computeMissingOutputSizeMetrics: Bool
113114

115+
@MainActor
114116
func run() throws {
115117
print("Building docc in release configuration".styled(.bold))
116118
let doccURL = try Self.buildDocC(at: doccProjectRootURL)

bin/benchmark/Sources/benchmark/Subcommands/RenderTrend.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftDocC
1414

1515
// For argument parsing and validation
1616

17-
struct RenderTrend: ParsableCommand {
17+
struct RenderTrend: @MainActor ParsableCommand {
1818
@Option(
1919
name: .customLong("filter"),
2020
help: "A list metric IDs to render trends for. If no filters are specified, trends are rendered for all numeric metrics."
@@ -27,6 +27,7 @@ struct RenderTrend: ParsableCommand {
2727
)
2828
var benchmarkResults: [URL]
2929

30+
@MainActor
3031
mutating func run() throws {
3132
try RenderTrendAction(
3233
metricFilters: metricFilters,
@@ -41,6 +42,7 @@ struct RenderTrendAction {
4142
var metricFilters: [String]
4243
var benchmarkResults: [URL]
4344

45+
@MainActor
4446
func run() throws {
4547
// Map the metric ID to the measured values across the benchmarks
4648
var trendValues: [String: [BenchmarkResultSeries.MetricSeries]] = [:]
@@ -76,6 +78,7 @@ struct RenderTrendAction {
7678
}
7779
}
7880

81+
@MainActor
7982
static func renderTrendBars(metrics: [BenchmarkResultSeries.MetricSeries]) -> String {
8083
var output = ""
8184

0 commit comments

Comments
 (0)