Skip to content

Speed up allocation counting tests #3197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ build_package \
set -eu
cd "$working_dir"
swift build "${build_opts[@]}"
build_dir=$(swift build "${build_opts[@]}" --show-bin-path)
for f in "${files[@]}"; do
echo "- $f"
swift run "${build_opts[@]}" "$(module_name_from_path "$f")"
"${build_dir}/$(module_name_from_path "$f")"
done
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

import AtomicCounter
import Dispatch
import Foundation

#if canImport(Darwin)
Expand Down Expand Up @@ -139,7 +140,14 @@ func measureAll(trackFDs: Bool, _ fn: () -> Int) -> [Measurement] {
}

func measureAndPrint(desc: String, trackFDs: Bool, fn: () -> Int) {
let start = DispatchTime.now()
let measurements = measureAll(trackFDs: trackFDs, fn)

let end = DispatchTime.now()
let durationNanos = end.uptimeNanoseconds - start.uptimeNanoseconds
let durationSeconds = Double(durationNanos) / 1e9
print("\(desc).duration: \(durationSeconds) (s)")

measurements.printTotalAllocations(description: desc)
measurements.printRemainingAllocations(description: desc)
measurements.printTotalAllocatedBytes(description: desc)
Expand Down
22 changes: 11 additions & 11 deletions IntegrationTests/tests_04_performance/Thresholds/5.10.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"1000_udp_reqs": 6050,
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 384050,
"assume_isolated_scheduling_10000_executions": 89,
"1_reqs_1000_conn": 384000,
"assume_isolated_scheduling_10000_executions": 11,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
Expand All @@ -33,8 +33,8 @@
"encode_1000_ws_frames_new_buffer_with_space": 3050,
"encode_1000_ws_frames_new_buffer_with_space_with_mask": 5050,
"execute_hop_10000_tasks": 0,
"flat_schedule_10000_tasks": 100100,
"flat_schedule_assume_isolated_10000_tasks": 90100,
"flat_schedule_10000_tasks": 120050,
"flat_schedule_assume_isolated_10000_tasks": 110050,
"future_assume_isolated_lots_of_callbacks": 74050,
"future_erase_result": 4050,
"future_lots_of_callbacks": 74050,
Expand All @@ -46,14 +46,14 @@
"modifying_byte_buffer_view": 6050,
"ping_pong_1000_reqs_1_conn": 314,
"read_10000_chunks_from_file": 110050,
"schedule_10000_tasks": 40100,
"schedule_10000_tasks": 50050,
"schedule_and_run_10000_tasks": 50050,
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"schedule_assume_isolated_10000_tasks": 50050,
"schedule_with_deadline_10000_tasks": 50050,
"schedule_with_deadline_assume_isolated_10000_tasks": 50050,
"scheduling_10000_executions": 11,
"submit_10000_tasks": 20050,
"submit_assume_isolated_10000_tasks": 20050,
"udp_1000_reqs_1_conn": 6200,
"udp_1_reqs_1000_conn": 162050
}
20 changes: 10 additions & 10 deletions IntegrationTests/tests_04_performance/Thresholds/5.9.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 393050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 11,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
Expand All @@ -33,8 +33,8 @@
"encode_1000_ws_frames_new_buffer_with_space": 3050,
"encode_1000_ws_frames_new_buffer_with_space_with_mask": 5050,
"execute_hop_10000_tasks": 0,
"flat_schedule_10000_tasks": 100100,
"flat_schedule_assume_isolated_10000_tasks": 90100,
"flat_schedule_10000_tasks": 120050,
"flat_schedule_assume_isolated_10000_tasks": 110050,
"future_assume_isolated_lots_of_callbacks": 74050,
"future_erase_result": 4050,
"future_lots_of_callbacks": 74050,
Expand All @@ -46,14 +46,14 @@
"modifying_byte_buffer_view": 6050,
"ping_pong_1000_reqs_1_conn": 329,
"read_10000_chunks_from_file": 110050,
"schedule_10000_tasks": 40100,
"schedule_10000_tasks": 50050,
"schedule_and_run_10000_tasks": 50050,
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"schedule_assume_isolated_10000_tasks": 50050,
"schedule_with_deadline_10000_tasks": 50050,
"schedule_with_deadline_assume_isolated_10000_tasks": 50050,
"scheduling_10000_executions": 11,
"submit_10000_tasks": 20050,
"submit_assume_isolated_10000_tasks": 20050,
"udp_1000_reqs_1_conn": 6200,
"udp_1_reqs_1000_conn": 162050
}
20 changes: 10 additions & 10 deletions IntegrationTests/tests_04_performance/Thresholds/6.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 384000,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 11,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
Expand All @@ -33,8 +33,8 @@
"encode_1000_ws_frames_new_buffer_with_space": 3050,
"encode_1000_ws_frames_new_buffer_with_space_with_mask": 5050,
"execute_hop_10000_tasks": 0,
"flat_schedule_10000_tasks": 100100,
"flat_schedule_assume_isolated_10000_tasks": 90100,
"flat_schedule_10000_tasks": 120050,
"flat_schedule_assume_isolated_10000_tasks": 110050,
"future_assume_isolated_lots_of_callbacks": 74050,
"future_erase_result": 4050,
"future_lots_of_callbacks": 74050,
Expand All @@ -46,14 +46,14 @@
"modifying_byte_buffer_view": 6050,
"ping_pong_1000_reqs_1_conn": 314,
"read_10000_chunks_from_file": 110050,
"schedule_10000_tasks": 40100,
"schedule_10000_tasks": 50050,
"schedule_and_run_10000_tasks": 50050,
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"schedule_assume_isolated_10000_tasks": 50050,
"schedule_with_deadline_10000_tasks": 50050,
"schedule_with_deadline_assume_isolated_10000_tasks": 50050,
"scheduling_10000_executions": 11,
"submit_10000_tasks": 20050,
"submit_assume_isolated_10000_tasks": 20050,
"udp_1000_reqs_1_conn": 6200,
"udp_1_reqs_1000_conn": 162050
}
22 changes: 11 additions & 11 deletions IntegrationTests/tests_04_performance/Thresholds/6.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"1000_udp_reqs": 6050,
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 384000,
"assume_isolated_scheduling_10000_executions": 89,
"1_reqs_1000_conn": 384050,
"assume_isolated_scheduling_10000_executions": 11,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
Expand All @@ -33,8 +33,8 @@
"encode_1000_ws_frames_new_buffer_with_space": 3050,
"encode_1000_ws_frames_new_buffer_with_space_with_mask": 5050,
"execute_hop_10000_tasks": 0,
"flat_schedule_10000_tasks": 100100,
"flat_schedule_assume_isolated_10000_tasks": 90100,
"flat_schedule_10000_tasks": 120050,
"flat_schedule_assume_isolated_10000_tasks": 110050,
"future_assume_isolated_lots_of_callbacks": 74050,
"future_erase_result": 4050,
"future_lots_of_callbacks": 74050,
Expand All @@ -46,14 +46,14 @@
"modifying_byte_buffer_view": 6050,
"ping_pong_1000_reqs_1_conn": 314,
"read_10000_chunks_from_file": 110050,
"schedule_10000_tasks": 40100,
"schedule_10000_tasks": 50050,
"schedule_and_run_10000_tasks": 50050,
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"schedule_assume_isolated_10000_tasks": 50050,
"schedule_with_deadline_10000_tasks": 50050,
"schedule_with_deadline_assume_isolated_10000_tasks": 50050,
"scheduling_10000_executions": 11,
"submit_10000_tasks": 20050,
"submit_assume_isolated_10000_tasks": 20050,
"udp_1000_reqs_1_conn": 6200,
"udp_1_reqs_1000_conn": 162050
}
22 changes: 11 additions & 11 deletions IntegrationTests/tests_04_performance/Thresholds/nightly-main.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"1000_udp_reqs": 6050,
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 384050,
"assume_isolated_scheduling_10000_executions": 89,
"1_reqs_1000_conn": 384000,
"assume_isolated_scheduling_10000_executions": 11,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
Expand All @@ -33,8 +33,8 @@
"encode_1000_ws_frames_new_buffer_with_space": 3050,
"encode_1000_ws_frames_new_buffer_with_space_with_mask": 5050,
"execute_hop_10000_tasks": 0,
"flat_schedule_10000_tasks": 100100,
"flat_schedule_assume_isolated_10000_tasks": 80100,
"flat_schedule_10000_tasks": 120050,
"flat_schedule_assume_isolated_10000_tasks": 110050,
"future_assume_isolated_lots_of_callbacks": 74050,
"future_erase_result": 4050,
"future_lots_of_callbacks": 74050,
Expand All @@ -46,14 +46,14 @@
"modifying_byte_buffer_view": 6050,
"ping_pong_1000_reqs_1_conn": 314,
"read_10000_chunks_from_file": 110050,
"schedule_10000_tasks": 40100,
"schedule_10000_tasks": 50050,
"schedule_and_run_10000_tasks": 50050,
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"schedule_assume_isolated_10000_tasks": 50050,
"schedule_with_deadline_10000_tasks": 50050,
"schedule_with_deadline_assume_isolated_10000_tasks": 50050,
"scheduling_10000_executions": 11,
"submit_10000_tasks": 20050,
"submit_assume_isolated_10000_tasks": 20050,
"udp_1000_reqs_1_conn": 6200,
"udp_1_reqs_1000_conn": 162050
}
20 changes: 10 additions & 10 deletions IntegrationTests/tests_04_performance/Thresholds/nightly-next.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"1000_udpbootstraps": 2050,
"1000_udpconnections": 75050,
"1_reqs_1000_conn": 384050,
"assume_isolated_scheduling_10000_executions": 89,
"assume_isolated_scheduling_10000_executions": 11,
"bytebuffer_lots_of_rw": 2050,
"creating_10000_headers": 0,
"decode_1000_ws_frames": 2050,
Expand All @@ -33,8 +33,8 @@
"encode_1000_ws_frames_new_buffer_with_space": 3050,
"encode_1000_ws_frames_new_buffer_with_space_with_mask": 5050,
"execute_hop_10000_tasks": 0,
"flat_schedule_10000_tasks": 100100,
"flat_schedule_assume_isolated_10000_tasks": 90100,
"flat_schedule_10000_tasks": 120050,
"flat_schedule_assume_isolated_10000_tasks": 110050,
"future_assume_isolated_lots_of_callbacks": 74050,
"future_erase_result": 4050,
"future_lots_of_callbacks": 74050,
Expand All @@ -46,14 +46,14 @@
"modifying_byte_buffer_view": 6050,
"ping_pong_1000_reqs_1_conn": 314,
"read_10000_chunks_from_file": 110050,
"schedule_10000_tasks": 40100,
"schedule_10000_tasks": 50050,
"schedule_and_run_10000_tasks": 50050,
"schedule_assume_isolated_10000_tasks": 40100,
"schedule_with_deadline_10000_tasks": 40100,
"schedule_with_deadline_assume_isolated_10000_tasks": 40100,
"scheduling_10000_executions": 89,
"submit_10000_tasks": 20100,
"submit_assume_isolated_10000_tasks": 20100,
"schedule_assume_isolated_10000_tasks": 50050,
"schedule_with_deadline_10000_tasks": 50050,
"schedule_with_deadline_assume_isolated_10000_tasks": 50050,
"scheduling_10000_executions": 11,
"submit_10000_tasks": 20050,
"submit_assume_isolated_10000_tasks": 20050,
"udp_1000_reqs_1_conn": 6200,
"udp_1_reqs_1000_conn": 162050
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ observed_allocations="${observed_allocations}
info "observed allocations:
${observed_allocations}"

