-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(minor): Add support for metricP90AbsoluteThresholds export format (
#180) This provides better out-of-the-box experience for absolute checks and is geared towards CI checking of e.g. malloc/syscall deviations. The thresholds can e.g. be put be in a "Thresholds" directory. To generate a new baseline, run e.g. ```bash swift package --allow-writing-to-package-directory benchmark --format metricP90AbsoluteThresholds --path Thresholds/ ``` To run a check vs. the saved thresholds, run: ```bash swift package benchmark baseline check --check-absolute-path /relative/or/absolute/path/to/Thresholds ```
- Loading branch information
Showing
18 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// File.swift | ||
// Basic+SetupTeardown.swift | ||
// | ||
// | ||
// Created by Joakim Hassila on 2023-04-21. | ||
|
33 changes: 33 additions & 0 deletions
33
Benchmarks/P90AbsoluteThresholds/P90AbsoluteThresholds.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Copyright (c) 2023 Ordo One AB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
import Benchmark | ||
import Foundation | ||
|
||
let benchmarks = { | ||
Benchmark.defaultConfiguration = .init(metrics: [.mallocCountTotal, .syscalls], | ||
warmupIterations: 1, | ||
scalingFactor: .kilo, | ||
maxDuration: .seconds(2), | ||
maxIterations: .kilo(100)) | ||
|
||
Benchmark("P90Date") { benchmark in | ||
for _ in benchmark.scaledIterations { | ||
blackHole(Foundation.Date()) | ||
} | ||
} | ||
|
||
Benchmark("P90Malloc") { benchmark in | ||
for _ in benchmark.scaledIterations { | ||
var array: [Int] = [] | ||
array.append(contentsOf: 0 ... 1_000) | ||
blackHole(array) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Plugins/BenchmarkTool/BenchmarkTool+ReadP90AbsoluteThresholds.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// Copyright (c) 2023 Ordo One AB. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
|
||
import Benchmark | ||
import Foundation | ||
import SystemPackage | ||
|
||
#if canImport(Darwin) | ||
import Darwin | ||
#elseif canImport(Glibc) | ||
import Glibc | ||
#else | ||
#error("Unsupported Platform") | ||
#endif | ||
|
||
extension BenchmarkTool { | ||
/// `makeBenchmarkThresholds` is a convenience function for reading p90 static thresholds that previously have been exported with `metricP90AbsoluteThresholds` | ||
/// | ||
/// - Parameters: | ||
/// - path: The path where the `Thresholds` directory should be located, containing static thresholds files using the naming pattern: | ||
/// `moduleName.benchmarkName.p90.json` | ||
/// - moduleName: The name of the benchmark module, can be extracted in the benchmark using: | ||
/// `String("\(#fileID)".prefix(while: { $0 != "/" }))` | ||
/// - benchmarkName: The name of the benchmark | ||
/// - Returns: A dictionary with static benchmark thresholds per metric or nil if the file could not be found or read | ||
static func makeBenchmarkThresholds(path: String, | ||
moduleName: String, | ||
benchmarkName: String) -> [String : BenchmarkThresholds.AbsoluteThreshold]? { | ||
var path = FilePath(path) | ||
if path.isAbsolute { | ||
path.append("\(moduleName).\(benchmarkName).p90.json") | ||
} else { | ||
var cwdPath = FilePath(FileManager.default.currentDirectoryPath) | ||
cwdPath.append(path.components) | ||
cwdPath.append("\(moduleName).\(benchmarkName).p90.json") | ||
path = cwdPath | ||
} | ||
|
||
var p90Thresholds: [String : BenchmarkThresholds.AbsoluteThreshold]? | ||
|
||
do { | ||
let fileDescriptor = try FileDescriptor.open(path, .readOnly, options: [], permissions: .ownerRead) | ||
|
||
do { | ||
try fileDescriptor.closeAfter { | ||
do { | ||
var readBytes = [UInt8]() | ||
let bufferSize = 16 * 1_024 * 1_024 | ||
|
||
while true { | ||
let nextBytes = try [UInt8](unsafeUninitializedCapacity: bufferSize) { buf, count in | ||
count = try fileDescriptor.read(into: UnsafeMutableRawBufferPointer(buf)) | ||
} | ||
if nextBytes.isEmpty { | ||
break | ||
} | ||
readBytes.append(contentsOf: nextBytes) | ||
} | ||
|
||
p90Thresholds = try JSONDecoder().decode([String : BenchmarkThresholds.AbsoluteThreshold].self, from: Data(readBytes)) | ||
} catch { | ||
print("Failed to read file at \(path) [\(error)] \(Errno(rawValue: errno).description)") | ||
} | ||
} | ||
} catch { | ||
print("Failed to close fd for \(path) after reading.") | ||
} | ||
} catch { | ||
if errno != ENOENT { // file not found is ok, e.g. no thresholds found, then silently return nil | ||
print("Failed to open file \(path), errno = [\(errno)] \(Errno(rawValue: errno).description)") | ||
} | ||
} | ||
return p90Thresholds | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.