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