Skip to content

Instantly share code, notes, and snippets.

@GRBaset
Last active December 8, 2021 14:50
Show Gist options
  • Save GRBaset/dfa1b19ca4eaf54f4fe2ce9cd403615d to your computer and use it in GitHub Desktop.
Save GRBaset/dfa1b19ca4eaf54f4fe2ce9cd403615d to your computer and use it in GitHub Desktop.
RAM disk ZFS benchmark for macOS and Linux
#!/bin/bash
set -euo pipefail
benchmark() {
# Create a 1000 MiB zero-filled file with DD and write the output to a file to log the speed, then remove it.
echo "Running benchmark for $1..." | tee -a "$logfile"
dd if=/dev/zero of="$mountroot/$rampool/$1/test" bs=104857600 count=10 2>&1 | tee -a "$logfile"
rm "$mountroot/$rampool/$1/test"
}
testkey="$PWD/testkey-$(date "+%s")" # Location of test key.
logfile="$PWD/ramdisktest-$(date "+%s").log" # Location of logfile.
username=$(whoami)
ramsectors=4194304 # Number of 512-byte sectors to allocate for RAM disk. 2 GiB = 4194304 sectors.
rampool="ram" # Name of ZPOOL.
# Detect OS and create RAM disk and set mount point
if [[ $OSTYPE == "darwin"* ]]; then
mountroot="/Volumes"
ramdisk=$(hdiutil attach -nomount "ram://$ramsectors" | sed 's/[[:space:]]*//g')
elif [[ $OSTYPE == "linux-gnu" ]]; then
mountroot="/"
sudo modprobe brd rd_nr=1 rd_size="$((ramsectors / 2))"
ramdisk="/dev/ram0"
else
echo 'Error: unknown OS'
exit
fi
# Create random test key and ZPOOL.
dd if=/dev/urandom of="$testkey" bs=32 count=1
sudo zpool create "$rampool" "$ramdisk"
# Create ZFS datasets and change ownership to avoid running the benchmark as root.
sudo zfs create "$rampool/noencryption"
sudo zfs create -o encryption=aes-128-ccm -o keyformat=raw -o keylocation="file://$testkey" "$rampool/aes128ccm"
sudo zfs create -o encryption=aes-256-ccm -o keyformat=raw -o keylocation="file://$testkey" "$rampool/aes256ccm"
sudo zfs create -o encryption=aes-128-gcm -o keyformat=raw -o keylocation="file://$testkey" "$rampool/aes128gcm"
sudo zfs create -o encryption=aes-256-gcm -o keyformat=raw -o keylocation="file://$testkey" "$rampool/aes256gcm"
sudo chown "$username" "$mountroot/$rampool/"*
# Run benchmark.
benchmark 'noencryption'
benchmark 'aes128ccm'
benchmark 'aes256ccm'
benchmark 'aes128gcm'
benchmark 'aes256gcm'
# Clean up by destroying the ZPOOL and removing the test key.
sudo zpool destroy "$rampool"
rm "$testkey"
# Detect OS and remove the RAM disk
if [[ $OSTYPE == "darwin"* ]]; then
hdiutil detach "$ramdisk"
elif [[ $OSTYPE == "linux-gnu" ]]; then
sudo modprobe -r brd
else
echo 'Error: unknown OS'
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment