OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2015 Bucknell University |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: Li Li (ll024@bucknell.edu) |
| 19 */ |
| 20 |
| 21 #include <iostream> |
| 22 #include <string> |
| 23 #include "ns3/time-averaging-collector.h" |
| 24 #include "ns3/double-probe.h" |
| 25 #include "ns3/test.h" |
| 26 #include "ns3/core-module.h" |
| 27 |
| 28 using namespace ns3; |
| 29 |
| 30 class TimeAveragingCollectorSampleEmitter : public Object |
| 31 { |
| 32 public: |
| 33 static TypeId GetTypeId (void); |
| 34 TimeAveragingCollectorSampleEmitter () |
| 35 { |
| 36 m_var = CreateObject<ExponentialRandomVariable> (); |
| 37 } |
| 38 virtual ~TimeAveragingCollectorSampleEmitter () |
| 39 { |
| 40 } |
| 41 void Start () |
| 42 { |
| 43 Reschedule (); |
| 44 } |
| 45 void Reschedule () |
| 46 { |
| 47 m_time = m_var->GetValue (); |
| 48 Simulator::Schedule (Seconds (m_time), &TimeAveragingCollectorSampleEmitter:
:Report, this); |
| 49 m_time += Simulator::Now ().GetSeconds (); |
| 50 } |
| 51 double GetTime () |
| 52 { |
| 53 return m_time; |
| 54 } |
| 55 double GetValue () |
| 56 { |
| 57 return aux; |
| 58 } |
| 59 private: |
| 60 void Report () |
| 61 { |
| 62 aux = m_var->GetValue (); |
| 63 m_trace = aux; |
| 64 Reschedule (); |
| 65 } |
| 66 Ptr<ExponentialRandomVariable> m_var; |
| 67 double m_time; |
| 68 TracedValue<double> m_trace; |
| 69 double aux; |
| 70 }; |
| 71 |
| 72 |
| 73 TypeId |
| 74 TimeAveragingCollectorSampleEmitter::GetTypeId (void) |
| 75 { |
| 76 static TypeId tid = TypeId ("TimeAveragingCollectorSampleEmitter") |
| 77 .SetParent<Object> () |
| 78 .AddTraceSource ("Emitter", "XX", MakeTraceSourceAccessor (&TimeAveragingCol
lectorSampleEmitter::m_trace), "ns3::TracedValue::DoubleCallback") |
| 79 ; |
| 80 return tid; |
| 81 } |
| 82 |
| 83 |
| 84 // =========================================================================== |
| 85 // Test case |
| 86 // =========================================================================== |
| 87 |
| 88 class TimeAveragingCollectorTestCase1 : public TestCase |
| 89 { |
| 90 public: |
| 91 TimeAveragingCollectorTestCase1 (); |
| 92 virtual ~TimeAveragingCollectorTestCase1 (); |
| 93 |
| 94 private: |
| 95 double currentTime; |
| 96 double average; |
| 97 double batchCount; |
| 98 virtual void DoRun (void); |
| 99 void TraceSinkCallback (std::string context, double oldValue, double newValue)
; |
| 100 void ProbeTraceSinkCallback (double time, double newValue); |
| 101 Ptr<TimeAveragingCollector> collector; |
| 102 Ptr<TimeAveragingCollectorSampleEmitter> s; |
| 103 }; |
| 104 |
| 105 TimeAveragingCollectorTestCase1::TimeAveragingCollectorTestCase1 () |
| 106 : TestCase ("TimeAveragingCollector test case 1") |
| 107 { |
| 108 } |
| 109 |
| 110 TimeAveragingCollectorTestCase1::~TimeAveragingCollectorTestCase1 () |
| 111 { |
| 112 } |
| 113 |
| 114 void |
| 115 TimeAveragingCollectorTestCase1::DoRun (void) |
| 116 { |
| 117 // let the collector generate outputs every 1 second |
| 118 collector = CreateObject<TimeAveragingCollector> (); |
| 119 collector->SetPeriod (Seconds (3)); |
| 120 collector->Enable (); |
| 121 s = CreateObject<TimeAveragingCollectorSampleEmitter> (); |
| 122 currentTime = 3; |
| 123 average = 0; |
| 124 batchCount = 0; |
| 125 |
| 126 Ptr<DoubleProbe> p = CreateObject<DoubleProbe> (); |
| 127 p->SetName ("TimeAveragingCollectorSampleProbe"); |
| 128 |
| 129 Simulator::Schedule (Seconds (0), &TimeAveragingCollectorSampleEmitter::Start,
s); |
| 130 p->SetAttribute ("Start", TimeValue (Seconds (0.0))); |
| 131 p->SetAttribute ("Stop", TimeValue (Seconds (100.0))); |
| 132 Simulator::Stop (Seconds (200)); |
| 133 |
| 134 Names::Add ("/Names/TimeAveragingCollectorSampleEmitter", s); |
| 135 |
| 136 // Hook probe to the emitter. |
| 137 p->ConnectByObject ("Emitter", s); |
| 138 |
| 139 // Hook the collector to the probe |
| 140 bool connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&Time
AveragingCollector::TraceSinkDouble, collector)); |
| 141 NS_ASSERT (connected == true); |
| 142 |
| 143 // Connect the probe to the test trace sink defined in this class |
| 144 connected = p->TraceConnectWithoutContext ("Output", MakeCallback (&TimeAvera
gingCollectorTestCase1::ProbeTraceSinkCallback, this)); |
| 145 NS_ASSERT (connected == true); |
| 146 |
| 147 // connect the collector to the TraceSinkCallback. |
| 148 connected = collector->TraceConnect ("Output", p->GetName (), MakeCallback (&T
imeAveragingCollectorTestCase1::TraceSinkCallback, this)); |
| 149 NS_ASSERT (connected == true); |
| 150 NS_UNUSED (connected); |
| 151 |
| 152 Simulator::Run (); |
| 153 Simulator::Destroy (); |
| 154 } |
| 155 |
| 156 void |
| 157 TimeAveragingCollectorTestCase1::TraceSinkCallback (std::string context, double
time, double newValue) |
| 158 { |
| 159 // check whether the collector is generating outputs every 3 second. |
| 160 NS_TEST_ASSERT_MSG_EQ_TOL (currentTime, time, 0.0001, "Time generated by the c
ollector different than the actual time"); |
| 161 |
| 162 // check whether the average value generated by the collector is correct |
| 163 NS_TEST_ASSERT_MSG_EQ_TOL (average, newValue, 0.0001, "Average generated by th
e collector different than the actual average"); |
| 164 batchCount = 0; |
| 165 currentTime += 3; |
| 166 } |
| 167 |
| 168 void |
| 169 TimeAveragingCollectorTestCase1::ProbeTraceSinkCallback (double time, double new
Value) |
| 170 { |
| 171 average = (average * batchCount + newValue) / (batchCount + 1); |
| 172 batchCount++; |
| 173 } |
| 174 |
| 175 class TimeAveragingCollectorTestSuite : public TestSuite |
| 176 { |
| 177 public: |
| 178 TimeAveragingCollectorTestSuite (); |
| 179 }; |
| 180 |
| 181 TimeAveragingCollectorTestSuite::TimeAveragingCollectorTestSuite () |
| 182 : TestSuite ("time-averaging-collector", UNIT) |
| 183 { |
| 184 AddTestCase (new TimeAveragingCollectorTestCase1, TestCase::QUICK); |
| 185 } |
| 186 |
| 187 static TimeAveragingCollectorTestSuite TimeAveragingCollectorTestSuite; |
| 188 |
OLD | NEW |