cat "$tmp/output" # helps debugging

for test in "${all_tests[@]}"; do
cat "$tmp/output" # helps debugging

while read -r test_case; do
test_case=${test_case#test_*}
Expand All @@ -55,7 +56,7 @@ for test in "${all_tests[@]}"; do
leaked_fds=$(grep "^test_$test_case.leaked_fds:" "$tmp/output" | cut -d: -f2 | sed 's/ //g')
max_allowed_env_name="MAX_ALLOCS_ALLOWED_$test_case"
max_allowed=$(jq '.'\""$test_case"\" "$here/Thresholds/$SWIFT_VERSION.json")

assert_is_number "$max_allowed" "Malformed or nonexistent ${SWIFT_VERSION}.json thresholds file"

info "$test_case: allocations not freed: $not_freed_allocations"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,44 @@ import NIOHTTP1
import NIOPosix

let localhostPickPort = try! SocketAddress.makeAddressResolvingHost("127.0.0.1", port: 0)
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
let group: MultiThreadedEventLoopGroup = {
preHeatMTELGSingleton()
return MultiThreadedEventLoopGroup.singleton
}()

extension MultiThreadedEventLoopGroup {
static var preheatedSingleton: MultiThreadedEventLoopGroup {
group
}
}

private func preHeatMTELGSingleton() {
for loop in MultiThreadedEventLoopGroup.singleton.makeIterator() {
var futures = [EventLoopFuture<Void>]()
futures.reserveCapacity(10_000)

for _ in 0..<10_000 {
let f = loop.submit {}
futures.append(f)
}

for f in futures {
try! f.wait()
}

var scheduleds = [Scheduled<Void>]()
scheduleds.reserveCapacity(10_000)

for _ in 0..<10_000 {
let t = loop.scheduleTask(in: .hours(1)) {}
scheduleds.append(t)
}

for t in scheduleds {
t.cancel()
}
}
}

final class RepeatedRequests: ChannelInboundHandler {
typealias InboundIn = HTTPClientResponsePart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NIOCore
import NIOPosix

func run(identifier: String) {
let group = MultiThreadedEventLoopGroup.preheatedSingleton
MultiThreadedEventLoopGroup.withCurrentThreadAsEventLoop { loop in
ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).map { server in
measure(identifier: identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ private final class CloseAfterTimeoutHandler: ChannelInboundHandler {
}

func run(identifier: String) {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}
let group = MultiThreadedEventLoopGroup.preheatedSingleton.next()

let serverConnection = try! ServerBootstrap(group: group)
.bind(host: "localhost", port: 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import NIOPosix

func run(identifier: String) {
measure(identifier: identifier) {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let group = MultiThreadedEventLoopGroup.preheatedSingleton.next()
let loop = group.next()
let dg = DispatchGroup()

Expand All @@ -28,7 +28,6 @@ func run(identifier: String) {
}
}.wait()
dg.wait()
try! group.syncShutdownGracefully()
return 10_000
}
}
Loading
Loading