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" |
| 23 | #include "ns3/ndnSIM-module.h" |
| 24 | #include "../utils/stats-tree.h" |
| 25 | #include "../apps/ccnx-producer.h" |
| 26 | |
| 27 | #include <boost/lexical_cast.hpp> |
| 28 | |
| 29 | NS_LOG_COMPONENT_DEFINE ("CcnxStatsTreeTest"); |
| 30 | |
| 31 | using namespace ndnSIM; |
| 32 | |
| 33 | namespace ns3 |
| 34 | { |
| 35 | |
| 36 | void |
| 37 | StatsTreeTest::DoRun () |
| 38 | { |
| 39 | CcnxStackHelper ccnx; |
| 40 | |
| 41 | Ptr<Node> node1 = CreateObject<Node> (); |
| 42 | Ptr<CcnxApp> app1 = CreateObject<CcnxProducer> (); |
| 43 | node1->AddApplication (app1); |
| 44 | ccnx.Install (node1); |
| 45 | |
| 46 | Ptr<CcnxFace> face1 = CreateObject<CcnxAppFace> (app1); |
| 47 | Ptr<CcnxFace> face2 = CreateObject<CcnxAppFace> (app1); |
| 48 | Ptr<CcnxFace> face3 = CreateObject<CcnxAppFace> (app1); |
| 49 | |
| 50 | node1->GetObject<Ccnx> ()->AddFace (face1); |
| 51 | node1->GetObject<Ccnx> ()->AddFace (face2); |
| 52 | node1->GetObject<Ccnx> ()->AddFace (face3); |
| 53 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 54 | // NS_LOG_DEBUG (*face1 << ", " << *face2 << ", " << *face3); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 55 | |
| 56 | NS_TEST_ASSERT_MSG_NE (*face1, *face2, "Face1 should not be equal to Face2"); |
| 57 | NS_TEST_ASSERT_MSG_NE (face1, face2, "&Face1 should not be equal to &Face2"); |
| 58 | NS_TEST_ASSERT_MSG_NE (*face2, *face3, "Face2 should not be equal to Face3"); |
| 59 | NS_TEST_ASSERT_MSG_NE (face2, face3, "&Face2 should not be equal to &Face3"); |
| 60 | |
| 61 | // hack |
| 62 | face3->SetId (0); |
| 63 | NS_TEST_ASSERT_MSG_EQ (*face1, *face3, "Face1 should be now equal to Face3"); |
| 64 | NS_TEST_ASSERT_MSG_NE (face1, face3, "&Face1 should not be equal to &Face3"); |
| 65 | |
| 66 | LoadStatsNode::stats_container bla; |
| 67 | bla[face1].Step (); |
| 68 | bla[face2].Step (); |
| 69 | |
| 70 | NS_TEST_ASSERT_MSG_EQ (bla.size (), 2, "Should be two entries in the container"); |
| 71 | |
| 72 | bla[face3].Step (); |
| 73 | NS_TEST_ASSERT_MSG_EQ (bla.size (), 2, "Should be still two entries in the container"); |
| 74 | |
| 75 | LoadStatsNode node; |
| 76 | node.AddIncoming (face1); |
| 77 | node.AddIncoming (face1); |
| 78 | node.AddIncoming (face2); |
| 79 | node.AddIncoming (face3); |
| 80 | |
| 81 | NS_TEST_ASSERT_MSG_EQ (node.incoming ().size (), 2, "Incoming should have two entries again"); |
| 82 | NS_TEST_ASSERT_MSG_EQ (node.outgoing ().size (), 0, "Outgoing should have 0 entries"); |
| 83 | |
| 84 | node.Satisfy (); |
| 85 | node.Satisfy (); |
| 86 | NS_TEST_ASSERT_MSG_EQ (node.incoming ().size (), 2, "Incoming should have two entries again"); |
| 87 | NS_TEST_ASSERT_MSG_EQ (node.outgoing ().size (), 0, "Outgoing should have 0 entries"); |
| 88 | |
| 89 | node.Timeout (); |
| 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 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 93 | // NS_LOG_DEBUG ("count: " << node.incoming ().find (face1)->second.count ()); |
| 94 | // NS_LOG_DEBUG ("satisfied: " << node.incoming ().find (face1)->second.satisfied ()); |
| 95 | // NS_LOG_DEBUG ("unsatisfied:" << node.incoming ().find (face1)->second.unsatisfied ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 96 | |
| 97 | node.Step (); |
| 98 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 99 | // NS_LOG_DEBUG ("count: " << node.incoming ().find (face1)->second.count ()); |
| 100 | // NS_LOG_DEBUG ("satisfied: " << node.incoming ().find (face1)->second.satisfied ()); |
| 101 | // NS_LOG_DEBUG ("unsatisfied:" << node.incoming ().find (face1)->second.unsatisfied ()); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 102 | |
| 103 | LoadStats::stats_tuple tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 104 | // 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] | 105 | |
| 106 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.667, 0.01, "Satisfied ratio should be ~ 2/3"); |
| 107 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.667, 0.01, "Satisfied ratio should be ~ 2/3"); |
| 108 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<2> (), 0.667, 0.01, "Satisfied ratio should be ~ 2/3"); |
| 109 | |
| 110 | tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 111 | // 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] | 112 | |
| 113 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.333, 0.01, "Satisfied ratio should be ~ 1/3"); |
| 114 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.333, 0.01, "Satisfied ratio should be ~ 1/3"); |
| 115 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<2> (), 0.333, 0.01, "Satisfied ratio should be ~ 1/3"); |
| 116 | |
| 117 | node.AddIncoming (face1); |
| 118 | node.Timeout (); |
| 119 | node.Step (); |
| 120 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 121 | // NS_LOG_DEBUG ("After decaying"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 122 | |
| 123 | tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 124 | // 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] | 125 | |
| 126 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.489, 0.01, ""); |
| 127 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.489, 0.01, ""); |
| 128 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<2> (), 0.489, 0.01, ""); |
| 129 | |
| 130 | tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 131 | // 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] | 132 | |
| 133 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.51, 0.01, ""); |
| 134 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.51, 0.01, ""); |
| 135 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<2> (), 0.51, 0.01, ""); |
| 136 | |
| 137 | for (uint32_t i = 0; i < 50; i++ ) |
| 138 | node.Step (); |
| 139 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 140 | // NS_LOG_DEBUG ("After more decaying"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 141 | |
| 142 | tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 143 | // 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] | 144 | |
| 145 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.228, 0.01, ""); |
| 146 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.047, 0.01, ""); |
| 147 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<2> (), 0.015, 0.01, ""); |
| 148 | |
| 149 | tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 150 | // 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] | 151 | |
| 152 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.238, 0.01, ""); |
| 153 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.049, 0.01, ""); |
| 154 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<2> (), 0.016, 0.01, ""); |
| 155 | |
| 156 | ///////////////////////////////////////////////////// |
| 157 | // Actual tree testing // |
| 158 | ///////////////////////////////////////////////////// |
| 159 | |
| 160 | StatsTree tree; |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 161 | tree.NewPitEntry ("/bla/bla/bla"); |
| 162 | tree.NewPitEntry ("/bla/bla/bla"); |
| 163 | tree.NewPitEntry ("/bla/bla/bla"); |
| 164 | tree.NewPitEntry ("/foo/bar"); |
| 165 | tree.NewPitEntry ("/bar/foo"); |
| 166 | tree.NewPitEntry ("/tra/la/la"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 168 | tree.Incoming ("/bla/bla/bla", face1); |
| 169 | tree.Outgoing ("/foo/bar", face2); |
| 170 | tree.Satisfy ("/bar/foo"); |
| 171 | tree.Satisfy ("/tra/la/la"); |
| 172 | tree.Timeout ("/tra/la/la"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 173 | |
| 174 | tree.Step (); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 175 | |
| 176 | NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (tree ["/"]), |
| 177 | "PIT: 0.479734, 0.0991713, 0.0332409/0.159911, 0.0330571, 0.0110803/0.0799556, 0.0165285, 0.00554015", |
| 178 | "Something wrong with stats tree"); |
| 179 | |
| 180 | NS_TEST_ASSERT_MSG_NE (&tree ["/bla/bla/bla"], |
| 181 | &tree ["/"], |
| 182 | "The stats tree should not be empty"); |
| 183 | for (uint32_t i = 0; i < 150; i++) |
| 184 | { |
| 185 | tree.Step (); |
| 186 | } |
| 187 | NS_TEST_ASSERT_MSG_EQ (&tree ["/bla/bla/bla"], |
| 188 | &tree ["/"], |
| 189 | "The stats tree should be empty (only root node)"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } |