1#!/bin/bash
2#
3# Copyright 2017, Joe Tsai. All rights reserved.
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE.md file.
6
7if [ $# == 0 ]; then
8	echo "Usage: $0 PKG_PATH TEST_ARGS..."
9	echo ""
10	echo "Runs coverage and performance benchmarks for a given package."
11	echo "The results are stored in the _zprof_ directory."
12	echo ""
13	echo "Example:"
14	echo "	$0 flate -test.bench=Decode/Twain/Default"
15	exit 1
16fi
17
18DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19PKG_PATH=$1
20PKG_NAME=$(basename $PKG_PATH)
21shift
22
23TMPDIR=$(mktemp -d)
24trap "rm -rf $TMPDIR $PKG_PATH/$PKG_NAME.test" SIGINT SIGTERM EXIT
25
26(
27	cd $DIR/$PKG_PATH
28
29	# Print the go version.
30	go version
31
32	# Perform coverage profiling.
33	go test github.com/dsnet/compress/$PKG_PATH -coverprofile $TMPDIR/cover.profile
34	if [ $? != 0 ]; then exit 1; fi
35	go tool cover -html $TMPDIR/cover.profile -o cover.html
36
37	# Perform performance profiling.
38	if [ $# != 0 ]; then
39		go test -c github.com/dsnet/compress/$PKG_PATH
40		if [ $? != 0 ]; then exit 1; fi
41		./$PKG_NAME.test -test.cpuprofile $TMPDIR/cpu.profile -test.memprofile $TMPDIR/mem.profile -test.run - "$@"
42		PPROF="go tool pprof"
43		$PPROF -output=cpu.svg          -web                      $PKG_NAME.test $TMPDIR/cpu.profile 2> /dev/null
44		$PPROF -output=cpu.html         -weblist=.                $PKG_NAME.test $TMPDIR/cpu.profile 2> /dev/null
45		$PPROF -output=mem_objects.svg  -alloc_objects -web       $PKG_NAME.test $TMPDIR/mem.profile 2> /dev/null
46		$PPROF -output=mem_objects.html -alloc_objects -weblist=. $PKG_NAME.test $TMPDIR/mem.profile 2> /dev/null
47		$PPROF -output=mem_space.svg    -alloc_space   -web       $PKG_NAME.test $TMPDIR/mem.profile 2> /dev/null
48		$PPROF -output=mem_space.html   -alloc_space   -weblist=. $PKG_NAME.test $TMPDIR/mem.profile 2> /dev/null
49	fi
50
51	rm -rf $DIR/_zprof_/$PKG_NAME
52	mkdir -p $DIR/_zprof_/$PKG_NAME
53	mv *.html *.svg $DIR/_zprof_/$PKG_NAME 2> /dev/null
54)
55