utils/bench-simulator.cc
changeset 53 ae406f4957d5
parent 38 8ca1b9b1d3a9
child 54 f860e6f94787
equal deleted inserted replaced
52:72a52d59ee9f 53:ae406f4957d5
    26 #include <vector>
    26 #include <vector>
    27 
    27 
    28 using namespace ns3;
    28 using namespace ns3;
    29 
    29 
    30 
    30 
    31 bool g_debug = false;
    31 bool gDebug = false;
    32 
    32 
    33 class Bench {
    33 class Bench {
    34 public:
    34 public:
    35 	void read_distribution (std::istream &istream);
    35 	void readDistribution (std::istream &istream);
    36 	void set_total (uint32_t total);
    36 	void setTotal (uint32_t total);
    37 	void bench (void);
    37 	void bench (void);
    38 private:
    38 private:
    39 	void cb (void);
    39 	void cb (void);
    40 	std::vector<uint64_t> m_distribution;
    40 	std::vector<uint64_t> m_distribution;
    41 	std::vector<uint64_t>::const_iterator m_current;
    41 	std::vector<uint64_t>::const_iterator m_current;
    42 	uint32_t m_n;
    42 	uint32_t m_n;
    43 	uint32_t m_total;
    43 	uint32_t m_total;
    44 };
    44 };
    45 
    45 
    46 void 
    46 void 
    47 Bench::set_total (uint32_t total)
    47 Bench::setTotal (uint32_t total)
    48 {
    48 {
    49 	m_total = total;
    49 	m_total = total;
    50 }
    50 }
    51 
    51 
    52 void
    52 void
    53 Bench::read_distribution (std::istream &input)
    53 Bench::readDistribution (std::istream &input)
    54 {
    54 {
    55 	double data;
    55 	double data;
    56 	while (!input.eof ()) {
    56 	while (!input.eof ()) {
    57 		if (input >> data) {
    57 		if (input >> data) {
    58 			uint64_t ns = (uint64_t) (data * 1000000000);
    58 			uint64_t ns = (uint64_t) (data * 1000000000);
    71 	SystemWallClockMs time;
    71 	SystemWallClockMs time;
    72 	double init, simu;
    72 	double init, simu;
    73 	time.start ();
    73 	time.start ();
    74 	for (std::vector<uint64_t>::const_iterator i = m_distribution.begin ();
    74 	for (std::vector<uint64_t>::const_iterator i = m_distribution.begin ();
    75 	     i != m_distribution.end (); i++) {
    75 	     i != m_distribution.end (); i++) {
    76 		Simulator::schedule (Time::abs_ns (*i), &Bench::cb, this);
    76 		Simulator::schedule (Time::absNs (*i), &Bench::cb, this);
    77 	}
    77 	}
    78 	init = time.end ();
    78 	init = time.end ();
    79 
    79 
    80 	m_current = m_distribution.begin ();
    80 	m_current = m_distribution.begin ();
    81 
    81 
   100 		return;
   100 		return;
   101 	}
   101 	}
   102 	if (m_current == m_distribution.end ()) {
   102 	if (m_current == m_distribution.end ()) {
   103 		m_current = m_distribution.begin ();
   103 		m_current = m_distribution.begin ();
   104 	}
   104 	}
   105 	if (g_debug) {
   105 	if (gDebug) {
   106 		std::cerr << "event at " << Simulator::now ().s () << "s" << std::endl;
   106 		std::cerr << "event at " << Simulator::now ().s () << "s" << std::endl;
   107 	}
   107 	}
   108 	Simulator::schedule (Time::abs_ns (*m_current), &Bench::cb, this);
   108 	Simulator::schedule (Time::absNs (*m_current), &Bench::cb, this);
   109 	m_current++;
   109 	m_current++;
   110 	m_n++;
   110 	m_n++;
   111 }
   111 }
   112 
   112 
   113 int main (int argc, char *argv[])
   113 int main (int argc, char *argv[])
   121 	} else {
   121 	} else {
   122 		input = new std::ifstream (filename);
   122 		input = new std::ifstream (filename);
   123 	}
   123 	}
   124 	while (argc > 0) {
   124 	while (argc > 0) {
   125 		if (strcmp ("--list", argv[0]) == 0) {
   125 		if (strcmp ("--list", argv[0]) == 0) {
   126 			Simulator::set_linked_list ();
   126 			Simulator::setLinkedList ();
   127 		} else if (strcmp ("--heap", argv[0]) == 0) {
   127 		} else if (strcmp ("--heap", argv[0]) == 0) {
   128 			Simulator::set_binary_heap ();
   128 			Simulator::setBinaryHeap ();
   129 		} else if (strcmp ("--map", argv[0]) == 0) {
   129 		} else if (strcmp ("--map", argv[0]) == 0) {
   130 			Simulator::set_std_map ();
   130 			Simulator::setStdMap ();
   131 		} else if (strcmp ("--debug", argv[0]) == 0) {
   131 		} else if (strcmp ("--debug", argv[0]) == 0) {
   132 			g_debug = true;
   132 			gDebug = true;
   133 		} else if (strncmp ("--log=", argv[0],strlen ("--log=")) == 0) {
   133 		} else if (strncmp ("--log=", argv[0],strlen ("--log=")) == 0) {
   134 			char const *filename = argv[0] + strlen ("--log=");
   134 			char const *filename = argv[0] + strlen ("--log=");
   135 			Simulator::enable_log_to (filename);
   135 			Simulator::enableLogTo (filename);
   136 		}
   136 		}
   137 		argc--;
   137 		argc--;
   138 		argv++;
   138 		argv++;
   139 	}
   139 	}
   140 	Bench *bench = new Bench ();
   140 	Bench *bench = new Bench ();
   141 	bench->read_distribution (*input);
   141 	bench->readDistribution (*input);
   142 	bench->set_total (20000);
   142 	bench->setTotal (20000);
   143 	bench->bench ();
   143 	bench->bench ();
   144 
   144 
   145 	return 0;
   145 	return 0;
   146 }
   146 }