|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +/// The results of a single benchmark run. |
| 14 | +public struct BenchmarkResults: Codable { |
| 15 | + /// The name of the platform where the benchmark ran. |
| 16 | + public let platformName: String |
| 17 | + |
| 18 | + /// The timestamp for when the benchmark started. |
| 19 | + public let timestamp: Date |
| 20 | + |
| 21 | + /// The arguments that Swift-DocC ran with when gathering benchmark data. |
| 22 | + public let doccArguments: [String] |
| 23 | + |
| 24 | + /// Creates a new benchmark result to gather measurement results into. |
| 25 | + /// |
| 26 | + /// - Parameters: |
| 27 | + /// - platformName: The name of the platform that the benchmark ran on. |
| 28 | + /// - timestamp: The timestamp when benchmark started. |
| 29 | + /// - doccArguments: The arguments that Swift-DocC ran with when gathering benchmark data. |
| 30 | + /// - unorderedMetrics: A list of unordered metrics for this benchmark. |
| 31 | + public init( |
| 32 | + platformName: String, |
| 33 | + timestamp: Date = Date(), |
| 34 | + doccArguments: [String] = Array(CommandLine.arguments.dropFirst()), |
| 35 | + unorderedMetrics: [Metric] |
| 36 | + ) { |
| 37 | + self.platformName = platformName |
| 38 | + self.timestamp = timestamp |
| 39 | + self.doccArguments = doccArguments |
| 40 | + self.metrics = Self.sortedMetrics(unorderedMetrics) |
| 41 | + } |
| 42 | + |
| 43 | + /// A private convenience method for sorting metrics in a stable order based on their |
| 44 | + fileprivate static func sortedMetrics(_ unorderedMetrics: [Metric]) -> [Metric] { |
| 45 | + // Sort by value type and then by name |
| 46 | + return unorderedMetrics.sorted { (lhs, rhs) in |
| 47 | + if lhs.value.kindSortOrder == rhs.value.kindSortOrder { |
| 48 | + return lhs.displayName < rhs.displayName |
| 49 | + } else { |
| 50 | + return lhs.value.kindSortOrder < rhs.value.kindSortOrder |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// The list of metrics gathered in this benchmark. |
| 56 | + /// |
| 57 | + /// - Note: The metrics are sorted based on presentation priority. |
| 58 | + public var metrics: [Metric] |
| 59 | + |
| 60 | + /// A gathered metric. |
| 61 | + public struct Metric: Codable, Equatable { |
| 62 | + /// The ID for this gathered metric. |
| 63 | + /// |
| 64 | + /// Use the ID to match up metrics across benchmarks to compare results. |
| 65 | + public var id: String |
| 66 | + /// The name that describe this gathered metric. Suitable for presentation. |
| 67 | + public var displayName: String |
| 68 | + /// The gathered value for this metric. |
| 69 | + public var value: Value |
| 70 | + |
| 71 | + /// Creates a new metric to represent gathered measurements. |
| 72 | + /// - Parameters: |
| 73 | + /// - id: The ID for this gathered metric. |
| 74 | + /// - displayName: The name that describe this gathered metric. |
| 75 | + /// - value: The gathered value for this metric. |
| 76 | + public init(id: String, displayName: String, value: BenchmarkResults.Metric.Value) { |
| 77 | + self.id = id |
| 78 | + self.displayName = displayName |
| 79 | + self.value = value |
| 80 | + } |
| 81 | + |
| 82 | + /// The gathered value for a metric. |
| 83 | + public enum Value: Codable, Equatable { |
| 84 | + /// A duration in seconds. |
| 85 | + case duration(Double) |
| 86 | + /// A number of bytes for a memory measurement. |
| 87 | + case bytesInMemory(Int64) |
| 88 | + /// A number of bytes for a storage measurement. |
| 89 | + case bytesOnDisk(Int64) |
| 90 | + /// A checksum. |
| 91 | + case checksum(String) |
| 92 | + |
| 93 | + enum CodingKeys: CodingKey { |
| 94 | + case type, value |
| 95 | + } |
| 96 | + private enum ValueKind: String, Codable { |
| 97 | + case duration, bytesInMemory, bytesOnDisk, checksum |
| 98 | + } |
| 99 | + |
| 100 | + public init(from decoder: Decoder) throws { |
| 101 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 102 | + |
| 103 | + switch try container.decode(ValueKind.self, forKey: .type) { |
| 104 | + case .bytesInMemory: |
| 105 | + self = try .bytesInMemory(container.decode(Int64.self, forKey: .value)) |
| 106 | + case .bytesOnDisk: |
| 107 | + self = try .bytesOnDisk(container.decode(Int64.self, forKey: .value)) |
| 108 | + case .duration: |
| 109 | + self = try .duration(container.decode(Double.self, forKey: .value)) |
| 110 | + case .checksum: |
| 111 | + self = try .checksum(container.decode(String.self, forKey: .value)) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public func encode(to encoder: Encoder) throws { |
| 116 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 117 | + |
| 118 | + switch self { |
| 119 | + case .bytesInMemory(let value): |
| 120 | + try container.encode(ValueKind.bytesInMemory, forKey: .type) |
| 121 | + try container.encode(value, forKey: .value) |
| 122 | + case .bytesOnDisk(let value): |
| 123 | + try container.encode(ValueKind.bytesOnDisk, forKey: .type) |
| 124 | + try container.encode(value, forKey: .value) |
| 125 | + case .duration(let value): |
| 126 | + try container.encode(ValueKind.duration, forKey: .type) |
| 127 | + try container.encode(value, forKey: .value) |
| 128 | + case .checksum(let value): |
| 129 | + try container.encode(ValueKind.checksum, forKey: .type) |
| 130 | + try container.encode(value, forKey: .value) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// MARK: Legacy format |
| 138 | + |
| 139 | +extension BenchmarkResults { |
| 140 | + private enum LegacyCodingKeys: String, CodingKey { |
| 141 | + case platform, date, arguments, metrics |
| 142 | + } |
| 143 | + |
| 144 | + private enum LegacyMetricCodingKeys: String, CodingKey { |
| 145 | + case identifier, displayName, result |
| 146 | + } |
| 147 | + |
| 148 | + public enum CodingKeys: CodingKey { |
| 149 | + case platformName, timestamp, doccArguments, metrics |
| 150 | + } |
| 151 | + |
| 152 | + public init(from decoder: Decoder) throws { |
| 153 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 154 | + |
| 155 | + if container.contains(.platformName) { |
| 156 | + self.platformName = try container.decode(String.self, forKey: .platformName) |
| 157 | + self.timestamp = try container.decode(Date.self, forKey: .timestamp) |
| 158 | + self.doccArguments = try container.decode([String].self, forKey: .doccArguments) |
| 159 | + self.metrics = try container.decode([Metric].self, forKey: .metrics) |
| 160 | + } else { |
| 161 | + // Legacy format |
| 162 | + let container = try decoder.container(keyedBy: LegacyCodingKeys.self) |
| 163 | + |
| 164 | + self.platformName = try container.decode(String.self, forKey: .platform) |
| 165 | + |
| 166 | + let dateFormatter = DateFormatter() |
| 167 | + dateFormatter.dateStyle = .medium |
| 168 | + dateFormatter.timeStyle = .medium |
| 169 | + guard let date = try dateFormatter.date(from: container.decode(String.self, forKey: .date)) else { |
| 170 | + throw DecodingError.dataCorruptedError(forKey: .date, in: container, debugDescription: "Unable to decode benchmark Date value from legacy benchmark.json format.") |
| 171 | + } |
| 172 | + |
| 173 | + self.timestamp = date |
| 174 | + self.doccArguments = try container.decode([String].self, forKey: .arguments) |
| 175 | + |
| 176 | + var metricsContainer = try container.nestedUnkeyedContainer(forKey: .metrics) |
| 177 | + var unsortedMetrics: [Metric] = [] |
| 178 | + if let containerCount = metricsContainer.count { |
| 179 | + unsortedMetrics.reserveCapacity(containerCount) |
| 180 | + } |
| 181 | + while !metricsContainer.isAtEnd { |
| 182 | + let metricContainer = try metricsContainer.nestedContainer(keyedBy: LegacyMetricCodingKeys.self) |
| 183 | + let id = try metricContainer.decode(String.self, forKey: .identifier) |
| 184 | + let name = try metricContainer.decode(String.self, forKey: .displayName) |
| 185 | + |
| 186 | + if name.hasSuffix(" (msec)") { |
| 187 | + let value = try metricContainer.decode(Double.self, forKey: .result) |
| 188 | + unsortedMetrics.append(.init(id: id, displayName: String(name.dropLast(7)), value: .duration(value / 1000.0))) |
| 189 | + continue |
| 190 | + } else if name.hasSuffix("memory footprint (bytes)") { |
| 191 | + let value = try metricContainer.decode(Int64.self, forKey: .result) |
| 192 | + unsortedMetrics.append(.init(id: id, displayName: String(name.dropLast(8)), value: .bytesInMemory(value))) |
| 193 | + continue |
| 194 | + } else if name.hasSuffix(" (bytes)") { |
| 195 | + let value = try metricContainer.decode(Int64.self, forKey: .result) |
| 196 | + unsortedMetrics.append(.init(id: id, displayName: String(name.dropLast(8)), value: .bytesOnDisk(value))) |
| 197 | + continue |
| 198 | + } else { |
| 199 | + let value = try metricContainer.decode(String.self, forKey: .result) |
| 200 | + unsortedMetrics.append(.init(id: id, displayName: name, value: .checksum(value))) |
| 201 | + continue |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + self.metrics = Self.sortedMetrics(unsortedMetrics) |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +private extension BenchmarkResults.Metric.Value { |
| 211 | + var kindSortOrder: Int { |
| 212 | + switch self { |
| 213 | + case .duration: |
| 214 | + return 0 |
| 215 | + case .bytesInMemory: |
| 216 | + return 1 |
| 217 | + case .bytesOnDisk: |
| 218 | + return 2 |
| 219 | + case .checksum: |
| 220 | + return 3 |
| 221 | + } |
| 222 | + } |
| 223 | +} |
| 224 | + |
0 commit comments