GZip has 9 compression levels, which one is the most appropriate for the system, to assure the balance between compress speed and ratio, especially on server side, it’s generally neglected, but indeed important.
As compression mainly depends on CPU, the compression level may vary on different CPUs. Here is a simple bash script attempted to give some information on determining the level.
As it will test with dropcache, so you need at least kernel 2.6.16, since which the dropcache was tunable.
Use with CAUTION, do NOT test on production environment.
#!/bin/sh
# Written by Cowyn Li <cowynli_#_gmail.com>.
# for testing GZip compression levels
set -e
dropcache="0 1"
size="10240 102400 1024000 10240000"
tmp="./tmp"
log="./gzip.log"
finallog="gzip.diff"
rm -rf ${tmp} ${log}* 2> /dev/null
mkdir -p ${tmp}
for l in ${dropcache}; do
for m in ${size}; do
dd if=/dev/zero of=${tmp}/file.${m} bs=${m} count=1 2> /dev/null
for n in $(seq 9); do
echo -e "gzip level:\t\t${n}" >> ${log}.${l}
[ ${l} -eq 1 ] && sudo sync && sudo echo 3 > /proc/sys/vm/drop_caches
/usr/bin/time "-ftime real\t\t%e" -o ${log}.${l} -a gzip -nv${n} \
${tmp}/file.${m} > /dev/null 2>> ${log}.${l}
[ ${l} -eq 1 ] && sudo echo 0 > /proc/sys/vm/drop_caches
echo -e "compressed:\t\t$(stat -c%s ${tmp}/file.${m}.gz)\n" >> ${log}.${l}
gzip -d ${tmp}/file.${m}.gz
done
echo "--------------------" >> ${log}.${l}
done
done
diff -u ${log}.$(echo ${dropcache}|cut -d' ' -f1) \
${log}.$(echo ${dropcache}|cut -d' ' -f2) > ${finallog} || true
echo -n "Log of gzip test WITHOUT fs-dropcache is at "
echo "\"${log}.$(echo ${dropcache}|cut -d' ' -f1)\"."
echo "while the one WITH is at \"${log}.$(echo ${dropcache}|cut -d' ' -f2)\"."
echo "Check the diff log between the two at \"${finallog}\"."
I’m afraid the dropcache test doesn’t make much sense, added here for just in case somebody think it should be token in consideration.
The suffix of tmp-file(dded) names indicates the file size, in bytes. No more explanations, please read and run and think for yourselves, if you wish.
For my situation, I’m using compression level 4 within Nginx, on a Pentium 4 2.8GHz, 512 KB cache size CPU.