public function metricAverageDoesntExceed($metric, $expectedAverage, $startTime, $endTime)
{
// when are we looking for?
$humanStartTime = date('Y-m-d H:i:s', $startTime);
$humanEndTime = date('Y-m-d H:i:s', $endTime);
// what are we doing?
$log = usingLog()->startAction("ensure metric '{$metric}' average never exceeds value '{$expectedAverage}' between '{$humanStartTime}' and '{$humanEndTime}'");
// get the data from graphite
$data = fromGraphite()->getDataFor($metric, $startTime, $endTime);
// do we *have* any data?
if (empty($data) || !isset($data[0]->target, $data[0]->datapoints)) {
// graphite returns an empty data set when there is no data
//
// NOTE: this can also happen when the test is asking for
// the wrong metric :(
if ($expectedAverage !== 0) {
// we were expecting there to be some data
throw new E5xx_ExpectFailed(__METHOD__, "data for metric '{$metric}'", "no data available for metric '{$metric}'");
}
// if we get here, it's reasonable to assume that everything is
// as it should be
$log->endAction("no data available for metric '{$metric}'; assuming success");
return;
}
// we have data ... let's make sure we're happy with it
$total = 0;
$count = 0;
foreach ($data[0]->datapoints as $datapoint) {
if ($datapoint[0] !== null) {
$total += $datapoint[0];
$count++;
}
}
// what is the average?
$average = $total / $count;
// are we happy?
assertsDouble($average)->isLessThanOrEqualTo($expectedAverage);
// all done
$log->endAction("data was available, metric '{$metric}' never exceeds '{$expectedAverage}'");
return;
}