Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011,2012 University of California, Los Angeles |
| 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "ndnSIM-stats-tree.h" |
| 22 | #include "ns3/core-module.h" |
Alexander Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame] | 23 | #include "ns3/point-to-point-module.h" |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 24 | #include "ns3/ndnSIM-module.h" |
| 25 | #include "../utils/stats-tree.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 26 | #include "../apps/ndn-producer.h" |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 27 | |
| 28 | #include <boost/lexical_cast.hpp> |
| 29 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 30 | NS_LOG_COMPONENT_DEFINE ("ndn.StatsTreeTest"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 32 | namespace ns3 { |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 34 | using namespace ndn; |
| 35 | using namespace ndn::ndnSIM; |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 37 | void |
| 38 | StatsTreeTest::DoRun () |
| 39 | { |
Alexander Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame] | 40 | BasicTests (); |
| 41 | SimpleScenario (); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void |
| 46 | StatsTreeTest::BasicTests () |
| 47 | { |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 48 | StackHelper ndn; |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 49 | |
| 50 | Ptr<Node> node1 = CreateObject<Node> (); |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 51 | Ptr<App> app1 = CreateObject<Producer> (); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 52 | node1->AddApplication (app1); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 53 | ndn.Install (node1); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 55 | Ptr<Face> face1 = CreateObject<AppFace> (app1); |
| 56 | Ptr<Face> face2 = CreateObject<AppFace> (app1); |
| 57 | Ptr<Face> face3 = CreateObject<AppFace> (app1); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 59 | node1->GetObject<L3Protocol> ()->AddFace (face1); |
| 60 | node1->GetObject<L3Protocol> ()->AddFace (face2); |
| 61 | node1->GetObject<L3Protocol> ()->AddFace (face3); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 62 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 63 | // NS_LOG_DEBUG (*face1 << ", " << *face2 << ", " << *face3); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 64 | |
| 65 | NS_TEST_ASSERT_MSG_NE (*face1, *face2, "Face1 should not be equal to Face2"); |
| 66 | NS_TEST_ASSERT_MSG_NE (face1, face2, "&Face1 should not be equal to &Face2"); |
| 67 | NS_TEST_ASSERT_MSG_NE (*face2, *face3, "Face2 should not be equal to Face3"); |
| 68 | NS_TEST_ASSERT_MSG_NE (face2, face3, "&Face2 should not be equal to &Face3"); |
| 69 | |
| 70 | // hack |
| 71 | face3->SetId (0); |
| 72 | NS_TEST_ASSERT_MSG_EQ (*face1, *face3, "Face1 should be now equal to Face3"); |
| 73 | NS_TEST_ASSERT_MSG_NE (face1, face3, "&Face1 should not be equal to &Face3"); |
| 74 | |
| 75 | LoadStatsNode::stats_container bla; |
| 76 | bla[face1].Step (); |
| 77 | bla[face2].Step (); |
| 78 | |
| 79 | NS_TEST_ASSERT_MSG_EQ (bla.size (), 2, "Should be two entries in the container"); |
| 80 | |
| 81 | bla[face3].Step (); |
| 82 | NS_TEST_ASSERT_MSG_EQ (bla.size (), 2, "Should be still two entries in the container"); |
| 83 | |
| 84 | LoadStatsNode node; |
| 85 | node.AddIncoming (face1); |
| 86 | node.AddIncoming (face1); |
| 87 | node.AddIncoming (face2); |
| 88 | node.AddIncoming (face3); |
| 89 | |
| 90 | NS_TEST_ASSERT_MSG_EQ (node.incoming ().size (), 2, "Incoming should have two entries again"); |
| 91 | NS_TEST_ASSERT_MSG_EQ (node.outgoing ().size (), 0, "Outgoing should have 0 entries"); |
| 92 | |
| 93 | node.Satisfy (); |
| 94 | node.Satisfy (); |
| 95 | NS_TEST_ASSERT_MSG_EQ (node.incoming ().size (), 2, "Incoming should have two entries again"); |
| 96 | NS_TEST_ASSERT_MSG_EQ (node.outgoing ().size (), 0, "Outgoing should have 0 entries"); |
| 97 | |
| 98 | node.Timeout (); |
| 99 | NS_TEST_ASSERT_MSG_EQ (node.incoming ().size (), 2, "Incoming should have two entries again"); |
| 100 | NS_TEST_ASSERT_MSG_EQ (node.outgoing ().size (), 0, "Outgoing should have 0 entries"); |
| 101 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 102 | // NS_LOG_DEBUG ("count: " << node.incoming ().find (face1)->second.count ()); |
| 103 | // NS_LOG_DEBUG ("satisfied: " << node.incoming ().find (face1)->second.satisfied ()); |
| 104 | // NS_LOG_DEBUG ("unsatisfied:" << node.incoming ().find (face1)->second.unsatisfied ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 105 | |
| 106 | node.Step (); |
| 107 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 108 | // NS_LOG_DEBUG ("count: " << node.incoming ().find (face1)->second.count ()); |
| 109 | // NS_LOG_DEBUG ("satisfied: " << node.incoming ().find (face1)->second.satisfied ()); |
| 110 | // NS_LOG_DEBUG ("unsatisfied:" << node.incoming ().find (face1)->second.unsatisfied ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 111 | |
| 112 | LoadStats::stats_tuple tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 113 | // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 114 | |
| 115 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.667, 0.01, "Satisfied ratio should be ~ 2/3"); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 116 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, "Satisfied ratio should be less 0 (invalid)"); |
| 117 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, "Satisfied ratio should be less 0 (invalid)"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 118 | |
| 119 | tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 120 | // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 121 | |
| 122 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.333, 0.01, "Satisfied ratio should be ~ 1/3"); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 123 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, "Satisfied ratio should be less 0 (invalid)"); |
| 124 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, "Satisfied ratio should be less 0 (invalid)"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 125 | |
| 126 | node.AddIncoming (face1); |
| 127 | node.Timeout (); |
| 128 | node.Step (); |
| 129 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 130 | // NS_LOG_DEBUG ("After decaying"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 131 | |
| 132 | tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 133 | // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 135 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.473776, 0.01, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 136 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.489, 0.01, ""); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 137 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 138 | |
| 139 | tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 140 | // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 141 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 142 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.526, 0.01, ""); |
| 143 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.504, 0.01, ""); |
| 144 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 146 | for (uint32_t i = 0; i < 10; i++ ) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 147 | node.Step (); |
| 148 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 149 | // NS_LOG_DEBUG ("After more decaying"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 150 | |
| 151 | tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 152 | // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 154 | NS_TEST_ASSERT_MSG_LT (tuple.get<0> (), 0, ""); |
| 155 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, ""); |
| 156 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 157 | |
| 158 | tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 159 | // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 161 | NS_TEST_ASSERT_MSG_LT (tuple.get<0> (), 0, ""); |
| 162 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, ""); |
| 163 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 164 | |
| 165 | ///////////////////////////////////////////////////// |
| 166 | // Actual tree testing // |
| 167 | ///////////////////////////////////////////////////// |
| 168 | |
| 169 | StatsTree tree; |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 170 | tree.NewPitEntry ("/bla/bla/bla"); |
| 171 | tree.NewPitEntry ("/bla/bla/bla"); |
| 172 | tree.NewPitEntry ("/bla/bla/bla"); |
| 173 | tree.NewPitEntry ("/foo/bar"); |
| 174 | tree.NewPitEntry ("/bar/foo"); |
| 175 | tree.NewPitEntry ("/tra/la/la"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 176 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 177 | tree.Incoming ("/bla/bla/bla", face1); |
| 178 | tree.Outgoing ("/foo/bar", face2); |
| 179 | tree.Satisfy ("/bar/foo"); |
| 180 | tree.Satisfy ("/tra/la/la"); |
| 181 | tree.Timeout ("/tra/la/la"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 182 | |
| 183 | tree.Step (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 184 | |
| 185 | NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (tree ["/"]), |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 186 | // "PIT: 0.479734, 0.0991713, 0.0332409/0.159911, 0.0330571, 0.0110803/0.0799556, 0.0165285, 0.00554015", |
| 187 | "PIT: ration satisfied: 0.333333 0.333333 -1 / unsatisfied: 0.166667 0.166667 -1 ", |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 188 | "Something wrong with stats tree"); |
| 189 | |
| 190 | NS_TEST_ASSERT_MSG_NE (&tree ["/bla/bla/bla"], |
| 191 | &tree ["/"], |
| 192 | "The stats tree should not be empty"); |
Alexander Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame] | 193 | for (uint32_t i = 0; i < 9; i++) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 194 | { |
| 195 | tree.Step (); |
| 196 | } |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame] | 197 | NS_LOG_DEBUG (tree ["/bla/bla/bla"]); |
| 198 | NS_LOG_DEBUG (tree ["/"]); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 199 | NS_TEST_ASSERT_MSG_EQ (&tree ["/bla/bla/bla"], |
| 200 | &tree ["/"], |
| 201 | "The stats tree should be empty (only root node)"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | 27acad9 | 2012-07-20 15:32:20 -0700 | [diff] [blame] | 204 | |
| 205 | |
| 206 | void |
| 207 | StatsTreeTest::SimpleScenario () |
| 208 | { |
| 209 | NodeContainer nodes; |
| 210 | nodes.Create (2); |
| 211 | PointToPointHelper p2pHelper; |
| 212 | p2pHelper.Install (nodes); |
| 213 | |
| 214 | |
| 215 | |
| 216 | } |
| 217 | |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 218 | } |