|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 XCTest |
| 14 | +import _InstructionCounter |
| 15 | + |
| 16 | +extension XCTestCase { |
| 17 | + private var ciBaselineURL: URL { |
| 18 | + return URL(fileURLWithPath: #file) |
| 19 | + .deletingLastPathComponent() |
| 20 | + .appendingPathComponent("ci-baselines.json") |
| 21 | + } |
| 22 | + |
| 23 | + private var baselineURL: URL { |
| 24 | + return URL(fileURLWithPath: #file) |
| 25 | + .deletingLastPathComponent() |
| 26 | + .appendingPathComponent("baselines.json") |
| 27 | + } |
| 28 | + |
| 29 | + func measureInstructions(_ baselineName: StaticString = #function, block: () -> Void, file: StaticString = #file, line: UInt = #line) throws { |
| 30 | + let strippedBaselineName = "\(baselineName)".replacingOccurrences(of: "()", with: "") |
| 31 | + var baseline: UInt64? = try baseline(for: strippedBaselineName) |
| 32 | + |
| 33 | + if let environmentBaseline = ProcessInfo.processInfo.environment[strippedBaselineName] { |
| 34 | + baseline = UInt64(environmentBaseline) |
| 35 | + } |
| 36 | + |
| 37 | + guard let baseline = baseline else { |
| 38 | + XCTFail("Missing baseline for \(baselineName)", file: file, line: line) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + let startInstructions = getInstructionsExecuted() |
| 43 | + block() |
| 44 | + let endInstructions = getInstructionsExecuted() |
| 45 | + let numberOfInstructions = endInstructions - startInstructions |
| 46 | + |
| 47 | + let lowerBaseline = UInt64(Double(baseline) * 0.97) |
| 48 | + let upperBaseline = UInt64(Double(baseline) * 1.03) |
| 49 | + let baselineRange = lowerBaseline...upperBaseline |
| 50 | + |
| 51 | + XCTAssertTrue( |
| 52 | + baselineRange.contains(numberOfInstructions), |
| 53 | + """ |
| 54 | + Number of instructions '\(numberOfInstructions)' |
| 55 | + is not within range \(lowerBaseline)-\(upperBaseline) |
| 56 | + """, |
| 57 | + file: file, |
| 58 | + line: line |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + private func baseline(for key: String) throws -> UInt64? { |
| 63 | + let ciBaselineData = try? Data(contentsOf: ciBaselineURL) |
| 64 | + |
| 65 | + let baselineData = try? Data(contentsOf: baselineURL) |
| 66 | + |
| 67 | + guard let data = baselineData ?? ciBaselineData else { |
| 68 | + fatalError("No baseline.json provided") |
| 69 | + } |
| 70 | + |
| 71 | + let jsonDecoder = JSONDecoder() |
| 72 | + let baselineMap = try jsonDecoder.decode([String: UInt64].self, from: data) |
| 73 | + |
| 74 | + return baselineMap[key] |
| 75 | + } |
| 76 | +} |
0 commit comments