#!/bin/sh
# PCP QA Test No. 260
#
# Exercise delta() for derived metrics
#
# Copyright (c) 2009 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=0	# success is the default!
$sudo rm -rf $tmp.* $seq.full
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

export PCP_DERIVED_CONFIG=$tmp.config

# Derived metric expr dump from 0x8513a48...
# expr node 0x867eb68 type=PLUS left=0x867eb98 right=0x867ed28
_filter()
{
    cat $tmp.out >>$seq.full
    awk <$tmp.out >$tmp.sed '
BEGIN				{ n = 0 }
# expr node 0x9edc340 type=PLUS left=0x9edc2f8 right=0x9edc370
$1 == "expr" && $2 == "node" && $3 ~ /^0x/	{ print "s/" $3 "/<addr-" n ">/"; n++ }
				{ next }'
    echo "=== sed ===" >>$seq.full
    cat $tmp.sed >>$seq.full
    sed -f $tmp.sed <$tmp.out \
    | sed \
	-e '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/s/[^ ]*.*numpmid/TIMESTAMP ... numpmid/' \
	-e 's/=0x0 /=(nil) /g' \
	-e "s;$tmp;TMP;"
# -e 's/ val=[0-9][0-9]*/ val=<number>/g'
}

# tolerate some arithmetic "slop" in the reported values ... mostly
# due to timer sensitivity for sample intervals at client and at the
# PMDAs
#
_filter_pmie()
{
    tee -a $seq.full \
    | $PCP_AWK_PROG '
BEGIN			{ sample = 1 }
$1 ~ /^debug_/		{ next }
NF == 0			{ sample++ }
$2 == "?"		{ print; next }
$1 == "r_xpect_one_quarter:"	{ if ($2 >= 0.246 && $2 <= 0.254) $2 = "0.25"; else $0 = $0 " !bad" }
$1 == "pmie_xpect_120:"	{ if ($2 >= 110.0 && $2 <= 130.0) $2 = "120"; else $0 = $0 " !bad" }
$1 == "pmie_xpect_360:"	{ if ($2 >= 330.0 && $2 <= 390.0) $2 = "360"; else $0 = $0 " !bad" }
$1 == "derived_xpect_120:"	{ if (sample < 3) $2 = "<skipped>"
				  else if ($2 >= 110.0 && $2 <= 130.0) $2 = "120"; else $0 = $0 " !bad" }
$1 == "derived_xpect_360:"	{ if (sample < 3) $2 = "<skipped>"
				  else if ($2 >= 330.0 && $2 <= 390.0) $2 = "360"; else $0 = $0 " !bad" }
			{ print }'
}

# initialize counters used in expressions
#
pmstore sample.const_rate.gradient 120 >>$seq.full 2>&1
pmstore sample.const_rate.value 0 >>$seq.full 2>&1
pmstore sampledso.const_rate.gradient 360 >>$seq.full 2>&1
pmstore sampledso.const_rate.value 0 >>$seq.full 2>&1

# real QA test starts here

echo "=== expression trees and pmDesc propagation ===" | tee -a $seq.full
cat <<End-of-File >$tmp.config
delta.m1 = delta(sample.const_rate.value)
delta.m2 = delta(sampledso.const_rate.value) + sample.bigid
delta.m3 = sample.bigid - delta(sampledso.const_rate.value)
delta.m4 = sample.bigid - 3 * delta(sampledso.const_rate.value) / sample.long.ten
delta.m5 =  delta(sample.const_rate.value) / ( delta(sample.const_rate.value) + delta(sampledso.const_rate.value) )
delta.m6 =  delta(sample.const_rate.value) / delta(sample.const_rate.value) - delta(sampledso.const_rate.value) / delta(sample.const_rate.value)
End-of-File
echo
cat $tmp.config

for args in delta
do
    echo
    echo "=== $args ==="
    pminfo -Dderive,appl0,appl1 -dm $args >$tmp.out 2>&1
    _filter
done

# how expected values are determined ...
#
# sample.const_rate.value is increasing 120 per second
# sampledso.const_rate.value is increasing 360 per second
# pmie is sampling @ 0.5 second intervals, so the derived metrics
# will increase at half this rate, but pmie converts back to a rate
# per second (so 120.0 and 360.0)
# r = 60 / (60 + 180) = 0.25
# all of the others are constant, so delta() is zero
#
echo | tee -a $seq.full
echo "=== fetch values exercises ===" | tee -a $seq.full
cat <<End-of-File >$tmp.config
d1 = delta(sample.const_rate.value)
d2 = delta(sampledso.const_rate.value)
ratio =  delta(sample.const_rate.value) / ( delta(sample.const_rate.value) + delta(sampledso.const_rate.value) )
delta_l = delta(sample.long.hundred)
delta_ull = delta(sample.ulonglong.hundred)
delta_f = delta(sample.float.hundred)
delta_d = delta(sample.double.hundred)
End-of-File

cat <<End-of-File \
| pmie -h localhost -v -t 1 -T '+10sec' 2>&1 \
| sed \
    -e 's/.*Info: evaluator exiting/pmie: note - evaluator exiting/g' \
| _filter_pmie
debug_1 = sample.const_rate.value;
debug_2 = sampledso.const_rate.value;
debug_3 = d1;
debug_4 = d2;
r_xpect_one_quarter = ratio;
l_xpect_0 = delta_l;
ull_xpect_0 = delta_ull;
f_xpect_0 = delta_f;
d_xpect_0 = delta_d;
pmie_xpect_120 = sample.const_rate.value;
derived_xpect_120 = d1;
pmie_xpect_360 = sampledso.const_rate.value;
derived_xpect_360 = d2;
End-of-File

# success, all done
exit
