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
mathieu@150
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@12
     2
/*
mathieu@12
     3
 * Copyright (c) 2006 INRIA
mathieu@12
     4
 *
mathieu@12
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@12
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@12
     7
 * published by the Free Software Foundation;
mathieu@12
     8
 *
mathieu@12
     9
 * This program is distributed in the hope that it will be useful,
mathieu@12
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@12
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@12
    12
 * GNU General Public License for more details.
mathieu@12
    13
 *
mathieu@12
    14
 * You should have received a copy of the GNU General Public License
mathieu@12
    15
 * along with this program; if not, write to the Free Software
mathieu@12
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@12
    17
 *
mathieu@12
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@12
    19
 */
mathieu@12
    20
mathieu@2913
    21
#include "ns3/simulator-module.h"
mathieu@2913
    22
#include "ns3/core-module.h"
mathieu@12
    23
#include <iostream>
mathieu@12
    24
#include <fstream>
mathieu@12
    25
#include <vector>
mathieu@3365
    26
#include <string.h>
mathieu@12
    27
mathieu@16
    28
using namespace ns3;
mathieu@12
    29
mathieu@12
    30
mathieu@190
    31
bool g_debug = false;
mathieu@12
    32
mathieu@190
    33
class Bench 
mathieu@190
    34
{
mathieu@12
    35
public:
mathieu@4682
    36
  Bench ();
mathieu@150
    37
  void ReadDistribution (std::istream &istream);
mathieu@150
    38
  void SetTotal (uint32_t total);
mathieu@150
    39
  void RunBench (void);
mathieu@12
    40
private:
mathieu@150
    41
  void Cb (void);
mathieu@150
    42
  std::vector<uint64_t> m_distribution;
mathieu@150
    43
  std::vector<uint64_t>::const_iterator m_current;
mathieu@150
    44
  uint32_t m_n;
mathieu@150
    45
  uint32_t m_total;
mathieu@12
    46
};
mathieu@12
    47
mathieu@4682
    48
Bench::Bench ()
mathieu@4682
    49
  : m_n (0),
mathieu@4682
    50
    m_total (0)
mathieu@4682
    51
{}
mathieu@4682
    52
mathieu@12
    53
void 
mathieu@122
    54
Bench::SetTotal (uint32_t total)
mathieu@12
    55
{
mathieu@150
    56
  m_total = total;
mathieu@12
    57
}
mathieu@12
    58
mathieu@12
    59
void
mathieu@122
    60
Bench::ReadDistribution (std::istream &input)
mathieu@12
    61
{
mathieu@150
    62
  double data;
mathieu@190
    63
  while (!input.eof ()) 
mathieu@190
    64
    {
mathieu@190
    65
      if (input >> data) 
mathieu@190
    66
        {
mathieu@150
    67
          uint64_t ns = (uint64_t) (data * 1000000000);
mathieu@150
    68
          m_distribution.push_back (ns);
mathieu@190
    69
        } 
mathieu@190
    70
      else 
mathieu@190
    71
        {
mathieu@150
    72
          input.clear ();
mathieu@150
    73
          std::string line;
mathieu@150
    74
          input >> line;
mathieu@190
    75
        }
mathieu@190
    76
    }
mathieu@12
    77
}
mathieu@12
    78
mathieu@12
    79
void
mathieu@122
    80
Bench::RunBench (void) 
mathieu@12
    81
{
mathieu@150
    82
  SystemWallClockMs time;
mathieu@150
    83
  double init, simu;
mathieu@150
    84
  time.Start ();
mathieu@150
    85
  for (std::vector<uint64_t>::const_iterator i = m_distribution.begin ();
mathieu@190
    86
       i != m_distribution.end (); i++) 
mathieu@190
    87
    {
mathieu@162
    88
      Simulator::Schedule (NanoSeconds (*i), &Bench::Cb, this);
mathieu@190
    89
    }
mathieu@150
    90
  init = time.End ();
mathieu@204
    91
  init /= 1000;
mathieu@12
    92
mathieu@150
    93
  m_current = m_distribution.begin ();
mathieu@12
    94
mathieu@150
    95
  time.Start ();
mathieu@150
    96
  Simulator::Run ();
mathieu@150
    97
  simu = time.End ();
mathieu@204
    98
  simu /= 1000;
mathieu@12
    99
mathieu@150
   100
  std::cout <<
mathieu@150
   101
      "init n=" << m_distribution.size () << ", time=" << init << "s" << std::endl <<
mathieu@150
   102
      "simu n=" << m_n << ", time=" <<simu << "s" << std::endl <<
mathieu@150
   103
      "init " << ((double)m_distribution.size ()) / init << " insert/s, avg insert=" <<
mathieu@150
   104
      init / ((double)m_distribution.size ())<< "s" << std::endl <<
mathieu@150
   105
      "simu " << ((double)m_n) / simu<< " hold/s, avg hold=" << 
mathieu@150
   106
      simu / ((double)m_n) << "s" << std::endl
mathieu@150
   107
      ;
mathieu@12
   108
}
mathieu@12
   109
mathieu@12
   110
void
mathieu@122
   111
