blob: 8e522c56ee9198fa6b46d203b44596b1f218d26d [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
54 NS_LOG_DEBUG (*face1 << ", " << *face2 << ", " << *face3);
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
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 ());
96
97 node.Step ();
98
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 ());
102
103 LoadStats::stats_tuple tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
104 NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
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 ();
111 NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
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
121 NS_LOG_DEBUG ("After decaying");
122
123 tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
124 NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
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 ();
131 NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
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
140 NS_LOG_DEBUG ("After more decaying");
141
142 tuple = node.incoming ().find (face1)->second.GetSatisfiedRatio ();
143 NS_LOG_DEBUG ("In, face1, satisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
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 ();
150 NS_LOG_DEBUG ("In, face1, unsatisfied ratio: " << tuple.get<0> () << ", " << tuple.get<1> () << ", " << tuple.get<2> ());
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;
161 tree.NewPitEntry (CcnxNameComponents ("/bla/bla/bla"));
162 tree.NewPitEntry (CcnxNameComponents ("/foo/bar"));
163 tree.NewPitEntry (CcnxNameComponents ("/bar/foo"));
164 tree.NewPitEntry (CcnxNameComponents ("/tra/la/la"));
165
166 tree.Incoming (CcnxNameComponents ("/bla/bla/bla"), face1);
167 tree.Outgoing (CcnxNameComponents ("/foo/bar"), face2);
168 tree.Satisfy (CcnxNameComponents ("/bar/foo"));
169 tree.Satisfy (CcnxNameComponents ("/tra/la/la"));
170
171 tree.Step ();
172
173 NS_LOG_DEBUG (tree);
174}
175
176}