Skip to content

Commit d945522

Browse files
committed
Refactor SampleRunner into POSIX and Darwin versions
1 parent 7980623 commit d945522

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,41 +230,29 @@ func stopTrackingObjects(_: UnsafeMutableRawPointer) -> Int
230230

231231
#endif
232232

233-
class SampleRunner {
234-
#if !os(Linux)
235-
var info = mach_timebase_info_data_t(numer: 0, denom: 0)
236-
#endif
237-
init() {
238-
#if !os(Linux)
239-
mach_timebase_info(&info)
240-
#endif
241-
}
233+
protocol SampleRunner {
234+
func run(_ name: String, fn: (Int) -> Void, num_iters: UInt) -> UInt64
235+
}
236+
237+
#if os(Linux)
238+
class POSIXSampleRunner: SampleRunner {
242239
func run(_ name: String, fn: (Int) -> Void, num_iters: UInt) -> UInt64 {
243240
// Start the timer.
244241
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
245242
var str = name
246243
startTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
247244
#endif
248-
#if os(Linux)
249245
var start_ticks = timespec(tv_sec: 0, tv_nsec: 0)
250246
clock_gettime(CLOCK_REALTIME, &start_ticks)
251-
#else
252-
let start_ticks = mach_absolute_time()
253-
#endif
254247
fn(Int(num_iters))
255248
// Stop the timer.
256-
#if os(Linux)
257249
var end_ticks = timespec(tv_sec: 0, tv_nsec: 0)
258250
clock_gettime(CLOCK_REALTIME, &end_ticks)
259-
#else
260-
let end_ticks = mach_absolute_time()
261-
#endif
262251
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
263252
stopTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
264253
#endif
265254

266255
// Compute the spent time and the scaling factor.
267-
#if os(Linux)
268256
var elapsed_ticks = timespec(tv_sec: 0, tv_nsec: 0)
269257
if end_ticks.tv_nsec - start_ticks.tv_nsec < 0 {
270258
elapsed_ticks.tv_sec = end_ticks.tv_sec - start_ticks.tv_sec - 1
@@ -274,12 +262,34 @@ class SampleRunner {
274262
elapsed_ticks.tv_nsec = end_ticks.tv_nsec - start_ticks.tv_nsec
275263
}
276264
return UInt64(elapsed_ticks.tv_sec) * UInt64(1000000000) + UInt64(elapsed_ticks.tv_nsec)
265+
}
266+
}
277267
#else
268+
class DarwinSampleRunner: SampleRunner {
269+
var info = mach_timebase_info_data_t(numer: 0, denom: 0)
270+
init() {
271+
mach_timebase_info(&info)
272+
}
273+
func run(_ name: String, fn: (Int) -> Void, num_iters: UInt) -> UInt64 {
274+
// Start the timer.
275+
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
276+
var str = name
277+
startTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
278+
#endif
279+
let start_ticks = mach_absolute_time()
280+
fn(Int(num_iters))
281+
// Stop the timer.
282+
let end_ticks = mach_absolute_time()
283+
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
284+
stopTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
285+
#endif
286+
287+
// Compute the spent time and the scaling factor.
278288
let elapsed_ticks = end_ticks - start_ticks
279289
return elapsed_ticks * UInt64(info.numer) / UInt64(info.denom)
280-
#endif
281290
}
282291
}
292+
#endif
283293

284294
/// Invoke the benchmark entry point and return the run time in milliseconds.
285295
func runBench(_ name: String, _ fn: (Int) -> Void, _ c: TestConfig) -> BenchResults {
@@ -290,7 +300,11 @@ func runBench(_ name: String, _ fn: (Int) -> Void, _ c: TestConfig) -> BenchResu
290300
print("Running \(name) for \(c.numSamples) samples.")
291301
}
292302

293-
let sampler = SampleRunner()
303+
#if os(Linux)
304+
let sampler:SampleRunner = POSIXSampleRunner()
305+
#else
306+
let sampler:SampleRunner = DarwinSampleRunner()
307+
#endif
294308
for s in 0..<c.numSamples {
295309
let time_per_sample: UInt64 = 1_000_000_000 * UInt64(c.iterationScale)
296310

0 commit comments

Comments
 (0)