-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from finestructure/develop
release-0.5.0
- Loading branch information
Showing
17 changed files
with
256 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Collection+ext.swift | ||
// ResterCore | ||
// | ||
// Created by Sven A. Schmidt on 10/04/2019. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
extension Collection where Element == Double { | ||
public var average: Element { | ||
let total = reduce(0, +) | ||
return isEmpty ? .nan : total / Element(count) | ||
} | ||
} | ||
|
||
|
||
extension Collection where Element == Double { | ||
public var median: Element { | ||
guard !isEmpty else { return .nan } | ||
let s = sorted() | ||
if count.isMultiple(of: 2) { | ||
return [s[count/2 - 1], s[count/2]].average | ||
} else { | ||
return s[count/2] | ||
} | ||
} | ||
} | ||
|
||
|
||
extension Collection where Element == Double { | ||
public func percentile(_ p: Double) -> Element { | ||
guard count >= 2 else { return .nan } | ||
let s = sorted() | ||
let cutoff = abs(p).clamp(max: 0.99) * Double(count) | ||
let index = Int(cutoff) | ||
let isInteger = (Double(index) == cutoff) | ||
if isInteger { | ||
guard (1..<count).contains(index) else { return .nan } | ||
return [s[index - 1], s[index]].average | ||
} else { | ||
return s[index] | ||
} | ||
} | ||
} | ||
|
||
|
||
extension Collection where Element == Double { | ||
public var stddev: Element { | ||
guard count > 0 else { return .nan } | ||
let mean = average | ||
let sumOfMeanSqr = map { pow($0 - mean, 2) }.reduce(0, +) | ||
let variance = sumOfMeanSqr / Double(count - 1) | ||
return sqrt(variance) | ||
} | ||
} |
File renamed without changes.
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,15 @@ | ||
// | ||
// Numeric+ext.swift | ||
// ResterCore | ||
// | ||
// Created by Sven A. Schmidt on 10/04/2019. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
extension Numeric where Self: Comparable { | ||
public func clamp(max: Self) -> Self { | ||
return min(self, max) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,45 @@ | ||
// | ||
// Stats.swift | ||
// ResterCore | ||
// | ||
// Created by Sven A. Schmidt on 09/04/2019. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
public struct Stats { | ||
public var durations = [TimeInterval]() | ||
|
||
public mutating func add(_ duration: TimeInterval) { | ||
durations.append(duration) | ||
} | ||
} | ||
|
||
|
||
extension Stats: CustomStringConvertible { | ||
public var description: String { | ||
return """ | ||
Average: \(durations.average.fmt) | ||
Median: \(durations.median.fmt) | ||
Min: \(durations.min()?.fmt ?? "-") | ||
Max: \(durations.max()?.fmt ?? "-") | ||
Std dev: \(durations.stddev.fmt) | ||
90% Pctl: \(durations.percentile(0.9).fmt) | ||
""" | ||
} | ||
} | ||
|
||
|
||
extension Double { | ||
fileprivate var fmt: String { | ||
guard !isNaN else { return "-" } | ||
let formatter = NumberFormatter() | ||
formatter.minimumIntegerDigits = 1 | ||
formatter.minimumFractionDigits = 3 | ||
formatter.maximumFractionDigits = 3 | ||
formatter.roundingMode = .halfUp | ||
guard let str = formatter.string(from: NSNumber(value: self)) else { return "-" } | ||
return str + "s" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// StatsTests.swift | ||
// ResterTests | ||
// | ||
// Created by Sven A. Schmidt on 10/04/2019. | ||
// | ||
|
||
import XCTest | ||
@testable import ResterCore | ||
|
||
|
||
class StatsTests: XCTestCase { | ||
|
||
func test_average() { | ||
XCTAssertEqual([1.0, 4.0, 3.0, 2.0].average, 2.5) | ||
XCTAssertEqual([1, 4, 3, 2].average, 2.5) | ||
XCTAssert([Double]().average.isNaN) | ||
} | ||
|
||
func test_median() { | ||
XCTAssertEqual([24, 1, 4, 5, 20, 6, 7, 12, 14, 18, 19, 22].median, 13.0) | ||
XCTAssertEqual([1.0, 5.0, 3.0, 2.0].median, 2.5) | ||
XCTAssertEqual([1].median, 1.0) | ||
XCTAssert([Double]().median.isNaN) | ||
} | ||
|
||
func test_percentile() { | ||
let values: [Double] = [43, 54, 56, 61, 62, 66, 68, 69, 69, 70, 71, 72, 77, 78, 79, 85, 87, 88, 89, 93, 95, 96, 98, 99, 99].shuffled() | ||
XCTAssertEqual(values.percentile(0.9), 98.0) | ||
XCTAssertEqual(values.percentile(1.0), 99.0) | ||
XCTAssertEqual(values.percentile(1.1), 99.0) | ||
XCTAssertEqual(values.percentile(0.5), values.median) | ||
XCTAssertEqual([1, 4, -3, 2, -9, -7, 0, -4, -1, 2, 1, -5, -3, 10, 10, 5].percentile(0.75), 3) | ||
XCTAssert([0, 1].percentile(0).isNaN) | ||
XCTAssert([1].percentile(0.5).isNaN) | ||
XCTAssert([1].percentile(1.0).isNaN) | ||
XCTAssert([1].percentile(0).isNaN) | ||
XCTAssert([Double]().percentile(0).isNaN) | ||
} | ||
|
||
func test_stddev() { | ||
let values: [Double] = [10, 8, 10, 8, 8 , 4] | ||
XCTAssertEqual(values.stddev, 2.19, accuracy: 0.01) | ||
} | ||
} |
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,13 @@ | ||
variables: | ||
API_URL: https://httpbin.org | ||
requests: | ||
request 1: | ||
url: ${API_URL}/anything | ||
method: GET | ||
validation: | ||
status: 200 | ||
request 2: | ||
url: ${API_URL}/anything | ||
method: GET | ||
validation: | ||
status: 200 |
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
35 changes: 35 additions & 0 deletions
35
Tests/ResterTests/__Snapshots__/LaunchTests/test_launch_stats.1.txt
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,35 @@ | ||
🚀 Resting basic2.yml ... | ||
|
||
🎬 request 1 started ... | ||
|
||
✅ request 1 PASSED (X.XXXs) | ||
|
||
request 1 | ||
Average: X.XXXs | ||
Median: X.XXXs | ||
Min: X.XXXs | ||
Max: X.XXXs | ||
Std dev: - | ||
90% Pctl: - | ||
|
||
🎬 request 2 started ... | ||
|
||
✅ request 2 PASSED (X.XXXs) | ||
|
||
request 1 | ||
Average: X.XXXs | ||
Median: X.XXXs | ||
Min: X.XXXs | ||
Max: X.XXXs | ||
Std dev: - | ||
90% Pctl: - | ||
|
||
request 2 | ||
Average: X.XXXs | ||
Median: X.XXXs | ||
Min: X.XXXs | ||
Max: X.XXXs | ||
Std dev: - | ||
90% Pctl: - | ||
|
||
Executed 2 tests, with 0 failures |