Skip to content

Instantly share code, notes, and snippets.

@LuRenJiasWorld
Last active May 5, 2024 08:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save LuRenJiasWorld/7de95bbfdba542cea17271ba06ccb85a to your computer and use it in GitHub Desktop.
Save LuRenJiasWorld/7de95bbfdba542cea17271ba06ccb85a to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eu
algos=(lzo lzo-rle lz4 zstd)
page_clusters=(0 1 2 3)
# This testfile was produced by using my machine for a while with 5 GB of zram
# swap and very high swappiness, then dumping, compressing, and encrypting the
# zram device. So it should be reasonably representative of what gets swapped
# out.
testfile_path="./test-memory.bin"
require_testfile () {
[ -f "$testfile_path" ] && return 0
echo "Building test file; I hope your zram is full..."
(
umask 0077
pv -s 512M -S /dev/zram0 > "$testfile_path"
)
}
require_miti_off () {
if grep -q mitigations=off /proc/cmdline; then
return 0
else
echo "Please run benchmark with mitigations=off, because fio uses" \
"syscalls for its I/O but the kernel will not need to."
exit 1
fi
}
require_perf_gov () {
# For reproducibility
cpupower frequency-set -g performance
}
get_prepared_zram_dev () {
local algo="$1"
local zramdev
zramdev="$(zramctl --find --size=512M --algorithm "$algo")"
echo "got $zramdev; filling with test data..." 1>&2
# Write the zram with dd bs=4KiB to be as similar as possible to kernel
# swap behavior.
cat "$testfile_path" | dd of="$zramdev" bs=4KiB oflag=direct status=progress
echo "$zramdev"
}
run_fio_benchmark () {
local page_cluster="$1" zramdev="$2" outfile="$3" blocksize
blocksize="$(( 4 * ( 1 << page_cluster ) ))k"
fio \
--readonly \
--name=randread \
--direct=1 \
--rw=randread \
--ioengine=psync \
--randrepeat=0 \
--bs="$blocksize" \
--numjobs="$(grep -c processor /proc/cpuinfo)" \
--iodepth=1 \
--group_reporting=1 \
--time_based=1 \
--runtime=60 \
--filename="$zramdev" \
--output-format=json \
--output="$outfile"
sleep 0.1 # fio with background threads behaving badly
}
get_compression_ratio () {
local zramdev="$1"
local stored used
read stored _ used _ <"/sys/block/${zramdev#/dev/}/mm_stat"
echo "scale=2; $stored/$used" | bc
}
run_all_tests () {
local resultdir zramdev
for algo in ${algos[*]}; do
resultdir="./fio-bench-results/$algo"
mkdir -p "$resultdir"
zramdev="$(get_prepared_zram_dev "$algo")"
get_compression_ratio "$zramdev" >"$resultdir/compratio"
for page_cluster in ${page_clusters[*]}; do
echo "Testing $algo with page-cluster=$page_cluster..."
run_fio_benchmark \
"$page_cluster" \
"$zramdev" \
"$resultdir/pc-$page_cluster.json"
done
zramctl -r "$zramdev" # free the zram
done
}
main () {
require_miti_off
require_perf_gov
require_testfile
run_all_tests
}
## Data workup
produce_csv () {
file_to_record () {
local file="$1"
local algo page_cluster
read algo page_cluster < <(
echo "$file" \
| awk -F '/' '{gsub("[^[:digit:]]","",$NF); print $(NF-1),$NF}'
)
echo "$algo,$page_cluster,$(<"$file" jq -r '.jobs[0].read | [ .bw_mean / 1024, .iops_mean, .clat_ns.mean, .clat_ns.percentile."99.000000" ] | @csv')"
}
echo 'algo,page-cluster,"MiB/s","IOPS","Mean Latency (ns)","99% Latency (ns)"'
while read filename; do
file_to_record "$filename"
done < <(find ./fio-bench-results -name '*.json')
}
if [ $UID -eq 0 ]; then
main
else
echo "Not root, dumping results instead." 1>&2
produce_csv
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment