Skip to content

Instantly share code, notes, and snippets.

@travislee89
Last active May 9, 2017 12:42
Show Gist options
  • Save travislee89/9270e794ee92f01efbfc943f5022d067 to your computer and use it in GitHub Desktop.
Save travislee89/9270e794ee92f01efbfc943f5022d067 to your computer and use it in GitHub Desktop.
Compare {gzip, brotli, bzip2, xz} {compress, decompress} {ratio, speed, memory usage}.
#!/bin/bash
# Compare {gzip, brotli, bzip2, xz} {compress, decompress} {ratio, speed, memory usage}.
# Usage: bash compress_compare.sh <filename>
# You need to install (yum, dnf, apt or compile) brotli gzip bzip2 xz time first.
# https://github.com/google/brotli
# references: http://mattmahoney.net/dc/text.html
FILENAME=${1}
FILESIZE=$(stat --printf="%s" ${FILENAME} 2>/dev/null)
gz_compress () {
RESULT=$({ /usr/bin/time -v gzip -${2} ${1} -c > ${1}.${z}; } 2>&1)
RESULT_DE=$({ /usr/bin/time -v gzip -d ${1}.${z} -c > /dev/null; } 2>&1)
}
br_compress () {
rm -f "${1}.${z}";
RESULT=$({ /usr/bin/time -v brotli --input ${1} --quality ${2} --output ${1}.${z}; } 2>&1)
RESULT_DE=$({ /usr/bin/time -v brotli --decompress --input ${1}.${z} --output ${1}.tmp; } 2>&1)
rm -f "${1}.tmp";
}
bz2_compress () {
RESULT=$({ /usr/bin/time -v bzip2 -z -${2} ${1} -c > ${1}.${z}; } 2>&1)
RESULT_DE=$({ /usr/bin/time -v bzip2 -d ${1}.${z} -c > /dev/null; } 2>&1)
}
xz_compress () {
RESULT=$({ /usr/bin/time -v xz -z -${2} ${1} -c > ${1}.${z}; } 2>&1)
RESULT_DE=$({ /usr/bin/time -v xz -d ${1}.${z} -c > /dev/null; } 2>&1)
}
compress_result () {
${z}_compress ${1} ${2}
TIME=$(echo -e "${RESULT}" | grep "User time" | awk -F ": " '{ print $2 }')
MEM=$(echo -e "${RESULT}" | grep "Maximum resident set size" | awk -F ": " '{ print $2 }')
COMPRESSED_SIZE=$(stat --printf="%s" ${FILENAME}.${z} 2>/dev/null)
RATIO=$(echo ${COMPRESSED_SIZE} ${FILESIZE} | awk '{ printf "%0.4f" ,$1/$2 }')
TIME_DE=$(echo -e "${RESULT_DE}" | grep "User time" | awk -F ": " '{ print $2 }')
MEM_DE=$(echo -e "${RESULT_DE}" | grep "Maximum resident set size" | awk -F ": " '{ print $2 }')
echo -e "${z}\t${i}\t${FILESIZE}\t${COMPRESSED_SIZE}\t${RATIO}\t${TIME}\t${MEM}\t${TIME_DE}\t${MEM_DE}"
}
download_testdata () {
wget -c -t 5 \
https://raw.githubusercontent.com/jquery/jquery/f71eeda0fac4ec1442e631e90ff0703a0fb4ac96/dist/jquery.min.js \
https://raw.githubusercontent.com/vuejs/vue/b977c77d344cb1fc63a3daa50a4b96ef70e77ec5/dist/vue.min.js \
https://raw.githubusercontent.com/twbs/bootstrap/0b9c4a4007c44201dce9a6cc1a38407005c26c86/dist/css/bootstrap.min.css \
https://www.freebsd.org/doc/handbook/book.html
# jquery.min 3.2.1
# vue.min 2.3.2
# bootstrap.min 3.3.7
# freebsd handbook 50234
tar -cf testdata.tar jquery.min.js vue.min.js bootstrap.min.css book.html \
&& rm -f jquery.min.js vue.min.js bootstrap.min.css book.html
FILESIZE=$(stat --printf="%s" testdata.tar 2>/dev/null)
}
if [ -z "${1}" ] || [ ! -f "${1}" ]; then
#download_testdata
#FILENAME="testdata.tar"
echo "Please specify a valid file."
exit 1
## You can uncomment 'download_testdata' & 'FILENAME="testdata.tar"' and
## delete the next 2 lines to download testdata automatically.
fi
echo -e "format\tlevel\tsize\tsize_c\tratio\ttime\tmemory\ttime_de\tmemory_de"
for i in {1..9}; do
for z in gz br bz2 xz; do
compress_result ${FILENAME} ${i}
done
done
for i in {10..11}; do
z='br'
compress_result ${FILENAME} ${i}
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment