blob: 377f09f82ef827782cc17b2d845be00176a2129e [file] [log] [blame]
Alexander Afanasyev0845c092012-07-13 17:45:33 -07001/* -*- 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
29NS_LOG_COMPONENT_DEFINE ("CcnxStatsTreeTest");
30
31using namespace ndnSIM;
32
33namespace ns3
34{
35
36void
37StatsTreeTest::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 Afanasyev0560eec2012-07-16 15:44:31 -070054 // NS_LOG_DEBUG (*face1 << ", " << *face2 << ", " << *face3);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070055
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 Afanasyev0560eec2012-07-16 15:44:31 -070093 // 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 Afanasyev0845c092012-07-13 17:45:33 -070096
97 node.Step ();
98
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070099 // 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 Afanasyev0845c092012-07-13 17:45:33 -0700102
103 LoadStats::stats_tuple tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700104 // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700105
106 NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.667, 0.01, "Satisfied ratio should be ~ 2/3");
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700107 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 Afanasyev0845c092012-07-13 17:45:33 -0700109
110 tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700111 // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700112
113 NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.333, 0.01, "Satisfied ratio should be ~ 1/3");
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700114 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 Afanasyev0845c092012-07-13 17:45:33 -0700116
117 node.AddIncoming (face1);
118 node.Timeout ();
119 node.Step ();
120
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700121 // NS_LOG_DEBUG ("After decaying");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700122
123 tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700124 // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700125
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700126 NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.473776, 0.01, "");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700127 NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.489, 0.01, "");
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700128 NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, "");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700129
130 tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700131 // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700132
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700133 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 Afanasyev0845c092012-07-13 17:45:33 -0700136
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700137 for (uint32_t i = 0; i < 10; i++ )
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700138 node.Step ();
139
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700140 // NS_LOG_DEBUG ("After more decaying");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700141
142 tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700143 // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700144
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700145 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 Afanasyev0845c092012-07-13 17:45:33 -0700148
149 tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700150 // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700151
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700152 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 Afanasyev0845c092012-07-13 17:45:33 -0700155
156 /////////////////////////////////////////////////////
157 // Actual tree testing //
158 /////////////////////////////////////////////////////
159
160 StatsTree tree;
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700161 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 Afanasyev0845c092012-07-13 17:45:33 -0700167
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700168 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 Afanasyev0845c092012-07-13 17:45:33 -0700173
174 tree.Step ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700175
176 NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (tree ["/"]),
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700177 // "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 Afanasyev0560eec2012-07-16 15:44:31 -0700179 "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 Afanasyeve55d1e32012-07-19 15:33:05 -0700184 for (uint32_t i = 0; i < 50; i++)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700185 {
186 tree.Step ();
187 }
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700188 NS_LOG_DEBUG (tree ["/bla/bla/bla"]);
189 NS_LOG_DEBUG (tree ["/"]);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700190 NS_TEST_ASSERT_MSG_EQ (&tree ["/bla/bla/bla"],
191 &tree ["/"],
192 "The stats tree should be empty (only root node)");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700193}
194
195}