src/stats/examples/gnuplot-helper-example.cc
changeset 11027 e943837b17ad
parent 10978 754c8256c35c
child 11432 d2656819dd54
equal deleted inserted replaced
11026:19b4146b1d39 11027:e943837b17ad
    32 
    32 
    33 using namespace ns3;
    33 using namespace ns3;
    34 
    34 
    35 NS_LOG_COMPONENT_DEFINE ("GnuplotHelperExample");
    35 NS_LOG_COMPONENT_DEFINE ("GnuplotHelperExample");
    36 
    36 
    37 /*
    37 //
    38  * This is our test object, an object that increments counters at
    38 // This is our test object, an object that increments a counter according
    39  * various times and emits one of them as a trace source.
    39 // to a Poisson process, and exports the (integer-valued) count as a
    40  */
    40 // trace source.
       
    41 //
    41 class Emitter : public Object
    42 class Emitter : public Object
    42 {
    43 {
    43 public:
    44 public:
    44   static TypeId GetTypeId (void);
    45   static TypeId GetTypeId (void);
    45   Emitter ();
    46   Emitter ();
    46 private:
    47 private:
    47   void DoInitialize (void);
    48   void DoInitialize (void);
    48   void Emit (void);
       
    49   void Count (void);
    49   void Count (void);
    50 
    50 
    51   TracedValue<double> m_counter;  // normally this would be integer type
    51   TracedValue<uint32_t> m_counter;
    52   Ptr<ExponentialRandomVariable> m_var;
    52   Ptr<ExponentialRandomVariable> m_var;
    53 
       
    54 };
    53 };
    55 
    54 
    56 NS_OBJECT_ENSURE_REGISTERED (Emitter);
    55 NS_OBJECT_ENSURE_REGISTERED (Emitter);
    57 
    56 
    58 TypeId
    57 TypeId
    78 
    77 
    79 void
    78 void
    80 Emitter::DoInitialize (void)
    79 Emitter::DoInitialize (void)
    81 {
    80 {
    82   NS_LOG_FUNCTION (this);
    81   NS_LOG_FUNCTION (this);
    83   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
       
    84   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
    82   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Count, this);
    85 }
       
    86 
       
    87 void
       
    88 Emitter::Emit (void)
       
    89 {
       
    90   NS_LOG_FUNCTION (this);
       
    91   NS_LOG_DEBUG ("Emitting at " << Simulator::Now ().GetSeconds ());
       
    92   Simulator::Schedule (Seconds (m_var->GetValue ()), &Emitter::Emit, this);
       
    93 }
    83 }
    94 
    84 
    95 void
    85 void
    96 Emitter::Count (void)
    86 Emitter::Count (void)
    97 {
    87 {
   113 
   103 
   114   Ptr<Emitter> emitter = CreateObject<Emitter> ();
   104   Ptr<Emitter> emitter = CreateObject<Emitter> ();
   115   Names::Add ("/Names/Emitter", emitter);
   105   Names::Add ("/Names/Emitter", emitter);
   116 
   106 
   117   //
   107   //
   118   // This Probe will be hooked to the Emitter's trace source object by
       
   119   // accessing it by path name in the Config database.
       
   120   //
       
   121 
       
   122   Ptr<DoubleProbe> probe = CreateObject<DoubleProbe> ();
       
   123   probe->SetName ("PathProbe");
       
   124   Names::Add ("/Names/Probe", probe);
       
   125 
       
   126   // Note, no return value is checked here.
       
   127   probe->ConnectByPath ("/Names/Emitter/Counter");
       
   128 
       
   129   //
       
   130   // This gnuplot helper will be used to produce output used to make
   108   // This gnuplot helper will be used to produce output used to make
   131   // gnuplot plots.
   109   // gnuplot plots.
   132   //
   110   //
   133 
   111 
   134   // Create the gnuplot helper.
   112   // Create the gnuplot helper.
   135   GnuplotHelper plotHelper;
   113   GnuplotHelper plotHelper;
   136 
   114 
   137   // Configure the plot.
   115   // Configure the plot.  Arguments include file prefix, plot title,
       
   116   // x-label, y-label, and output file type
   138   plotHelper.ConfigurePlot ("gnuplot-helper-example",
   117   plotHelper.ConfigurePlot ("gnuplot-helper-example",
   139                             "Emitter Counts vs. Time",
   118                             "Emitter Count vs. Time",
   140                             "Time (Seconds)",
   119                             "Time (Seconds)",
   141                             "Emitter Count",
   120                             "Emitter Count",
   142                             "png");
   121                             "png");
   143 
   122 
   144   // Plot the values generated by the probe.  The path that we provide
   123   // Create a probe.  Because the trace source we are interested in is 
   145   // helps to disambiguate the source of the trace.
   124   // of type uint32_t, we specify the type of probe to use by the first
   146   plotHelper.PlotProbe ("ns3::DoubleProbe",
   125   // argument specifying its ns3 TypeId.
   147                         "/Names/Probe/Output",
   126   plotHelper.PlotProbe ("ns3::Uinteger32Probe",
       
   127                         "/Names/Emitter/Counter",
   148                         "Output",
   128                         "Output",
   149                         "Emitter Count",
   129                         "Emitter Count",
   150                         GnuplotAggregator::KEY_INSIDE);
   130                         GnuplotAggregator::KEY_INSIDE);
   151 
   131 
   152   // The Emitter object is not associated with an ns-3 node, so
   132   // The Emitter object is not associated with an ns-3 node, so