#!/bin/sh

# Filesystem benchmarking script created by Peter Nelson
# Be carefull using this script as it does forced mkfs's and
# rm -rf *'s so there's the possibility of it wiping something
# important.
# My results are at:
#  http://rufus.hackish.org/wiki/2.6FileSystemBenchmarks

DEVICE=			# device to create partition on
MNT=/mnt		# where to mount partition
DATADIR=~/test		# directory to store results and temporary debs
KVER=2.6.3		# Kernel version to install/build
# debian mirror to use
MIRROR=http://debian.mirrors.pair.com
# extra debs to install (*must* resolve dependencies manualy)
EXTRAS=kernel-source-$KVER,binutils,bzip2,libbz2-1.0,ccache,gcc-3.3,cpp-3.3
# a bunch more extras because exim is being horribly broken
EXTRAS=$EXTRAS,libgnutls10,libtasn1-2,libgcrypt7,libgpg-error0,libopencdk8


# End of configuration, begin running the benchmark
[ -z $DEVICE ] && echo "Please set what device to use" && exit

# format TIME output nicely
export TIME="%U\t%S\t%E\t%P"
# compile using installed ccache and gcc
CC="$MNT/usr/bin/ccache $MNT/usr/bin/gcc-3.3"
# make ccache use mounted filesystem
export CCACHE_DIR=$MNT/root/.ccache

# download the packages the first time into DATADIR (packages go into the .../var)
echo "downloading debs"
debootstrap --download-only --include=$EXTRAS sid $DATADIR $MIRROR > /dev/null

for create in ext2 ext3 "jfs -f" "reiser4 -q" "reiserfs -q" "xfs -f"
do
	FS=`echo $create | cut -d ' ' -f 1`
	echo "testing $FS"
	LOG=$DATADIR/time.$FS

	echo $FS > $LOG
	echo -e "test\tuser\tsys\treal\tCPU" >> $LOG

	mkfs -t $create $DEVICE >/dev/null || break 
	mount $DEVICE $MNT || break

	echo "$FS: copying debs"
	cp -a $DATADIR/var $MNT
	sync

	echo "$FS: installing"
	echo -ne "deb\t" >> $LOG
	/usr/bin/time -o $LOG -a debootstrap --include=$EXTRAS sid $MNT $MIRROR > /dev/null 2>&1 || break
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	echo "$FS: untaring"
	echo -ne "tar\t" >> $LOG
	/usr/bin/time -o $LOG -a tar jxf $MNT/usr/src/kernel-source-$KVER.tar.bz2 -C $MNT/usr/src
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	echo "$FS: configuring"
	cd $MNT/usr/src/kernel-source-$KVER
	make defconfig CC="$CC" > /dev/null 2>&1
	sync

	echo "$FS: building"
	echo -ne "make\t" >> $LOG
	/usr/bin/time -o $LOG -a make all CC="$CC" > /dev/null 2>&1
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	echo "$FS: copying"
	mkdir $MNT/z
	echo -ne "cp\t" >> $LOG
	/usr/bin/time -o $LOG -a cp -a $MNT/[^z]* $MNT/z
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	echo "$FS: cleaning"
	echo -ne "clean\t" >> $LOG
	/usr/bin/time -o $LOG -a make clean > /dev/null
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	echo "$FS: building"
	echo -ne "make2\t" >> $LOG
	/usr/bin/time -o $LOG -a make all CC="$CC" > /dev/null 2>&1
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	# get out of the kernel/mounted partition
	cd $DATADIR

	echo "$FS: deleting"
	echo -ne "rm\t" >> $LOG
	/usr/bin/time -o $LOG -a rm -rf $MNT/*
	echo -ne "sync\t" >> $LOG
	/usr/bin/time -o $LOG -a sync

	umount $MNT || break
done
