Advertisement
Guest User

better_benchmarks.bash

a guest
Apr 27th, 2021
2,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.78 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -eu
  4.  
  5. algos=(lzo lzo-rle lz4 zstd)
  6. page_clusters=(0 1 2 3)
  7.  
  8. # This testfile was produced by using my machine for a while with 5 GB of zram
  9. # swap and very high swappiness, then dumping, compressing, and encrypting the
  10. # zram device. So it should be reasonably representative of what gets swapped
  11. # out.
  12. testfile_path="/$redacted/scratch/zram.zstd.gpg"
  13.  
  14. require_testfile () {
  15.     [ -f "$testfile_path" ] && return 0
  16.     echo "Building test file; I hope your zram is full..."
  17.     (
  18.         umask 0077 # it'll be encrypted, but belt-and-suspenders
  19.         pv -s 4G -S /dev/zram0 \
  20.             | zstd -10 -T0 --long \
  21.             | gpg --symmetric \
  22.             >"$testfile_path"
  23.     )
  24. }
  25.  
  26. require_miti_off () {
  27.     if grep -q mitigations=off /proc/cmdline; then
  28.         return 0
  29.     else
  30.         echo "Please run benchmark with mitigations=off, because fio uses" \
  31.             "syscalls for its I/O but the kernel will not need to."
  32.         exit 1
  33.     fi
  34. }
  35.  
  36. require_perf_gov () {
  37.     # For reproducibility
  38.     cpupower frequency-set -g performance
  39. }
  40.  
  41. get_prepared_zram_dev () {
  42.     local algo="$1"
  43.     local zramdev
  44.     zramdev="$(zramctl --find --size=4G --algorithm "$algo")"
  45.     echo "got $zramdev; filling with test data..." 1>&2
  46.     # Write the zram with dd bs=4KiB to be as similar as possible to kernel
  47.     # swap behavior.
  48.     gpg --decrypt "$testfile_path" \
  49.         | zstd -T0 -d \
  50.         | dd of="$zramdev" bs=4KiB oflag=direct status=progress
  51.     echo "$zramdev"
  52. }
  53.  
  54. run_fio_benchmark () {
  55.     local page_cluster="$1" zramdev="$2" outfile="$3" blocksize
  56.     blocksize="$(( 4 * ( 1 << page_cluster ) ))k"
  57.     fio \
  58.         --readonly \
  59.         --name=randread \
  60.         --direct=1 \
  61.         --rw=randread \
  62.         --ioengine=psync \
  63.         --randrepeat=0 \
  64.         --bs="$blocksize" \
  65.         --numjobs="$(grep -c processor /proc/cpuinfo)" \
  66.         --iodepth=1 \
  67.         --group_reporting=1 \
  68.         --time_based=1 \
  69.         --runtime=60 \
  70.         --filename="$zramdev" \
  71.         --output-format=json \
  72.         --output="$outfile"
  73.     sleep 0.1 # fio with background threads behaving badly
  74. }
  75.  
  76. get_compression_ratio () {
  77.     local zramdev="$1"
  78.     local stored used
  79.     read stored _ used _ <"/sys/block/${zramdev#/dev/}/mm_stat"
  80.     echo "scale=2; $stored/$used" | bc
  81. }
  82.  
  83. run_all_tests () {
  84.     local resultdir zramdev
  85.     for algo in ${algos[*]}; do
  86.         resultdir="./fio-bench-results/$algo"
  87.         mkdir -p "$resultdir"
  88.         zramdev="$(get_prepared_zram_dev "$algo")"
  89.         get_compression_ratio "$zramdev" >"$resultdir/compratio"
  90.         for page_cluster in ${page_clusters[*]}; do
  91.             echo "Testing $algo with page-cluster=$page_cluster..."
  92.             run_fio_benchmark \
  93.                 "$page_cluster" \
  94.                 "$zramdev" \
  95.                 "$resultdir/pc-$page_cluster.json"
  96.         done
  97.         zramctl -r "$zramdev" # free the zram
  98.     done
  99. }
  100.  
  101. main () {
  102.     require_miti_off
  103.     require_perf_gov
  104.     require_testfile
  105.     run_all_tests
  106. }
  107.  
  108. ## Data workup
  109.  
  110. produce_csv () {
  111.     file_to_record () {
  112.         local file="$1"
  113.         local algo page_cluster
  114.         read algo page_cluster < <(
  115.             echo "$file" \
  116.             | awk -F '/' '{gsub("[^[:digit:]]","",$NF); print $(NF-1),$NF}'
  117.         )
  118.         echo "$algo,$page_cluster,$(<"$file" jq -r '.jobs[0].read | [ .bw_mean / 1024, .iops_mean, .clat_ns.mean, .clat_ns.percentile."99.000000" ] | @csv')"
  119.     }
  120.     echo 'algo,page-cluster,"MiB/s","IOPS","Mean Latency (ns)","99% Latency (ns)"'
  121.     while read filename; do
  122.         file_to_record "$filename"
  123.     done < <(find ./fio-bench-results -name '*.json')
  124. }
  125.  
  126.  
  127. if [ $UID -eq 0 ]; then
  128.     main
  129. else
  130.     echo "Not root, dumping results instead." 1>&2
  131.     produce_csv
  132. fi
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement