Skip to content

Commit

Permalink
Added toggle to NetworkStressTest to use new concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
ArielDemarco committed Aug 20, 2024
1 parent 2ec8b91 commit 50a80b1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ extension NetworkStressTest {

task.resume()
}

func execute() async -> (NetworkResponse?) {
let before = Date()
do {
let response = try await URLSession.shared.data(for: .init(url: url))
let after = Date()
guard let httpResponse = response.1 as? HTTPURLResponse else {
return nil
}

return NetworkResponse(
id: idx,
requestURL: url,
response: httpResponse,
rtt: after.timeIntervalSince(before)
)
} catch let exception {
print(exception.localizedDescription)
return nil
}
}
}

struct NetworkResponse: Sendable, Identifiable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ struct NetworkStressTest: View {

]

@State var inputURL: String = NetworkStressTest.quickURLs.first!
@State var inputCount: UInt = 20
@State var didSubmit: Bool = false
@State private var inputURL: String = NetworkStressTest.quickURLs.first!
@State private var inputCount: UInt = 20
@State private var didSubmit: Bool = false
@State private var useNewConcurrency: Bool = false

@State var responses: [NetworkResponse] = []
@State private var responses: [NetworkResponse] = []

var body: some View {
VStack {
Form {
Section("Configuration") {
Toggle(isOn: $useNewConcurrency) {
Text("Use new concurrency?")
}
}
List {
Section("Quick URLs") {
ForEach(NetworkStressTest.quickURLs, id: \.self) { item in
Expand Down Expand Up @@ -85,28 +91,47 @@ struct NetworkStressTest: View {
}

responses = []
let group = DispatchGroup()

for index in 0..<inputCount {
if let request = NetworkRequest(string: inputURL, idx: index) {
group.enter()
request.execute { response in
DispatchQueue.main.async {
if let response = response {
self.responses.append(response)
}
group.leave()
}
}
}
}

group.notify(queue: .main) {
if useNewConcurrency {
await performNewConcurrencyRequests()
didSubmit = false
} else {
performLegacyRequests {
didSubmit = false
}
}
}
.navigationTitle("Network Requests")
}

private func performNewConcurrencyRequests() async {
for index in 0..<inputCount {
if let request = NetworkRequest(string: inputURL, idx: index),
let response = await request.execute() {
responses.append(response)
}
}
}

private func performLegacyRequests(completion: @escaping () -> Void) {
let group = DispatchGroup()

for index in 0..<inputCount {
group.enter()
if let request = NetworkRequest(string: inputURL, idx: index) {
request.execute { response in
DispatchQueue.main.async {
if let response = response {
self.responses.append(response)
}
group.leave()
}
}
} }

group.notify(queue: .main) {
completion()
}
}
}

Expand Down

0 comments on commit 50a80b1

Please sign in to comment.