Skip to content

Commit

Permalink
fix(minor): Fix improper generation of benchmark targets (#159)
Browse files Browse the repository at this point in the history
Fix benchmark init generation to add proper dependencies (as mentioned
in (#157)) - fix the
local benchmark targets too.
  • Loading branch information
hassila authored Jun 1, 2023
1 parent 94acbd8 commit 6a9f3e1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
18 changes: 12 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ package.targets += [
name: "BenchmarkDateTime",
dependencies: [
"Benchmark",
"BenchmarkPlugin"
],
path: "Benchmarks/DateTime"
path: "Benchmarks/DateTime",
plugins: [
"BenchmarkPlugin"
]
)
]

Expand All @@ -176,9 +178,11 @@ package.targets += [
name: "Basic",
dependencies: [
"Benchmark",
"BenchmarkPlugin"
],
path: "Benchmarks/Basic"
path: "Benchmarks/Basic",
plugins: [
"BenchmarkPlugin"
]
),
]

Expand All @@ -188,9 +192,11 @@ package.targets += [
name: "HistogramBenchmark",
dependencies: [
"Benchmark",
"BenchmarkPlugin",
.product(name: "Histogram", package: "package-histogram"),
],
path: "Benchmarks/Histogram"
path: "Benchmarks/Histogram",
plugins: [
"BenchmarkPlugin"
]
),
]
6 changes: 4 additions & 2 deletions Plugins/BenchmarkTool/BenchmarkTool+CreateBenchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ extension BenchmarkTool {
name: "\(targetName)",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "BenchmarkPlugin", package: "package-benchmark")
],
path: "Benchmarks/\(targetName)"
path: "Benchmarks/\(targetName)",
plugins: [
.product(name: "BenchmarkPlugin", package: "package-benchmark")
]
),
]
"""
Expand Down
4 changes: 3 additions & 1 deletion Sources/Benchmark/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import Dispatch

// swiftlint: disable file_length
// swiftlint: disable file_length prefer_self_in_static_references

/// Defines a benchmark
public final class Benchmark: Codable, Hashable {
Expand Down Expand Up @@ -39,6 +39,8 @@ public final class Benchmark: Codable, Hashable {
#endif
public typealias BenchmarkCustomMetricMeasurement = (BenchmarkMetric, Int) -> Void

// swiftlint: enable prefer_self_in_static_references

/// Alias for closures used to hook into setup / teardown
public typealias BenchmarkHook = () async throws -> Void
public typealias BenchmarkSetupTeardownHook = BenchmarkHook
Expand Down
14 changes: 7 additions & 7 deletions Sources/Benchmark/BenchmarkResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public struct BenchmarkResult: Codable, Comparable, Equatable {
"(\(scaledTimeUnits.description)) *" : "(\(timeUnits.description)) *"
}

public static func == (lhs: BenchmarkResult, rhs: BenchmarkResult) -> Bool {
public static func == (lhs: Self, rhs: Self) -> Bool {
guard lhs.metric == rhs.metric else {
return false
}
Expand All @@ -314,7 +314,7 @@ public struct BenchmarkResult: Codable, Comparable, Equatable {
return true
}

public static func < (lhs: BenchmarkResult, rhs: BenchmarkResult) -> Bool {
public static func < (lhs: Self, rhs: Self) -> Bool {
let reversedComparison = lhs.metric.polarity == .prefersLarger

guard lhs.metric == rhs.metric else {
Expand Down Expand Up @@ -353,13 +353,13 @@ public struct BenchmarkResult: Codable, Comparable, Equatable {
}

// swiftlint:disable function_body_length
public func betterResultsOrEqual(than otherResult: BenchmarkResult,
public func betterResultsOrEqual(than otherResult: Self,
thresholds: BenchmarkThresholds = .default,
name: String = "unknown name",
target: String = "unknown target") -> (Bool, [ThresholdDeviation]) {
var violationDescriptions: [ThresholdDeviation] = []
var rhs: BenchmarkResult
var lhs: BenchmarkResult
var rhs: Self
var lhs: Self

lhs = self
rhs = otherResult
Expand All @@ -372,7 +372,7 @@ public struct BenchmarkResult: Codable, Comparable, Equatable {
func worseResult(_ metric: BenchmarkMetric,
_ lhs: Int,
_ rhs: Int,
_ percentile: BenchmarkResult.Percentile,
_ percentile: Self.Percentile,
_ thresholds: BenchmarkThresholds,
_ scalingFactor: Statistics.Units) -> (Bool, [ThresholdDeviation]) {
let relativeDifference = rhs != 0 ? (100 - (100.0 * Double(lhs) / Double(rhs))) : 0.0
Expand Down Expand Up @@ -439,7 +439,7 @@ public struct BenchmarkResult: Codable, Comparable, Equatable {
target: String) -> [ThresholdDeviation] {
func worseResult(_ metric: BenchmarkMetric,
_ lhs: Int,
_ percentile: BenchmarkResult.Percentile,
_ percentile: Self.Percentile,
_ thresholds: BenchmarkThresholds,
_ scalingFactor: Statistics.Units) -> [ThresholdDeviation] {
let reverseComparison = metric.polarity == .prefersLarger
Expand Down
10 changes: 5 additions & 5 deletions Tests/BenchmarkTests/BenchmarkResultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ final class BenchmarkResultTests: XCTestCase {
thresholds: .default,
statistics: firstStatistics)

XCTAssert(firstResult != secondResult)
XCTAssertNotEqual(firstResult, secondResult)
XCTAssertFalse(firstResult > secondResult)
XCTAssertFalse(firstResult < secondResult)
XCTAssertFalse(firstResult == secondResult)
XCTAssertNotEqual(firstResult, secondResult)
}

func testBenchmarkResultBetterOrEqualWithDefaultThresholds() throws {
Expand Down Expand Up @@ -362,7 +362,7 @@ final class BenchmarkResultTests: XCTestCase {
thresholds: .default,
statistics: firstStatistics)

XCTAssert(result.normalize(125_000_000) == result.scale(125_000_000_000))
XCTAssertEqual(result.normalize(125_000_000), result.scale(125_000_000_000))

result = BenchmarkResult(metric: .cpuUser,
timeUnits: .microseconds,
Expand All @@ -371,7 +371,7 @@ final class BenchmarkResultTests: XCTestCase {
thresholds: .default,
statistics: firstStatistics)

XCTAssert(result.normalize(125_000_000) == result.scale(125_000_000_000))
XCTAssertEqual(result.normalize(125_000_000), result.scale(125_000_000_000))

result = BenchmarkResult(metric: .cpuUser,
timeUnits: .nanoseconds,
Expand All @@ -380,7 +380,7 @@ final class BenchmarkResultTests: XCTestCase {
thresholds: .default,
statistics: firstStatistics)

XCTAssert(result.normalize(125_000_000) == result.scale(125_000_000_000))
XCTAssertEqual(result.normalize(125_000_000), result.scale(125_000_000_000))
}

func testBenchmarkResultEnumerations() throws {
Expand Down
2 changes: 1 addition & 1 deletion Tests/BenchmarkTests/StatisticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class StatisticsTests: XCTestCase {
stats.add(measurement)
}

XCTAssert(Statistics.roundToDecimalplaces(123.4567898972239487234) == 123.46)
XCTAssertEqual(Statistics.roundToDecimalplaces(123.4567898972239487234), 123.46)
XCTAssertEqual(stats.measurementCount, measurementCount * 2)
XCTAssertEqual(stats.units(), .count)
XCTAssertEqual(round(stats.histogram.mean), round(Double(measurementCount / 2)))
Expand Down

0 comments on commit 6a9f3e1

Please sign in to comment.