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"); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 107 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, "Satisfied ratio should be less 0 (invalid)"); |
| 108 | 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] | 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"); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 114 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, "Satisfied ratio should be less 0 (invalid)"); |
| 115 | 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] | 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 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 126 | 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] | 127 | 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^] | 128 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 133 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.526, 0.01, ""); |
| 134 | NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.504, 0.01, ""); |
| 135 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 136 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 137 | for (uint32_t i = 0; i < 10; i++ ) |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 145 | NS_TEST_ASSERT_MSG_LT (tuple.get<0> (), 0, ""); |
| 146 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, ""); |
| 147 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 152 | NS_TEST_ASSERT_MSG_LT (tuple.get<0> (), 0, ""); |
| 153 | NS_TEST_ASSERT_MSG_LT (tuple.get<1> (), 0, ""); |
| 154 | NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, ""); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 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 ["/"]), |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 177 | // "PIT: 0.479734, 0.0991713, 0.0332409/0.159911, 0.0330571, 0.0110803/0.0799556, 0.0165285, 0.00554015", |
| 178 | "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] | 179 | "Something wrong with stats tree"); |
| 180 | |
| 181 | NS_TEST_ASSERT_MSG_NE (&tree ["/bla/bla/bla"], |
| 182 | &tree ["/"], |
| 183 | "The stats tree should not be empty"); |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 184 | for (uint32_t i = 0; i < 50; i++) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 185 | { |
| 186 | tree.Step (); |
| 187 | } |
Alexander Afanasyev | e55d1e3 | 2012-07-19 15:33:05 -0700 | [diff] [blame^] | 188 | NS_LOG_DEBUG (tree ["/bla/bla/bla"]); |
| 189 | NS_LOG_DEBUG (tree ["/"]); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 190 | NS_TEST_ASSERT_MSG_EQ (&tree ["/bla/bla/bla"], |
| 191 | &tree ["/"], |
| 192 | "The stats tree should be empty (only root node)"); |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | } |