0b26f7
#!/usr/bin/bash
0b26f7
# This script can be invoked as follows:
0b26f7
#
0b26f7
# glibc-bench-compare [options] <BUILD> [BUILD]
0b26f7
#
0b26f7
# Options may be one of the following:
0b26f7
#
0b26f7
# -t		The BUILD arguments are task ids and not a version-release string
0b26f7
# -a ARCH	Do comparison for ARCH architecture
0b26f7
#
0b26f7
# If any of the above options are given, both BUILD arguments must be given.
0b26f7
# Otherwise, if only one BUILD is specified, then it is compared against the
0b26f7
# installed glibc.
0b26f7
0b26f7
# Silence the pushd/popd messages
0b26f7
pushd() {
0b26f7
	command pushd "$@" > /dev/null 2>&1
0b26f7
}
0b26f7
0b26f7
popd() {
0b26f7
	command popd "$@" > /dev/null 2>&1
0b26f7
}
0b26f7
0b26f7
# Clean up any downloaded files before we exit
0b26f7
trap "rm -rf /tmp/glibc-bench-compare.$BASHPID.*" EXIT
0b26f7
0b26f7
task=0
0b26f7
arch=$(uname -i)
0b26f7
options=0
0b26f7
path=0
0b26f7
installed=
0b26f7
0b26f7
# Look for any commandline options
0b26f7
while getopts ":tpa:" opt; do
0b26f7
	case $opt in
0b26f7
		p)
0b26f7
		path=1
0b26f7
		;;
0b26f7
		t)
0b26f7
		task=1
0b26f7
		options=1
0b26f7
		echo "Not implemented."
0b26f7
		exit 1
0b26f7
		;;
0b26f7
		a)
0b26f7
		arch=$OPTARG
0b26f7
		options=1
0b26f7
		;;
0b26f7
		*)
0b26f7
		;;
0b26f7
	esac
0b26f7
done
0b26f7
0b26f7
# Done, now shift all option arguments out.
0b26f7
shift $((OPTIND-1))
0b26f7
0b26f7
if [ $# -gt 2 ] || [ $# -eq 0 ] || [ $# -lt 2 -a $options -eq 1 ]; then
0b26f7
	echo "Usage: $0 [OPTIONS] <old> [new]"
0b26f7
	echo
0b26f7
	echo "OPTIONS:"
0b26f7
	echo -e "\t-t\tCompare two brew tasks"
0b26f7
	echo -e "\t-a ARCH\tGet rpms for the ARCH architecture"
0b26f7
	echo -e "\t-p\tCompare built rpms in two paths."
0b26f7
	echo -e "\t\tThis minimally needs glibc, glibc-common and glibc-benchtests"
0b26f7
	exit 1
0b26f7
fi
0b26f7
0b26f7
if [ -z $2 ]; then
0b26f7
	new="$1"
0b26f7
	old=$(rpm --queryformat "%{VERSION}-%{RELEASE}\n" -q glibc | head -1)
0b26f7
	installed=$old
0b26f7
else
0b26f7
	new="$2"
0b26f7
	old="$1"
0b26f7
fi
0b26f7
0b26f7
decompress_rpms() {
0b26f7
	# We were given a path to the rpms.  Figure out the version-release and
0b26f7
	# decompress the rpms.
0b26f7
	if [ -n $1 ]; then
0b26f7
		vr=$(rpm --queryformat="%{VERSION}-%{RELEASE}" -qp $1/glibc-2*.rpm | head -1)
0b26f7
		mkdir $vr && pushd $vr
0b26f7
	fi
0b26f7
0b26f7
	for r in $1*.rpm; do
0b26f7
		( rpm2cpio $r | cpio -di ) > /dev/null
0b26f7
	done
0b26f7
0b26f7
	if [ -n $1 ]; then
0b26f7
		popd
0b26f7
		echo $vr
0b26f7
	fi
0b26f7
}
0b26f7
0b26f7
# Get rpms for a build and decompress them
0b26f7
get_build() {
0b26f7
	echo "Processing build $1"
0b26f7
	mkdir $1 && pushd $1
0b26f7
	brew buildinfo "glibc-$1" |
0b26f7
	sed -n -e "s|/mnt/koji\(.\+$arch.\+\)|http://kojipkgs.fedoraproject.org\1|p" |
0b26f7
	while read url; do
0b26f7
		echo "Downloading $url"
0b26f7
		wget -q $url
0b26f7
	done
0b26f7
	decompress_rpms
0b26f7
0b26f7
	echo "Removing rpms"
0b26f7
	rm -f $1/*.rpm
0b26f7
0b26f7
	popd
0b26f7
}
0b26f7
0b26f7
# Run benchmarks for a build
0b26f7
run_bench() {
0b26f7
	if [ -z $1 ]; then
0b26f7
		make DETAILED=1 ver=$installed prefix= -f /usr/libexec/glibc-benchtests/bench.mk bench
0b26f7
	else
0b26f7
		make DETAILED=1 ver=$1 prefix=$PWD -f $1/usr/libexec/glibc-benchtests/bench.mk bench
0b26f7
	fi
0b26f7
}
0b26f7
0b26f7
# Get absolute paths if needed, since we will change into the working directory
0b26f7
# next.
0b26f7
if [ $path -eq 1 ]; then
0b26f7
	old_path=$(realpath $old)/
0b26f7
	new_path=$(realpath $new)/
0b26f7
fi
0b26f7
0b26f7
tmpdir=$(mktemp -p /tmp -d glibc-bench-compare.$$.XXXX)
0b26f7
pushd $tmpdir
0b26f7
0b26f7
# Get both builds.
0b26f7
if [ $path -eq 0 ]; then
0b26f7
	if [ -z $installed ]; then
0b26f7
		get_build $old
0b26f7
	fi
0b26f7
	get_build $new
0b26f7
else
0b26f7
	old=$(decompress_rpms $old_path)
0b26f7
	new=$(decompress_rpms $new_path)
0b26f7
fi
0b26f7
0b26f7
# make bench for each of those.
0b26f7
if [ -z $installed ]; then
0b26f7
	run_bench $old
0b26f7
else
0b26f7
	run_bench
0b26f7
fi
0b26f7
run_bench $new
0b26f7
0b26f7
# Now run the comparison script.
0b26f7
$old/usr/libexec/glibc-benchtests/compare_bench.py $old/usr/libexec/glibc-benchtests/benchout.schema.json \
0b26f7
	bench.$old.out bench.$new.out