Files
swift-nio-http2/scripts/cachegrindify.sh
Cory Benfield 37919ba1b7 Reduce the cost of flushing (#265)
Motivation:

The OutboundFlowControlBuffer is responsible for managing DATA and
HEADERS frames for all streams in order to ensure that we obey HTTP/2
flow control rules, even when users don't. This object is therefore on
the data path for the vast majority of frames, and is extremely
performance sensitive.

Unfortunately, the current algorithm has some nasty performance cliffs.
Right now it has the awkward property of making flushes take an amount
of time linear in the number of streams on the connection, not the
number of streams that had had frames sent on them. The result is that
connections with highly parallel numbers of streams see extremely poor
performance when flushing, a problem that is exacerbated if other parts
of the HTTP/2 stack fail to coalesce those flushes.

We can clean this up in a few ways, but the easiest way is to just keep
track of which streams have done I/O since the last flush. While we're
there, we can make a few other optimisations to ensure we only flush
data on streams we are actually processing, to further reduce the risk
of wasting our time processing unnecessary work.

Modifications:

- Add a Set to keep track of where we've done writes since the last
   flush.
- Streams whose flow control window goes to zero stop being writable.
- Cheaper and more accurate calculation of zero-length writes, which
   avoids the possibility of awkward stalls with zero-length DATA frames
   and END_STREAM.
- Stop doing math about the number of flushed bytes, we don't use that
   information any more.

Results:

An approximately 10% performance gain on the 100 concurrent streams
server-only benchmark. The relative performance gain shifts with the
number of active streams, but it should be positive in all cases.
2020-12-03 11:00:25 +00:00

58 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set +ex
# Benchmarks known to behave well under cachegrind.
# When adding new benchmarks, consider adding them to this list.
CACHEGRIND_BENCHES=( server_only_10k_requests_100_concurrent server_only_10k_requests_1_concurrent )
function run_bench {
BRANCH="$1"
echo "Running against $BRANCH"
git checkout "$BRANCH"
swift build -c release -Xswiftc -g -Xswiftc -DCACHEGRIND > /dev/null
for BENCH in "${CACHEGRIND_BENCHES[@]}"; do
valgrind --tool=cachegrind --cachegrind-out-file="cachegrind.out.$BENCH.$BRANCH" ./.build/x86_64-unknown-linux-gnu/release/NIOHTTP2PerformanceTester "$BENCH" > /dev/null
done
}
function compare {
BASE_BRANCH="$1"
TARGET_BRANCH="$2"
for BENCH in "${CACHEGRIND_BENCHES[@]}"; do
BASE_TOTAL=$(cg_annotate "cachegrind.out.$BENCH.$BASE_BRANCH" | grep "PROGRAM TOTALS" | awk '{ print $1 }')
TARGET_TOTAL=$(cg_annotate "cachegrind.out.$BENCH.$TARGET_BRANCH" | grep "PROGRAM TOTALS" | awk '{ print $1 }')
echo "Bench $BENCH result changed: base $BASE_TOTAL, target $TARGET_TOTAL"
done
}
if ! command -v valgrind &> /dev/null; then
echo "Valgrind not installed, please install valgrind and re-run"
exit 1
fi
BASE_BRANCH="$1"
TARGET_BRANCH="$2"
run_bench "$BASE_BRANCH"
run_bench "$TARGET_BRANCH"
compare "$BASE_BRANCH" "$TARGET_BRANCH"