Bench::Cb (void)
mathieu@12
   112
{
mathieu@190
   113
  if (m_n > m_total) 
mathieu@190
   114
    {
mathieu@150
   115
      return;
mathieu@190
   116
    }
mathieu@190
   117
  if (m_current == m_distribution.end ()) 
mathieu@190
   118
    {
mathieu@150
   119
      m_current = m_distribution.begin ();
mathieu@190
   120
    }
mathieu@190
   121
  if (g_debug) 
mathieu@190
   122
    {
mathieu@163
   123
      std::cerr << "event at " << Simulator::Now ().GetSeconds () << "s" << std::endl;
mathieu@190
   124
    }
mathieu@162
   125
  Simulator::Schedule (NanoSeconds (*m_current), &Bench::Cb, this);
mathieu@150
   126
  m_current++;
mathieu@150
   127
  m_n++;
mathieu@12
   128
}
mathieu@12
   129
mathieu@189
   130
void
mathieu@189
   131
PrintHelp (void)
mathieu@189
   132
{
mathieu@189
   133
  std::cout << "bench-simulator filename [options]"<<std::endl;
mathieu@189
   134
  std::cout << "  filename: a string which identifies the input distribution. \"-\" represents stdin." << std::endl;
mathieu@189
   135
  std::cout << "  Options:"<<std::endl;
mathieu@189
   136
  std::cout << "      --list: use std::list scheduler"<<std::endl;
mathieu@189
   137
  std::cout << "      --map: use std::map cheduler"<<std::endl;
mathieu@189
   138
  std::cout << "      --heap: use Binary Heap scheduler"<<std::endl;
mathieu@189
   139
  std::cout << "      --debug: enable some debugging"<<std::endl;
mathieu@189
   140
}
mathieu@189
   141
mathieu@12
   142
int main (int argc, char *argv[])
mathieu@12
   143
{
mathieu@150
   144
  char const *filename = argv[1];
mathieu@150
   145
  std::istream *input;
mathieu@204
   146
  uint32_t n = 1;
mathieu@204
   147
  uint32_t total = 20000;
mathieu@189
   148
  if (argc == 1)
mathieu@189
   149
    {
mathieu@189
   150
      PrintHelp ();
mathieu@189
   151
      return 0;
mathieu@189
   152
    }
mathieu@150
   153
  argc-=2;
mathieu@150
   154
  argv+= 2;
mathieu@190
   155
  if (strcmp (filename, "-") == 0) 
mathieu@190
   156
    {
mathieu@150
   157
      input = &std::cin;
mathieu@190
   158
    } 
mathieu@190
   159
  else 
mathieu@190
   160
    {
mathieu@150
   161
      input = new std::ifstream (filename);
mathieu@190
   162
    }
mathieu@190
   163
  while (argc > 0) 
mathieu@190
   164
    {
guillaume@5507
   165
      ObjectFactory factory;
mathieu@190
   166
      if (strcmp ("--list", argv[0]) == 0) 
mathieu@190
   167
        {
guillaume@5507
   168
          factory.SetTypeId ("ns3::ListScheduler");
guillaume@5507
   169
          Simulator::SetScheduler (factory);
mathieu@190
   170
        } 
mathieu@190
   171
      else if (strcmp ("--heap", argv[0]) == 0) 
mathieu@190
   172
        {
guillaume@5507
   173
          factory.SetTypeId ("ns3::HeapScheduler");
guillaume@5507
   174
          Simulator::SetScheduler (factory);
mathieu@190
   175
        } 
mathieu@190
   176
      else if (strcmp ("--map", argv[0]) == 0) 
mathieu@190
   177
        {
guillaume@5507
   178
          factory.SetTypeId ("ns3::HeapScheduler");
guillaume@5507
   179
          Simulator::SetScheduler (factory);
mathieu@190
   180
        } 
mathieu@4053
   181
      else if (strcmp ("--calendar", argv[0]) == 0)
mathieu@4053
   182
        {
guillaume@5507
   183
          factory.SetTypeId ("ns3::CalendarScheduler");
guillaume@5507
   184
          Simulator::SetScheduler (factory);
mathieu@4053
   185
        }
mathieu@190
   186
      else if (strcmp ("--debug", argv[0]) == 0) 
mathieu@190
   187
        {
mathieu@190
   188
          g_debug = true;
mathieu@190
   189
        } 
mathieu@204
   190
      else if (strncmp ("--total=", argv[0], strlen("--total=")) == 0) 
mathieu@204
   191
        {
mathieu@204
   192
          total = atoi (argv[0]+strlen ("--total="));
mathieu@204
   193
        } 
mathieu@204
   194
      else if (strncmp ("--n=", argv[0], strlen("--n=")) == 0) 
mathieu@204
   195
        {
mathieu@204
   196
          n = atoi (argv[0]+strlen ("--n="));
mathieu@204
   197
        } 
mathieu@204
   198
mathieu@150
   199
      argc--;
mathieu@150
   200
      argv++;
mathieu@150
   201
  }
mathieu@150
   202
  Bench *bench = new Bench ();
mathieu@150
   203
  bench->ReadDistribution (*input);
mathieu@204
   204
  bench->SetTotal (total);
mathieu@204
   205
  for (uint32_t i = 0; i < n; i++)
mathieu@204
   206
    {
mathieu@204
   207
      bench->RunBench ();
mathieu@204
   208
    }
mathieu@12
   209
mathieu@150
   210
  return 0;
mathieu@12
   211
}