#! /bin/sh # compbench, a compressor benchmarking script. # usage: compbench file command1 [command2 ...] # For example: # $ compbench test.tar cat lz4 'gzip -9' 'zstd --long -19' # Tested on Ubuntu 22.04, Debian GNU/Linux 11, # FreeBSD 13.1-RELEASE, NetBSD 9.3, and OpenBSD 7.2. # # Copyright (c) 2020-2023 D. Bohdan # # Permission to use, copy, modify, and/or distribute this software # for any purpose with or without fee is hereby granted. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE # AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. file="$1" if [ ! -f "$file" ]; then echo 'no file' exit 1 fi shift if [ -z "$1" ]; then echo 'no compressor commands' exit 1 fi origsize="$(wc -c "$file" | awk '{ print $1 }')" tempsize="$(mktemp)" temptime="$(mktemp)" cleanup() { rm "$tempsize" "$temptime" } trap cleanup EXIT if [ "$(uname)" = Linux ]; then # GNU, BusyBox. arg1=-f arg2q='%e real\n%M maximum resident size' else # DragonFly/Free/Net/OpenBSD. arg1= arg2q=-l fi first=1 for comp in "$@"; do if [ "$first" = 1 ]; then first=0 else printf '\n' fi echo "=== $comp" command time $arg1 "$arg2q" $comp < "$file" 2> "$temptime" \ | wc -c \ | awk -v "origsize=$origsize" ' { printf "%8.2f MiB compressed\n", $1 / 1024 / 1024 if (origsize > 0) { printf "%8.2f ratio\n", $1 / origsize } } ' > "$tempsize" awk ' /real/ { m = $1 / 60 s = $1 % 60 cs = $1 * 100 % 100 printf "%2u:%02u.%02u elapsed\n", m, s, cs } /maximum resident/ { printf "%8.2f MiB max RSS\n", $1 / 1024 } ' "$temptime" cat "$tempsize" done