Files
unison/utils/bench-simulator.cc

211 lines
5.1 KiB
C++
Raw Normal View History

2006-11-01 13:11:30 +01:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2006-08-29 17:47:17 +02:00
/*
* Copyright (c) 2006 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2008-04-15 10:09:42 -07:00
#include "ns3/core-module.h"
2006-08-29 17:47:17 +02:00
#include <iostream>
#include <fstream>
#include <vector>
2008-07-02 03:16:36 -07:00
#include <string.h>
2006-08-29 17:47:17 +02:00
2006-08-29 17:55:34 +02:00
using namespace ns3;
2006-08-29 17:47:17 +02:00
2006-12-12 13:53:30 +01:00
bool g_debug = false;
2006-08-29 17:47:17 +02:00
2006-12-12 13:53:30 +01:00
class Bench
{
2006-08-29 17:47:17 +02:00
public:
2009-07-15 08:36:41 +02:00
Bench ();
2006-11-01 13:11:30 +01:00
void ReadDistribution (std::istream &istream);
void SetTotal (uint32_t total);
void RunBench (void);
2006-08-29 17:47:17 +02:00
private:
2006-11-01 13:11:30 +01:00
void Cb (void);
std::vector<uint64_t> m_distribution;
std::vector<uint64_t>::const_iterator m_current;
uint32_t m_n;
uint32_t m_total;
2006-08-29 17:47:17 +02:00
};
2009-07-15 08:36:41 +02:00
Bench::Bench ()
: m_n (0),
m_total (0)
{}
2006-08-29 17:47:17 +02:00
void
2006-10-06 13:37:25 +02:00
Bench::SetTotal (uint32_t total)
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
m_total = total;
2006-08-29 17:47:17 +02:00
}
void
2006-10-06 13:37:25 +02:00
Bench::ReadDistribution (std::istream &input)
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
double data;
2006-12-12 13:53:30 +01:00
while (!input.eof ())
{
if (input >> data)
{
2006-11-01 13:11:30 +01:00
uint64_t ns = (uint64_t) (data * 1000000000);
m_distribution.push_back (ns);
2006-12-12 13:53:30 +01:00
}
else
{
2006-11-01 13:11:30 +01:00
input.clear ();
std::string line;
input >> line;
2006-12-12 13:53:30 +01:00
}
}
2006-08-29 17:47:17 +02:00
}
void
2006-10-06 13:37:25 +02:00
Bench::RunBench (void)
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
SystemWallClockMs time;
double init, simu;
time.Start ();
for (std::vector<uint64_t>::const_iterator i = m_distribution.begin ();
2006-12-12 13:53:30 +01:00
i != m_distribution.end (); i++)
{
2006-11-21 15:50:09 +01:00
Simulator::Schedule (NanoSeconds (*i), &Bench::Cb, this);
2006-12-12 13:53:30 +01:00
}
2006-11-01 13:11:30 +01:00
init = time.End ();
init /= 1000;
2006-08-29 17:47:17 +02:00
2006-11-01 13:11:30 +01:00
m_current = m_distribution.begin ();
2006-08-29 17:47:17 +02:00
2006-11-01 13:11:30 +01:00
time.Start ();
Simulator::Run ();
simu = time.End ();
simu /= 1000;
2006-08-29 17:47:17 +02:00
2006-11-01 13:11:30 +01:00
std::cout <<
"init n=" << m_distribution.size () << ", time=" << init << "s" << std::endl <<
"simu n=" << m_n << ", time=" <<simu << "s" << std::endl <<
"init " << ((double)m_distribution.size ()) / init << " insert/s, avg insert=" <<
init / ((double)m_distribution.size ())<< "s" << std::endl <<
"simu " << ((double)m_n) / simu<< " hold/s, avg hold=" <<
simu / ((double)m_n) << "s" << std::endl
;
2006-08-29 17:47:17 +02:00
}
void
2006-10-06 13:37:25 +02:00
Bench::Cb (void)
2006-08-29 17:47:17 +02:00
{
2006-12-12 13:53:30 +01:00
if (m_n > m_total)
{
2006-11-01 13:11:30 +01:00
return;
2006-12-12 13:53:30 +01:00
}
if (m_current == m_distribution.end ())
{
2006-11-01 13:11:30 +01:00
m_current = m_distribution.begin ();
2006-12-12 13:53:30 +01:00
}
if (g_debug)
{
std::cerr << "event at " << Simulator::Now ().GetSeconds () << "s" << std::endl;
2006-12-12 13:53:30 +01:00
}
2006-11-21 15:50:09 +01:00
Simulator::Schedule (NanoSeconds (*m_current), &Bench::Cb, this);
2006-11-01 13:11:30 +01:00
m_current++;
m_n++;
2006-08-29 17:47:17 +02:00
}
void
PrintHelp (void)
{
std::cout << "bench-simulator filename [options]"<<std::endl;
std::cout << " filename: a string which identifies the input distribution. \"-\" represents stdin." << std::endl;
std::cout << " Options:"<<std::endl;
std::cout << " --list: use std::list scheduler"<<std::endl;
std::cout << " --map: use std::map cheduler"<<std::endl;
std::cout << " --heap: use Binary Heap scheduler"<<std::endl;
std::cout << " --debug: enable some debugging"<<std::endl;
}
2006-08-29 17:47:17 +02:00
int main (int argc, char *argv[])
{
2006-11-01 13:11:30 +01:00
char const *filename = argv[1];
std::istream *input;
uint32_t n = 1;
uint32_t total = 20000;
if (argc == 1)
{
PrintHelp ();
return 0;
}
2006-11-01 13:11:30 +01:00
argc-=2;
argv+= 2;
2006-12-12 13:53:30 +01:00
if (strcmp (filename, "-") == 0)
{
2006-11-01 13:11:30 +01:00
input = &std::cin;
2006-12-12 13:53:30 +01:00
}
else
{
2006-11-01 13:11:30 +01:00
input = new std::ifstream (filename);
2006-12-12 13:53:30 +01:00
}
while (argc > 0)
{
ObjectFactory factory;
2006-12-12 13:53:30 +01:00
if (strcmp ("--list", argv[0]) == 0)
{
factory.SetTypeId ("ns3::ListScheduler");
Simulator::SetScheduler (factory);
2006-12-12 13:53:30 +01:00
}
else if (strcmp ("--heap", argv[0]) == 0)
{
factory.SetTypeId ("ns3::HeapScheduler");
Simulator::SetScheduler (factory);
2006-12-12 13:53:30 +01:00
}
else if (strcmp ("--map", argv[0]) == 0)
{
factory.SetTypeId ("ns3::HeapScheduler");
Simulator::SetScheduler (factory);
2006-12-12 13:53:30 +01:00
}
2009-01-09 07:51:42 +01:00
else if (strcmp ("--calendar", argv[0]) == 0)
{
factory.SetTypeId ("ns3::CalendarScheduler");
Simulator::SetScheduler (factory);
2009-01-09 07:51:42 +01:00
}
2006-12-12 13:53:30 +01:00
else if (strcmp ("--debug", argv[0]) == 0)
{
g_debug = true;
}
else if (strncmp ("--total=", argv[0], strlen("--total=")) == 0)
{
total = atoi (argv[0]+strlen ("--total="));
}
else if (strncmp ("--n=", argv[0], strlen("--n=")) == 0)
{
n = atoi (argv[0]+strlen ("--n="));
}
2006-11-01 13:11:30 +01:00
argc--;
argv++;
}
Bench *bench = new Bench ();
bench->ReadDistribution (*input);
bench->SetTotal (total);
for (uint32_t i = 0; i < n; i++)
{
bench->RunBench ();
}
2006-08-29 17:47:17 +02:00
2006-11-01 13:11:30 +01:00
return 0;
2006-08-29 17:47:17 +02:00
}