#! /bin/bash

## Usage: performace <build-path> <width> <height> <loop-cnt>

P=${1:-`pwd -P`}
W=${2:-1000}
H=${3:-1000}
CNT=${4:-100}

: ${FMT:=gbrg}

TESTS=(
    "00-simple --quality -all"
    "01-round4 --quality +round4"
    "02-round2 --quality +round2"
    "03-round  --quality +round"
)

METHODS=(
    "c      --method c"
    "cc     --method cc"
    "c-opt  --method c-opt"
    "neon   --method neon"
)

######

tmp=`mktemp -d -t bayer-perf.XXXXXX` || exit 1
trap "rm -rf $tmp" EXIT

function info {
    local fmt=$1
    shift

    printf "$fmt\n" "$@"
}

function run {
    libtool --mode=execute ${DEBUG:-env} ${QEMU:-} "$@"
}

set -e

${QEMU:-} $P/testsuite/gen-image --width "$W" --height "$H" -M random --seed 42 > $tmp/image

for t in "${TESTS[@]}"; do
    args=( --width "$W" --height "$H" --loop "$CNT" --silent --timing --input-format "$FMT" )

    eval set -- $t

    testid=$1
    shift

    args+=( "$@" )

    for m in "${METHODS[@]}"; do
	eval set -- $m

	methid=$1
	shift

	suffix=$testid:$methid

	echo "testid" > "$tmp/testid:$suffix"
	echo "methid" > "$tmp/methid:$suffix"

	run bayer2rgb "${args[@]}" "$@" \
	    "$tmp/image" /dev/null > "$tmp/perf:$suffix"

	grep 'Raw conversion time:' "$tmp/perf:$suffix" | \
	    cut -d ':' -f 2 | awk '{ print $2; }' > "$tmp/raw:$suffix"

	grep 'Total conversion time:' "$tmp/perf:$suffix" | \
	    cut -d ':' -f 2 | awk '{ print $2; }' > "$tmp/total:$suffix"
    done
done

for f in "$tmp/perf:"*; do
    id=${f##$tmp/perf:}

    read raw   < $tmp/raw:$id
    read total < $tmp/total:$id

    printf "%-20s:\t%s\t%s\n" "$id" "$raw" "$total"
done
