utils/bench-simulator.cc
author Josh Pelkey <jpelkey@gatech.edu>
Wed, 11 Aug 2010 11:37:37 -0400
changeset 6553 fb5ad9c7755a
parent 4682 b9d9ae596f83
permissions -rw-r--r--
update release notes and fix doxygen warnings
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2006 INRIA
     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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 
    21 #include "ns3/simulator-module.h"
    22 #include "ns3/core-module.h"
    23 #include <iostream>
    24 #include <fstream>
    25 #include <vector>
    26 #include <string.h>
    27 
    28 using namespace ns3;
    29 
    30 
    31 bool g_debug = false;
    32 
    33 class Bench 
    34 {
    35 public:
    36   Bench ();
    37   void ReadDistribution (std::istream &istream);
    38   void SetTotal (uint32_t total);
    39   void RunBench (void);
    40 private:
    41   void Cb (void);
    42   std::vector<uint64_t> m_distribution;
    43   std::vector<uint64_t>::const_iterator m_current;
    44   uint32_t m_n;
    45   uint32_t m_total;
    46 };
    47 
    48 Bench::Bench ()
    49   : m_n (0),
    50     m_total (0)
    51 {}
    52 
    53 void 
    54 Bench::SetTotal (uint32_t total)
    55 {
    56   m_total = total;
    57 }
    58 
    59 void
    60 Bench::ReadDistribution (std::istream &input)
    61 {
    62   double data;
    63   while (!input.eof ()) 
    64     {
    65       if (input >> data) 
    66         {
    67           uint64_t ns = (uint64_t) (data * 1000000000);
    68           m_distribution.push_back (ns);
    69         } 
    70       else 
    71         {
    72           input.clear ();
    73           std::string line;
    74           input >> line;
    75         }
    76     }
    77 }
    78 
    79 void
    80 Bench::RunBench (void) 
    81 {
    82   SystemWallClockMs time;
    83   double init, simu;
    84   time.Start ();
    85   for (std::vector<uint64_t>::const_iterator i = m_distribution.begin ();
    86        i != m_distribution.end (); i++) 
    87     {
    88       Simulator::Schedule (NanoSeconds (*i), &Bench::Cb, this);
    89     }
    90   init = time.End ();
    91   init /= 1000;
    92 
    93   m_current = m_distribution.begin ();
    94 
    95   time.Start ();
    96   Simulator::Run ();
    97   simu = time.End ();
    98   simu /= 1000;
    99 
   100   std::cout <<
   101       "init n=" << m_distribution.size () << ", time=" << init << "s" << std::endl <<
   102       "simu n=" << m_n << ", time=" <<simu << "s" << std::endl <<
   103       "init " << ((double)m_distribution.size ()) / init << " insert/s, avg insert=" <<
   104       init / ((double)m_distribution.size ())<< "s" << std::endl <<
   105       "simu " << ((double)m_n) / simu<< " hold/s, avg hold=" << 
   106       simu / ((double)m_n) << "s" << std::endl
   107       ;
   108 }
   109 
   110 void
   111 Bench::Cb (void)
   112 {
   113   if (m_n > m_total) 
   114     {
   115       return;
   116     }
   117   if (m_current == m_distribution.end ()) 
   118     {
   119       m_current = m_distribution.begin ();
   120     }
   121   if (g_debug) 
   122     {
   123       std::cerr << "event at " << Simulator::Now ().GetSeconds () << "s" << std::endl;
   124     }
   125   Simulator::Schedule (NanoSeconds (*m_current), &Bench::Cb, this);
   126   m_current++;
   127   m_n++;
   128 }
   129 
   130 void
   131 PrintHelp (void)
   132 {
   133   std::cout << "bench-simulator filename [options]"<<std::endl;
   134   std::cout << "  filename: a string which identifies the input distribution. \"-\" represents stdin." << std::endl;
   135   std::cout << "  Options:"<<std::endl;
   136   std::cout << "      --list: use std::list scheduler"<<std::endl;
   137   std::cout << "      --map: use std::map cheduler"<<std::endl;
   138   std::cout << "      --heap: use Binary Heap scheduler"<<std::endl;
   139   std::cout << "      --debug: enable some debugging"<<std::endl;
   140 }
   141 
   142 int main (int argc, char *argv[])
   143 {
   144   char const *filename = argv[1];
   145   std::istream *input;
   146   uint32_t n = 1;
   147   uint32_t total = 20000;
   148   if (argc == 1)
   149     {
   150       PrintHelp ();
   151       return 0;
   152     }
   153   argc-=2;
   154   argv+= 2;
   155   if (strcmp (filename, "-") == 0) 
   156     {
   157       input = &std::cin;
   158     } 
   159   else 
   160     {
   161       input = new std::ifstream (filename);
   162     }
   163   while (argc > 0) 
   164     {
   165       ObjectFactory factory;
   166       if (strcmp ("--list", argv[0]) == 0) 
   167         {
   168           factory.SetTypeId ("ns3::ListScheduler");
   169           Simulator::SetScheduler (factory);
   170         } 
   171       else if (strcmp ("--heap", argv[0]) == 0) 
   172         {
   173           factory.SetTypeId ("ns3::HeapScheduler");
   174           Simulator::SetScheduler (factory);
   175         } 
   176       else if (strcmp ("--map", argv[0]) == 0) 
   177         {
   178           factory.SetTypeId ("ns3::HeapScheduler");
   179           Simulator::SetScheduler (factory);
   180         } 
   181       else if (strcmp ("--calendar", argv[0]) == 0)
   182         {
   183           factory.SetTypeId ("ns3::CalendarScheduler");
   184           Simulator::SetScheduler (factory);
   185         }
   186       else if (strcmp ("--debug", argv[0]) == 0) 
   187         {
   188           g_debug = true;
   189         } 
   190       else if (strncmp ("--total=", argv[0], strlen("--total=")) == 0) 
   191         {
   192           total = atoi (argv[0]+strlen ("--total="));
   193         } 
   194       else if (strncmp ("--n=", argv[0], strlen("--n=")) == 0) 
   195         {
   196           n = atoi (argv[0]+strlen ("--n="));
   197         } 
   198 
   199       argc--;
   200       argv++;
   201   }
   202   Bench *bench = new Bench ();
   203   bench->ReadDistribution (*input);
   204   bench->SetTotal (total);
   205   for (uint32_t i = 0; i < n; i++)
   206     {
   207       bench->RunBench ();
   208     }
   209 
   210   return 0;
   211 }