21 #include <robottestingframework/dll/Plugin.h>
22 #include <yarp/os/Port.h>
23 #include <yarp/os/SystemInfoSerializer.h>
24 #include "SystemStatus.h"
27 using namespace robottestingframework;
28 using namespace yarp::os;
30 #define CONNECTION_TIMEOUT 5.0
33 ROBOTTESTINGFRAMEWORK_PREPARE_PLUGIN(SystemStatus)
36 SystemStatus::SystemStatus() : yarp::robottestingframework::TestCase(
"SystemStatus") {
39 SystemStatus::~SystemStatus() { }
41 bool SystemStatus::setup(yarp::os::Property &property) {
44 if(property.check(
"name"))
45 setName(property.find(
"name").asString());
47 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(property.check(
"hosts"),
48 "A list of hosts name must be given using 'hosts' param");
49 yarp::os::Bottle portsSet =
property.findGroup(
"hosts").tail();
50 for(
unsigned int i=0; i<portsSet.size(); i++) {
51 yarp::os::Bottle* btport = portsSet.get(i).asList();
52 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(btport && btport->size()>=2,
"Hosts must be given as lists of <host name> <max cpu load>");
54 info.name = btport->get(0).asString();
55 info.maxCpuLoad = btport->get(1).asInt32();
56 hosts.push_back(info);
62 void SystemStatus::tearDown() {
66 void SystemStatus::run() {
67 for(
unsigned int i=0; i<hosts.size(); i++) {
68 SystemInfoSerializer info;
69 ROBOTTESTINGFRAMEWORK_TEST_REPORT(
"");
70 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Checking host %s ...", hosts[i].name.c_str()));
71 bool ret = getSystemInfo(hosts[i].name, info);
72 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(ret, Asserter::format(
"Failed to get the system status of host %s. Is the yarprun running on %s?",
73 hosts[i].name.c_str(), hosts[i].name.c_str()));
75 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Total memory %d MB, free memory %d MB.",
76 info.memory.totalSpace, info.memory.freeSpace));
77 unsigned int cpuLoad1 = (int)(info.load.cpuLoad1*100);
78 unsigned int cpuLoad5 = (int)(info.load.cpuLoad5*100);
79 unsigned int cpuLoad15 = (int)(info.load.cpuLoad15*100);
80 ROBOTTESTINGFRAMEWORK_TEST_REPORT(Asserter::format(
"Cpu load during last minute %d\%, last 5 minutes %d\%, last 15 minutes %d\%",
81 cpuLoad1, cpuLoad5, cpuLoad15));
82 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(cpuLoad1 < hosts[i].maxCpuLoad,
83 Asserter::format(
"Cpu load (last minute) is higher than desired [%d\%]", hosts[i].maxCpuLoad));
88 bool SystemStatus::getSystemInfo(std::string remote,
89 SystemInfoSerializer& info) {
94 port.setTimeout(CONNECTION_TIMEOUT);
95 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF(port.open(
"..."),
"Cannot open the yarp port");
97 yarp::os::Bottle msg, grp;
99 grp.addString(
"sysinfo");
104 style.timeout = CONNECTION_TIMEOUT;
106 bool connected = yarp::os::NetworkBase::connect(port.getName(), remote.c_str(), style);
108 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(connected,
string(
"Cannot connect to ") + remote);
113 bool ret = port.write(msg, info);
114 NetworkBase::disconnect(port.getName().c_str(), remote.c_str());
117 ROBOTTESTINGFRAMEWORK_TEST_FAIL_IF(ret, remote +
string(
" does not respond"));