blob: c1a813f2e378fe1fb9b130193b519b93d225b688 [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"
Alexander Afanasyev27acad92012-07-20 15:32:20 -070023#include "ns3/point-to-point-module.h"
Alexander Afanasyev0845c092012-07-13 17:45:33 -070024#include "ns3/ndnSIM-module.h"
Alexander Afanasyev1a2df6a2012-08-17 13:21:51 -070025#include "../utils/stats/stats-tree.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070026#include "../apps/ndn-producer.h"
Alexander Afanasyev0845c092012-07-13 17:45:33 -070027
28#include <boost/lexical_cast.hpp>
29
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070030NS_LOG_COMPONENT_DEFINE ("ndn.StatsTreeTest");
Alexander Afanasyev0845c092012-07-13 17:45:33 -070031
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070032namespace ns3 {
Alexander Afanasyev0845c092012-07-13 17:45:33 -070033
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070034using namespace ndn;
35using namespace ndn::ndnSIM;
Alexander Afanasyeve77db792012-08-09 11:10:58 -070036
Alexander Afanasyev0845c092012-07-13 17:45:33 -070037void
38StatsTreeTest::DoRun ()
39{
Alexander Afanasyev27acad92012-07-20 15:32:20 -070040 BasicTests ();
41 SimpleScenario ();
42}
43
44
45void
46StatsTreeTest::BasicTests ()
47{
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070048 StackHelper ndn;
Alexander Afanasyev0845c092012-07-13 17:45:33 -070049
50 Ptr<Node> node1 = CreateObject<Node> ();
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070051 Ptr<App> app1 = CreateObject<Producer> ();
Alexander Afanasyev0845c092012-07-13 17:45:33 -070052 node1->AddApplication (app1);
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070053 ndn.Install (node1);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070054
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070055 Ptr<Face> face1 = CreateObject<AppFace> (app1);
56 Ptr<Face> face2 = CreateObject<AppFace> (app1);
57 Ptr<Face> face3 = CreateObject<AppFace> (app1);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070058
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070059 node1->GetObject<L3Protocol> ()->AddFace (face1);
60 node1->GetObject<L3Protocol> ()->AddFace (face2);
61 node1->GetObject<L3Protocol> ()->AddFace (face3);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070062
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070063 // NS_LOG_DEBUG (*face1 << ", " << *face2 << ", " << *face3);
Alexander Afanasyev0845c092012-07-13 17:45:33 -070064
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 Afanasyev0560eec2012-07-16 15:44:31 -0700102 // 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 Afanasyev0845c092012-07-13 17:45:33 -0700105
106 node.Step ();
107
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700108 // 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 Afanasyev0845c092012-07-13 17:45:33 -0700111
112 LoadStats::stats_tuple tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700113 // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700114
115 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 -0700116 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 Afanasyev0845c092012-07-13 17:45:33 -0700118
119 tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700120 // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700121
122 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 -0700123 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 Afanasyev0845c092012-07-13 17:45:33 -0700125
126 node.AddIncoming (face1);
127 node.Timeout ();
128 node.Step ();
129
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700130 // NS_LOG_DEBUG ("After decaying");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700131
132 tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700133 // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700134
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700135 NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<0> (), 0.473776, 0.01, "");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700136 NS_TEST_ASSERT_MSG_EQ_TOL (tuple.get<1> (), 0.489, 0.01, "");
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700137 NS_TEST_ASSERT_MSG_LT (tuple.get<2> (), 0, "");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700138
139 tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700140 // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700141
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700142 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 Afanasyev0845c092012-07-13 17:45:33 -0700145
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700146 for (uint32_t i = 0; i < 10; i++ )
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700147 node.Step ();
148
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700149 // NS_LOG_DEBUG ("After more decaying");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700150
151 tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700152 // NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700153
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700154 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 Afanasyev0845c092012-07-13 17:45:33 -0700157
158 tuple = node.incoming ().find (face1)->second.GetUnsatisfiedRatio ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700159 // NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700160
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700161 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 Afanasyev0845c092012-07-13 17:45:33 -0700164
165 /////////////////////////////////////////////////////
166 // Actual tree testing //
167 /////////////////////////////////////////////////////
168
169 StatsTree tree;
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700170 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 Afanasyev0845c092012-07-13 17:45:33 -0700176
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700177 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 Afanasyev0845c092012-07-13 17:45:33 -0700182
183 tree.Step ();
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700184
185 NS_TEST_ASSERT_MSG_EQ (boost::lexical_cast<std::string> (tree ["/"]),
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700186 // "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 Afanasyev0560eec2012-07-16 15:44:31 -0700188 "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 Afanasyev27acad92012-07-20 15:32:20 -0700193 for (uint32_t i = 0; i < 9; i++)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700194 {
195 tree.Step ();
196 }
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -0700197 NS_LOG_DEBUG (tree ["/bla/bla/bla"]);
198 NS_LOG_DEBUG (tree ["/"]);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -0700199 NS_TEST_ASSERT_MSG_EQ (&tree ["/bla/bla/bla"],
200 &tree ["/"],
201 "The stats tree should be empty (only root node)");
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700202}
203
Alexander Afanasyev27acad92012-07-20 15:32:20 -0700204
205
206void
207StatsTreeTest::SimpleScenario ()
208{
209 NodeContainer nodes;
210 nodes.Create (2);
211 PointToPointHelper p2pHelper;
212 p2pHelper.Install (nodes);
213
214
215
216}
217
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700218}