1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/core/mt/Guard.hpp>
40 #include <anna/core/functions.hpp>
41 #include <anna/core/tracing/Logger.hpp>
42 #include <anna/core/util/DelayMeter.hpp>
43 #include <anna/core/tracing/TraceMethod.hpp>
45 #include <anna/xml/Node.hpp>
46 #include <anna/xml/Attribute.hpp>
48 #include <anna/app/Application.hpp>
50 #include <anna/comm/Communicator.hpp>
51 #include <anna/comm/CongestionController.hpp>
53 #include <anna/test/Control.hpp>
58 typedef Guard MyGuard;
59 //typedef Guard <test::Control> MyGuard;
61 test::Control::Control (comm::Communicator* engine) :
65 a_avgDelay ("AvgDelay"),
66 a_avgLatency ("AvgLatency"),
70 bool test::Control::canContinue (const comm::socket::Client&)
71 throw (RuntimeException)
73 LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::canContinue", ANNA_FILE_LOCATION));
75 comm::CongestionController& congestionController = comm::CongestionController::instantiate ();
77 if (a_maxMessage != -1 && congestionController.getMessageCounter () >= a_maxMessage) {
85 a_initTime = anna::Millisecond::getTime ();
90 void test::Control::delay ()
91 throw (RuntimeException)
93 LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::delay", ANNA_FILE_LOCATION));
96 Millisecond random = Millisecond ((a_delay > 10) ? (rand () % (a_delay / 10)): 0);
97 Millisecond delay (a_delay);
99 int sign = rand () % 2;
106 DelayMeter <Microsecond> meter;
108 anna::functions::sleep (delay);
111 MyGuard guard (this);
112 a_avgDelay += meter.getValue();
117 // Aproximación del valor que lleva en mensaje en cola I/O
118 Millisecond test::Control::latency (const Millisecond& init)
119 throw (RuntimeException)
121 LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::latency", ANNA_FILE_LOCATION));
123 const Millisecond now = anna::Millisecond::getTime();
125 const Millisecond _latency = now - init;
128 MyGuard guard (this);
129 a_avgLatency += _latency;
135 void test::Control::report ()
138 const Millisecond serviceTime = anna::Millisecond::getTime () - a_initTime;
140 comm::CongestionController& congestionController = comm::CongestionController::instantiate ();
142 const int workload = (serviceTime == 0) ? 0: congestionController.getMessageCounter () * 1000 / serviceTime;
143 string msg (anna::functions::asText ("Time of service: ", (int) serviceTime));
144 msg += anna::functions::asText (" ms | Workload: ", workload);
145 msg += anna::functions::asText (" msg/sec | Received Messages: ", congestionController.getMessageCounter ());
146 msg += anna::functions::asText (" | Processed Messages: ", congestionController.getSuccessCounter ());
147 Logger::notice (msg, ANNA_FILE_LOCATION);
148 cout << msg << endl << endl;
150 msg = "Average delay (us): ";
151 msg += a_avgDelay.asString ();
152 Logger::notice (msg, ANNA_FILE_LOCATION);
153 cout << msg << endl << endl;
155 msg = "Average latency (ms): ";
156 msg += a_avgLatency.asString ();
157 Logger::notice (msg, ANNA_FILE_LOCATION);
158 cout << msg << endl << endl;
161 void test::Control::stop ()
162 throw (RuntimeException)
164 if (a_engine.hasRequestedStop () == true)
167 a_engine.requestStop ();
171 xml::Node* test::Control::asXML (xml::Node* parent)
174 xml::Node* result = parent->createChild ("test.Control");
176 result->createAttribute ("MaxMessage", a_maxMessage);
177 result->createAttribute ("AvgDelay", a_avgDelay.value ().asString());
178 result->createAttribute ("AvgLatency", a_avgLatency.value ().asString());