Skip to content

Commit c7365a5

Browse files
committed
Add performance measurement with instructions count
1 parent c808498 commit c7365a5

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ Package.resolved
1818

1919
.DS_Store
2020
*.pyc
21+
22+
Tests/PerformanceTest/baselines.json

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ let package = Package(
283283

284284
.testTarget(
285285
name: "PerformanceTest",
286-
dependencies: ["SwiftIDEUtils", "SwiftParser", "SwiftSyntax"],
287-
exclude: ["Inputs"]
286+
dependencies: ["_InstructionCounter", "SwiftIDEUtils", "SwiftParser", "SwiftSyntax"],
287+
exclude: ["Inputs", "ci-baselines.json"]
288288
),
289289
]
290290
)

Tests/PerformanceTest/ParsingPerformanceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ParsingPerformanceTests: XCTestCase {
2525

2626
func testNativeParsingPerformance() throws {
2727
try XCTSkipIf(ProcessInfo.processInfo.environment["SKIP_LONG_TESTS"] == "1")
28-
measure {
28+
try measureInstructions {
2929
do {
3030
let source = try String(contentsOf: inputFile)
3131
_ = Parser.parse(source: source)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"testNativeParsingPerformance": 5850081534
3+
}

0 commit comments

Comments
 (0)