-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathTestExtensions.swift
238 lines (214 loc) · 7.06 KB
/
TestExtensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftExtensions
import Synchronization
import XCTest
#if compiler(>=6)
package import CompletionScoring
#else
import CompletionScoring
#endif
@inline(never)
package func drain<T>(_ value: T) {}
func duration(of body: () -> ()) -> TimeInterval {
let start = ProcessInfo.processInfo.systemUptime
body()
return ProcessInfo.processInfo.systemUptime - start
}
extension RandomNumberGenerator {
mutating func nextBool() -> Bool {
(next() & 0x01 == 0x01)
}
}
package func withEachPermutation<T>(_ a: T, _ b: T, body: (T, T) -> ()) {
body(a, b)
body(b, a)
}
extension XCTestCase {
private func heatUp() {
var integers = 1024
var elapsed = 0.0
while elapsed < 1.0 {
elapsed += duration {
let integers = Array(count: integers) {
UInt64.random(in: 0...UInt64.max)
}
DispatchQueue.concurrentPerform(iterations: 128) { _ in
integers.withUnsafeBytes { bytes in
var hasher = Hasher()
hasher.combine(bytes: bytes)
drain(hasher.finalize())
}
}
}
integers *= 2
}
}
private func coolDown() {
Thread.sleep(forTimeInterval: 2.0)
}
#if canImport(Darwin)
func induceThermalRange(_ range: ClosedRange<Int>) {
var temperature: Int
repeat {
temperature = ProcessInfo.processInfo.thermalLevel()
if temperature < range.lowerBound {
print("Too Cold: \(temperature)")
heatUp()
} else if temperature > range.upperBound {
print("Too Hot: \(temperature)")
coolDown()
}
} while !range.contains(temperature)
}
private static let targetThermalRange: ClosedRange<Int>? = {
if ProcessInfo.processInfo.environment["SOURCEKIT_LSP_PERFORMANCE_MEASUREMENTS_ENABLE_THERMAL_THROTTLING"]
!= nil
{
// This range is arbitrary. All that matters is that the same values are used on the baseline and the branch.
let target =
ProcessInfo.processInfo.environment["SOURCEKIT_LSP_PERFORMANCE_MEASUREMENTS_THERMAL_TARGET"].flatMap(
Int.init
)
?? 75
let variance =
ProcessInfo.processInfo.environment["SOURCEKIT_LSP_PERFORMANCE_MEASUREMENTS_THERMAL_VARIANCE"].flatMap(
Int.init
)
?? 5
return (target - variance)...(target + variance)
} else {
return nil
}
}()
#endif
private static let measurementsLogFile: String? = {
UserDefaults.standard.string(forKey: "TestMeasurementLogPath")
}()
static let printBeginingOfLog: Atomic<Bool> = Atomic(true)
private static func openPerformanceLog() throws -> FileHandle? {
try measurementsLogFile.map { path in
if !FileManager.default.fileExists(atPath: path) {
try FileManager.default.createDirectory(
at: URL(fileURLWithPath: path).deletingLastPathComponent(),
withIntermediateDirectories: true
)
FileManager.default.createFile(atPath: path, contents: Data())
}
let logFD = try FileHandle(forWritingAtPath: path).unwrap(orThrow: "Opening \(path) failed")
try logFD.seekToEnd()
if printBeginingOfLog.load(ordering: .sequentiallyConsistent) {
try logFD.print("========= \(Date().description(with: .current)) =========")
printBeginingOfLog.store(false, ordering: .sequentiallyConsistent)
}
return logFD
}
}
func tryOrFailTest<R>(_ expression: @autoclosure () throws -> R?, message: String) -> R? {
do {
return try expression()
} catch {
XCTFail("\(message): \(error)")
return nil
}
}
/// Run `body()` `iterations`, gathering timing stats, and print them.
/// In between runs, coax for the machine into an arbitrary but consistent thermal state by either sleeping or doing
/// pointless work so that results are more comparable run to run, no matter else is happening on the machine.
package func gaugeTiming(iterations: Int = 1, testName: String = #function, _ body: () -> ()) {
let logFD = tryOrFailTest(try Self.openPerformanceLog(), message: "Failed to open performance log")
var timings = Timings()
for iteration in 0..<iterations {
#if canImport(Darwin)
if let targetThermalRange = Self.targetThermalRange {
induceThermalRange(targetThermalRange)
}
let thermalLevel = ProcessInfo.processInfo.thermalLevel()
#else
let thermalLevel = "unknown"
#endif
let duration = duration(of: body)
let stats = timings.append(duration)
let confidence = timings.confidenceOfMean_95Percent
let confidencePercentOfAverage = confidence / stats.average * 100
let consoleMessage =
"""
[\(iteration.format("%4ld"))]: \
current: \(duration.format("%0.4f")), \
min: \(stats.min.format("%0.4f")), \
max: \(stats.max.format("%0.4f")), \
avg: \(stats.average.format("%0.4f")), \
confidence:±\(confidencePercentOfAverage.format("%0.2f"))% \
thermal: \(thermalLevel)
"""
let logMessage = "\(testName.prefix(while: \.isLetter)),\(duration.format("%0.4f")),\(thermalLevel)"
print(consoleMessage)
tryOrFailTest(try logFD?.print(logMessage), message: "Failed to write to log")
}
}
}
#if canImport(Darwin)
extension ProcessInfo {
func thermalLevel() -> Int {
var thermalLevel: UInt64 = 0
var size: size_t = MemoryLayout<UInt64>.size
sysctlbyname("machdep.xcpm.cpu_thermal_level", &thermalLevel, &size, nil, 0)
return Int(thermalLevel)
}
}
#endif
extension String {
fileprivate func dropSuffix(_ suffix: String) -> String {
if hasSuffix(suffix) {
return String(dropLast(suffix.count))
}
return self
}
fileprivate func dropPrefix(_ prefix: String) -> String {
if hasPrefix(prefix) {
return String(dropFirst(prefix.count))
}
return self
}
package func allocateCopyOfUTF8Buffer() -> CompletionScoring.Pattern.UTF8Bytes {
withUncachedUTF8Bytes { utf8Buffer in
UnsafeBufferPointer.allocate(copyOf: utf8Buffer)
}
}
}
extension FileHandle {
func write(_ text: String) throws {
try text.withUncachedUTF8Bytes { bytes in
try write(contentsOf: bytes)
}
}
func print(_ text: String) throws {
try write(text)
try write("\n")
}
}
extension Double {
func format(_ specifier: StringLiteralType) -> String {
String(format: specifier, self)
}
}
extension Int {
func format(_ specifier: StringLiteralType) -> String {
String(format: specifier, self)
}
}
extension CandidateBatch {
package init(symbols: [String]) {
self.init(candidates: symbols, contentType: .codeCompletionSymbol)
}
}