Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Sources/SwiftNetwork/Connection/NetworkMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,51 @@ public enum NetworkMetrics {
case protocolEstablishmentReports([ProtocolEstablishmentReport])
case dataTransferSnapshot(DataTransferSnapshot)
}

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
public protocol NetworkMetricsProviding: ~Copyable {
func getMetrics(
requestedNetworkMetric: RequestedNetworkMetrics,
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> NetworkMetrics?
}

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
extension NetworkMetricsProviding where Self: ~Copyable {
/// The data transfer snapshot, or nil if the instance did not answer with one.
public func getDataTransferSnapshot(
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> DataTransferSnapshot? {
getMetrics(requestedNetworkMetric: .dataTransferSnapshot, for: instance, in: &eventContext)?
.dataTransferSnapshot
}

/// The protocol establishment reports, or nil if the instance did not answer with them.
public func getProtocolEstablishmentReports(
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> [ProtocolEstablishmentReport]? {
getMetrics(requestedNetworkMetric: .protocolEstablishmentReports, for: instance, in: &eventContext)?
.protocolEstablishmentReports
}
}

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
extension NetworkMetrics {
/// The data transfer snapshot, if this answers a `.dataTransferSnapshot` request.
public var dataTransferSnapshot: DataTransferSnapshot? {
guard case .dataTransferSnapshot(let snapshot) = self else { return nil }
return snapshot
}

/// The protocol establishment reports, if this answers a `.protocolEstablishmentReports` request.
public var protocolEstablishmentReports: [ProtocolEstablishmentReport]? {
guard case .protocolEstablishmentReports(let reports) = self else { return nil }
return reports
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public protocol UpperProtocolHandler<LowerProtocol>: ~Copyable, ProtocolInstance
/// A protocol closer to the network, with a linkage to an upper protocol toward the app.
@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
public protocol LowerProtocolHandler<UpperProtocol>: ~Copyable, ProtocolInstance {
public protocol LowerProtocolHandler<UpperProtocol>: ~Copyable, ProtocolInstance, NetworkMetricsProviding {
associatedtype UpperProtocol: UpperProtocolLinkage

mutating func attachUpperProtocol(
Expand Down Expand Up @@ -78,9 +78,4 @@ public protocol LowerProtocolHandler<UpperProtocol>: ~Copyable, ProtocolInstance
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> ProtocolMetadata<P>?
func getMetrics(
requestedNetworkMetric: RequestedNetworkMetrics,
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> NetworkMetrics?
}
7 changes: 1 addition & 6 deletions Sources/SwiftNetwork/Protocols/ProtocolLinkage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public protocol ListenerLinkage: LowerProtocolLinkage where PairedUpperLinkage:

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
public protocol LowerProtocolLinkage: ProtocolLinkage {
public protocol LowerProtocolLinkage: ProtocolLinkage, NetworkMetricsProviding {
/// The upper linkage this lower linkage attaches to.
associatedtype PairedUpperLinkage: UpperProtocolLinkage

Expand Down Expand Up @@ -308,11 +308,6 @@ public protocol LowerProtocolLinkage: ProtocolLinkage {
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> ProtocolMetadata<P>?
func getMetrics(
requestedNetworkMetric: RequestedNetworkMetrics,
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> NetworkMetrics?
}

@_spi(ProtocolProvider)
Expand Down
49 changes: 47 additions & 2 deletions Sources/SwiftNetworkTestHarness/HarnessProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,33 @@ extension ProtocolInstance where Self: AnyObject {

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
public protocol UpperHarnessProtocol: TopDatapathProtocol, LoggableProtocol {
public protocol UpperHarnessProtocol: TopDatapathProtocol, LoggableProtocol, NetworkMetricsProviding {
/// Metadata handed over by the new inbound flow event that created this harness.
var flowMetadata: AbstractProtocolMetadata? { get set }
func teardown()
func getMetrics(requestedNetworkMetric: RequestedNetworkMetrics) -> NetworkMetrics?
}

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
extension UpperHarnessProtocol {
public func getMetrics(
requestedNetworkMetric: RequestedNetworkMetrics,
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> NetworkMetrics? {
lower.invokeGetMetrics(requestedNetworkMetric: requestedNetworkMetric, for: instance, in: &eventContext)
}

/// The data transfer snapshot, or nil if the stack did not answer with one.
public func getDataTransferSnapshot() -> DataTransferSnapshot? {
getMetrics(requestedNetworkMetric: .dataTransferSnapshot)?.dataTransferSnapshot
}

/// The protocol establishment reports, or nil if the stack did not answer with them.
public func getProtocolEstablishmentReports() -> [ProtocolEstablishmentReport]? {
getMetrics(requestedNetworkMetric: .protocolEstablishmentReports)?.protocolEstablishmentReports
}
}

@_spi(ProtocolProvider)
Expand Down Expand Up @@ -869,7 +892,7 @@ public class StreamLowerHarness<LinkageFamily: StreamLinkageFamily>: LowerHarnes
@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
public class NewFlowHarness<LinkageFamily: DataLinkageFamily, HarnessType: UpperHarnessProtocol>: InboundFlowHandler,
LoggableProtocol
LoggableProtocol, NetworkMetricsProviding
where
// The harness this one creates per inbound flow sits on the flow linkage that the listener
// hands back, so its lower protocol has to be the family's own data linkage. Stating it
Expand Down Expand Up @@ -1171,3 +1194,25 @@ public class NewStreamFlowHarness<LinkageFamily: StreamLinkageFamily>: NewFlowHa
{}

#endif

@_spi(ProtocolProvider)
@available(Network 0.1.0, *)
extension NewFlowHarness {
public func getMetrics(
requestedNetworkMetric: RequestedNetworkMetrics,
for instance: InstanceIdentifier,
in eventContext: inout NetworkContext.EventContext
) -> NetworkMetrics? {
lower.invokeGetMetrics(requestedNetworkMetric: requestedNetworkMetric, for: instance, in: &eventContext)
}

/// The data transfer snapshot, or nil if the stack did not answer with one.
public func getDataTransferSnapshot() -> DataTransferSnapshot? {
getMetrics(requestedNetworkMetric: .dataTransferSnapshot)?.dataTransferSnapshot
}

/// The protocol establishment reports, or nil if the stack did not answer with them.
public func getProtocolEstablishmentReports() -> [ProtocolEstablishmentReport]? {
getMetrics(requestedNetworkMetric: .protocolEstablishmentReports)?.protocolEstablishmentReports
}
}
20 changes: 10 additions & 10 deletions Tests/SwiftNetworkTests/QUICTestHarness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1436,24 +1436,24 @@ class QUICTestHarness {

if validateMetrics {
if let serverHarness = state?.serverHarness, let clientHarness = state?.clientHarness {
var clientReports: NetworkMetrics?
var serverReports: NetworkMetrics?
var clientSnapshot: DataTransferSnapshot?
var serverSnapshot: DataTransferSnapshot?
var snapshotFinished = false
context.async {
clientReports = clientHarness.getMetrics(requestedNetworkMetric: .dataTransferSnapshot)
serverReports = serverHarness.getMetrics(requestedNetworkMetric: .dataTransferSnapshot)
XCTAssertNotNil(clientReports)
XCTAssertNotNil(serverReports)
clientSnapshot = clientHarness.getDataTransferSnapshot()
serverSnapshot = serverHarness.getDataTransferSnapshot()
XCTAssertNotNil(clientSnapshot)
XCTAssertNotNil(serverSnapshot)
snapshotFinished = true
}
wait(until: { snapshotFinished }, timeout: 5.0, "qUIC connection to receive metrics")
// Now validate the protocol establishment report
clientReports = nil
serverReports = nil
var clientReports: [ProtocolEstablishmentReport]?
var serverReports: [ProtocolEstablishmentReport]?
var protocolEstablishmentReportFinished = false
context.async {
clientReports = clientHarness.getMetrics(requestedNetworkMetric: .protocolEstablishmentReports)
serverReports = serverHarness.getMetrics(requestedNetworkMetric: .protocolEstablishmentReports)
clientReports = clientHarness.getProtocolEstablishmentReports()
serverReports = serverHarness.getProtocolEstablishmentReports()
XCTAssertNotNil(clientReports)
XCTAssertNotNil(serverReports)
protocolEstablishmentReportFinished = true
Expand Down
8 changes: 1 addition & 7 deletions Tests/SwiftNetworkTests/SwiftNetworkIPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,7 @@ final class SwiftNetworkIPTests: NetTestCase {
}

if let expectedIPSnapshot {
let metrics = upperHarness.getMetrics(requestedNetworkMetric: .dataTransferSnapshot)
switch metrics {
case .dataTransferSnapshot(let snapshot):
XCTAssertEqual(snapshot, expectedIPSnapshot)
default:
XCTFail("Failed to get IP harness metrics")
}
XCTAssertEqual(upperHarness.getDataTransferSnapshot(), expectedIPSnapshot)
}
upperHarness.stop()
upperHarness.teardown()
Expand Down
12 changes: 2 additions & 10 deletions Tests/SwiftNetworkTests/SwiftNetworkSwiftTLSRecordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,8 @@ final class SwiftNetworkSwiftTLSRecordTests: NetTestCase {
var serverReports: [ProtocolEstablishmentReport] = []
let collected = XCTestExpectation(description: "Wait for the establishment reports")
state.context.async {
if case .protocolEstablishmentReports(let reports)? = state.clientApplicationLayer.getMetrics(
requestedNetworkMetric: .protocolEstablishmentReports
) {
clientReports = reports
}
if case .protocolEstablishmentReports(let reports)? = state.serverApplicationLayer.getMetrics(
requestedNetworkMetric: .protocolEstablishmentReports
) {
serverReports = reports
}
clientReports = state.clientApplicationLayer.getProtocolEstablishmentReports() ?? []
serverReports = state.serverApplicationLayer.getProtocolEstablishmentReports() ?? []
collected.fulfill()
}
wait(for: [collected], timeout: 5.0)
Expand Down
7 changes: 2 additions & 5 deletions Tests/SwiftNetworkTests/SwiftNetworkUDPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ final class SwiftNetworkUDPTests: NetTestCase {
XCTAssertEqual(expectedInputData, readApplicationData, "Failed to read expected UDP input data")
}
if validateMetrics {
let metrics = upperHarness.getMetrics(requestedNetworkMetric: .dataTransferSnapshot)
XCTAssertNotNil(metrics)
switch metrics {
case .dataTransferSnapshot(let snapshot):
if let snapshot = upperHarness.getDataTransferSnapshot() {
XCTAssertEqual(
snapshot.receivedTransportByteCount,
UInt64(inputPacket.count - UDPProtocol.headerLength)
Expand All @@ -198,7 +195,7 @@ final class SwiftNetworkUDPTests: NetTestCase {
expectedSnapshot.receivedTransportByteCount = snapshot.receivedTransportByteCount
expectedSnapshot.sentTransportByteCount = snapshot.sentTransportByteCount
XCTAssertEqual(snapshot, expectedSnapshot)
default:
} else {
XCTFail("Failed to get UDP harness metrics")
}
}
Expand Down
Loading