Skip to content

Commit

Permalink
Make time formatter produce uniform decimals (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
regexident authored Nov 4, 2020
1 parent 7663e48 commit 8e0ef8b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 38 deletions.
8 changes: 5 additions & 3 deletions Sources/Benchmark/BenchmarkColumn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public struct BenchmarkColumn: Hashable {
case .inverseTime:
self.formatter = BenchmarkFormatter.inverseTime
case .none:
self.formatter = BenchmarkFormatter.number
self.formatter = BenchmarkFormatter.real
}
}
}
Expand Down Expand Up @@ -98,7 +98,8 @@ public struct BenchmarkColumn: Hashable {
formatter: BenchmarkFormatter.stdPercentage)
result["iterations"] = BenchmarkColumn(
name: "iterations",
value: { Double($0.measurements.count) })
value: { Double($0.measurements.count) },
formatter: BenchmarkFormatter.integer)
result["warmup"] = BenchmarkColumn(
name: "warmup",
value: { $0.warmupMeasurements.sum },
Expand Down Expand Up @@ -208,7 +209,8 @@ public struct BenchmarkColumn: Hashable {
return 0
}
},
alignment: .right))
alignment: .right,
formatter: BenchmarkFormatter.integer))
}

return columns
Expand Down
22 changes: 11 additions & 11 deletions Sources/Benchmark/BenchmarkFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@
public enum BenchmarkFormatter {
public typealias Formatter = (Double, BenchmarkSettings) -> String

/// Just show a number, stripping ".0" if it's integer.
public static let number: Formatter = { (value, settings) in
let string = String(value)
if string.hasSuffix(".0") {
return String(string.dropLast(2))
} else {
return String(format: "%.3f", value)
}
/// Show an integer number without decimals.
public static let integer: Formatter = { (value, settings) in
return String(format: "%.0f", value)
}

/// Show a real number with decimals.
public static let real: Formatter = { (value, settings) in
return String(format: "%.3f", value)
}

/// Show number with the corresponding time unit.
public static let time: Formatter = { (value, settings) in
let num = number(value, settings)
let num = real(value, settings)
return "\(num) \(settings.timeUnit)"
}

/// Show number with the corresponding inverse time unit.
public static let inverseTime: Formatter = { (value, settings) in
let num = number(value, settings)
let num = real(value, settings)
return "\(num) /\(settings.inverseTimeUnit)"
}

Expand All @@ -45,7 +45,7 @@ public enum BenchmarkFormatter {

/// Show value as plus or minus standard deviation.
public static let std: Formatter = { (value, settings) in
let num = number(value, settings)
let num = real(value, settings)
return "± \(num)"
}

Expand Down
15 changes: 14 additions & 1 deletion Sources/Benchmark/BenchmarkSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,24 @@ public struct TimeUnit: BenchmarkSetting {
public init(_ value: Value) {
self.value = value
}
public enum Value: String, ExpressibleByArgument {
public enum Value: String, ExpressibleByArgument, CustomStringConvertible {
case ns
case us
case ms
case s

public var description: String {
switch self {
case .ns:
return "ns"
case .us:
return "us"
case .ms:
return "ms"
case .s:
return " s"
}
}
}
}

Expand Down
51 changes: 28 additions & 23 deletions Tests/BenchmarkTests/BenchmarkReporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,18 @@ final class BenchmarkReporterTests: XCTestCase {
file: StaticString = #filePath,
line: UInt = #line
) {
func trimmingTrailingWhitespace(_ string: String) -> String {
String(string.reversed().drop(while: { $0.isWhitespace }).reversed())
}
let lines = Array(got.split(separator: "\n").map { String($0) })
let expectedLines = expected.split(separator: "\n").map { String($0) }
let actual = lines.map { $0.trimmingCharacters(in: .newlines) }
.filter { !$0.isEmpty }
XCTAssertEqual(expectedLines.count, actual.count, message(), file: file, line: line)
for (expectedLine, actualLine) in zip(expectedLines, actual) {
XCTAssertEqual(expectedLine, actualLine, message(), file: file, line: line)
let trimmedExpectedLine = trimmingTrailingWhitespace(expectedLine)
let trimmedActualLine = trimmingTrailingWhitespace(actualLine)
XCTAssertEqual(trimmedExpectedLine, trimmedActualLine, message(), file: file, line: line)
}
}

Expand All @@ -89,10 +94,10 @@ final class BenchmarkReporterTests: XCTestCase {
counters: [:]),
]
let expected = #"""
name time std iterations
---------------------------------------------
MySuite.fast 1500 ns ± 47.14 % 2
MySuite.slow 1500000 ns ± 47.14 % 2
name time std iterations
-------------------------------------------------
MySuite.fast 1500.000 ns ± 47.14 % 2
MySuite.slow 1500000.000 ns ± 47.14 % 2
"""#
assertConsoleReported(results, expected)
}
Expand All @@ -113,10 +118,10 @@ final class BenchmarkReporterTests: XCTestCase {
counters: [:]),
]
let expected = #"""
name time std iterations foo
-------------------------------------------------
MySuite.fast 1500 ns ± 47.14 % 2 7
MySuite.slow 1500000 ns ± 47.14 % 2 0
name time std iterations foo
-----------------------------------------------------
MySuite.fast 1500.000 ns ± 47.14 % 2 7
MySuite.slow 1500000.000 ns ± 47.14 % 2 0
"""#
assertConsoleReported(results, expected)
}
Expand All @@ -137,10 +142,10 @@ final class BenchmarkReporterTests: XCTestCase {
counters: [:]),
]
let expected = #"""
name time std iterations warmup
----------------------------------------------------
MySuite.fast 1500 ns ± 47.14 % 2 60 ns
MySuite.slow 1500000 ns ± 47.14 % 2 0 ns
name time std iterations warmup
-----------------------------------------------------------
MySuite.fast 1500.000 ns ± 47.14 % 2 60.000 ns
MySuite.slow 1500000.000 ns ± 47.14 % 2 0.000 ns
"""#
assertConsoleReported(results, expected)
}
Expand Down Expand Up @@ -173,12 +178,12 @@ final class BenchmarkReporterTests: XCTestCase {
counters: [:]),
]
let expected = #"""
name time std iterations
----------------------------------------------
MySuite.ns 123456789 ns ± 0.00 % 1
MySuite.us 123456.789 us ± 0.00 % 1
MySuite.ms 123.457 ms ± 0.00 % 1
MySuite.s 0.123 s ± 0.00 % 1
name time std iterations
-------------------------------------------------
MySuite.ns 123456789.000 ns ± 0.00 % 1
MySuite.us 123456.789 us ± 0.00 % 1
MySuite.ms 123.457 ms ± 0.00 % 1
MySuite.s 0.123 s ± 0.00 % 1
"""#
assertConsoleReported(results, expected)
}
Expand All @@ -199,10 +204,10 @@ final class BenchmarkReporterTests: XCTestCase {
counters: [:]),
]
let expected = #"""
name min max
-------------------------------
MySuite.fast 1000 ns
MySuite.slow 2000000 ns
name min max
---------------------------------------
MySuite.fast 1000.000 ns
MySuite.slow 2000000.000 ns
"""#
assertConsoleReported(results, expected)
}
Expand Down

0 comments on commit 8e0ef8b

Please sign in to comment.