1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/mt/Guard.hpp>
12 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/Logger.hpp>
14 #include <anna/core/util/DelayMeter.hpp>
15 #include <anna/core/tracing/TraceMethod.hpp>
17 #include <anna/xml/Node.hpp>
18 #include <anna/xml/Attribute.hpp>
20 #include <anna/app/Application.hpp>
22 #include <anna/comm/Communicator.hpp>
23 #include <anna/comm/CongestionController.hpp>
25 #include <anna/test/Control.hpp>
30 typedef Guard MyGuard;
31 //typedef Guard <test::Control> MyGuard;
33 test::Control::Control (comm::Communicator* engine) :
37 a_avgDelay ("AvgDelay"),
38 a_avgLatency ("AvgLatency"),
42 bool test::Control::canContinue (const comm::socket::Client&)
43 throw (RuntimeException)
45 LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::canContinue", ANNA_FILE_LOCATION));
47 comm::CongestionController& congestionController = comm::CongestionController::instantiate ();
49 if (a_maxMessage != -1 && congestionController.getMessageCounter () >= a_maxMessage) {
57 a_initTime = anna::Millisecond::getTime ();
62 void test::Control::delay ()
63 throw (RuntimeException)
65 LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::delay", ANNA_FILE_LOCATION));
68 Millisecond random = Millisecond ((a_delay > 10) ? (rand () % (a_delay / 10)): 0);
69 Millisecond delay (a_delay);
71 int sign = rand () % 2;
78 DelayMeter <Microsecond> meter;
80 anna::functions::sleep (delay);
84 a_avgDelay += meter.getValue();
89 // Aproximación del valor que lleva en mensaje en cola I/O
90 Millisecond test::Control::latency (const Millisecond& init)
91 throw (RuntimeException)
93 LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::latency", ANNA_FILE_LOCATION));
95 const Millisecond now = anna::Millisecond::getTime();
97 const Millisecond _latency = now - init;
100 MyGuard guard (this);
101 a_avgLatency += _latency;
107 void test::Control::report ()
110 const Millisecond serviceTime = anna::Millisecond::getTime () - a_initTime;
112 comm::CongestionController& congestionController = comm::CongestionController::instantiate ();
114 const int workload = (serviceTime == 0) ? 0: congestionController.getMessageCounter () * 1000 / serviceTime;
115 string msg (anna::functions::asText ("Time of service: ", (int) serviceTime));
116 msg += anna::functions::asText (" ms | Workload: ", workload);
117 msg += anna::functions::asText (" msg/sec | Received Messages: ", congestionController.getMessageCounter ());
118 msg += anna::functions::asText (" | Processed Messages: ", congestionController.getSuccessCounter ());
119 Logger::notice (msg, ANNA_FILE_LOCATION);
120 cout << msg << endl << endl;
122 msg = "Average delay (us): ";
123 msg += a_avgDelay.asString ();
124 Logger::notice (msg, ANNA_FILE_LOCATION);
125 cout << msg << endl << endl;
127 msg = "Average latency (ms): ";
128 msg += a_avgLatency.asString ();
129 Logger::notice (msg, ANNA_FILE_LOCATION);
130 cout << msg << endl << endl;
133 void test::Control::stop ()
134 throw (RuntimeException)
136 if (a_engine.hasRequestedStop () == true)
139 a_engine.requestStop ();
143 xml::Node* test::Control::asXML (xml::Node* parent)
146 xml::Node* result = parent->createChild ("test.Control");
148 result->createAttribute ("MaxMessage", a_maxMessage);
149 result->createAttribute ("AvgDelay", a_avgDelay.value ().asString());
150 result->createAttribute ("AvgLatency", a_avgLatency.value ().asString());