38#include <boost/algorithm/string.hpp>
39#include <boost/asio/ip/host_name.hpp>
40#include <boost/core/ignore_unused.hpp>
41#include <boost/lexical_cast.hpp>
69 if (
m_type !=
"EXECUTIONTIME")
75 TiXmlElement *regex = metric->FirstChildElement(
"regex");
79 ASSERTL0(regex->GetText(),
"Failed to read regex element.");
85 m_regex =
"^.*Total Computation Time\\s*=\\s*(\\d+\\.?\\d*).*";
98 TiXmlElement *value = metric->FirstChildElement(
"value");
101 bool hostnameMatch =
false;
104 ASSERTL0(value->Attribute(
"tolerance"),
105 "Missing tolerance in execution time metric.");
106 ASSERTL0(value->Attribute(
"hostname"),
107 "Missing hostname in execution time metric.");
109 "Missing value in execution time metric.");
112 if (value->Attribute(
"hostname") == boost::asio::ip::host_name())
115 val.
m_value = value->GetText();
116 val.
m_tolerance = atof(value->Attribute(
"tolerance"));
119 hostnameMatch =
true;
125 value = value->NextSiblingElement(
"value");
131 cerr <<
"WARNING: No execution time value provided for host "
132 << boost::asio::ip::host_name() <<
". Skipping metric." << endl;
156 boost::ignore_unused(pStdout, pStderr);
163 boost::cmatch matches;
166 vector<double> times;
167 bool matched =
false;
170 while (getline(is, line))
173 if (boost::regex_match(line.c_str(), matches,
m_regex))
176 if (matches.size() == 1)
178 cerr <<
"No test sections in regex!" << endl;
183 for (
int i = 1; i < matches.size(); ++i)
186 string match(matches[i].first, matches[i].second);
190 val = boost::lexical_cast<double>(match);
193 times.push_back(val);
196 catch (boost::bad_lexical_cast &e)
198 cerr <<
"Could not convert match " << match <<
" to double"
209 cerr <<
"No execution times were found in the test output. Consider "
210 "providing a custom regex for this test metric."
216 double avgTime = 0.0;
217 for (
unsigned int i = 0; i < times.size(); ++i)
221 avgTime /= times.size();
227 cerr <<
"Average execution time for host "
228 << boost::asio::ip::host_name() <<
": " << avgTime << endl;
233 "No test conditions defined for execution time.");
236 if (fabs(avgTime - boost::lexical_cast<double>(
m_match.
m_value)) >
241 cerr <<
"Failed tolerance match." << endl;
242 cerr <<
" Expected avg execution time: " <<
m_match.
m_value <<
" +/- "
244 cerr <<
" Actual avg: " << avgTime << endl;
246 for (
unsigned int i = 0; i < times.size(); ++i)
248 cerr <<
" Run " << i <<
": " << times[i] << endl;
269 boost::ignore_unused(pStderr);
274 boost::cmatch matches;
278 vector<double> sampleTimes;
279 bool matched =
false;
282 while (getline(is, line))
285 if (boost::regex_match(line.c_str(), matches,
m_regex))
288 ASSERTL0(matches.size() != 1,
"No test sections in regex!");
291 for (
int i = 1; i < matches.size(); ++i)
294 string match(matches[i].first, matches[i].second);
298 val = boost::lexical_cast<double>(match);
300 sampleTimes.push_back(val);
303 catch (boost::bad_lexical_cast &e)
305 cerr <<
"Could not convert match " << match <<
" to double"
319 double avgTime = 0.0;
320 for (
unsigned int i = 0; i < sampleTimes.size(); ++i)
322 avgTime += sampleTimes[i];
324 avgTime /= sampleTimes.size();
326 okValue.
m_value = to_string(avgTime);
331 cerr <<
"No execution times were found in the test output. Value set "
337 if (
m_type ==
"EXECUTIONTIME")
340 while (
m_metric->FirstChildElement(
"value"))
344 "Couldn't remove value from metric!");
347 TiXmlElement *val =
new TiXmlElement(
"value");
349 val->SetAttribute(
"tolerance",
351 val->SetAttribute(
"hostname", boost::asio::ip::host_name());
#define ASSERTL0(condition, msg)
bool m_useStderr
If true, use stderr for testing/generation instead of stdout.
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
MetricExecutionTime(TiXmlElement *metric, bool generate)
Construct a new MetricExecutionTime object.
MetricExecutionTimeFieldValue m_match
Stores each execution time found in the test output.
boost::regex m_regex
Regex used to match an execution time in a test output.
virtual void v_Generate(std::istream &pStdout, std::istream &pStderr)
Generate an accepted execution time value using a test output or error stream.
virtual bool v_Test(std::istream &pStdout, std::istream &pStderr)
Test output against a regular expression and its expected value.
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Base class for all metrics. Metric represents a test metric that can be used to evaluate the function...
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
bool m_average
Indicates whether a metric supports averaging results from multiple runs.
std::string m_type
Stores the type of this metric (uppercase).
bool m_generate
Determines whether to generate this metric or not.
The above copyright notice and this permission notice shall be included.
bool EmptyString(const char *s)
Check to see whether the given string s is empty (or null).
MetricFactory & GetMetricFactory()
Data structure for an execution time field value.
double m_tolerance
The tolerance to use for checking the execution time. Defaults to 5.0.
std::string m_value
The value to match. Defaults to empty string.
bool m_skip
Indicates whether the metric should be skipped. Defaults to false.