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