A Discrete-Event Network Simulator
API
perf-io.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 #include <ctime>
18 #include <sys/time.h>
19 
20 #include <cstdio>
21 #include <cstdlib>
22 
23 #include <iostream>
24 #include <fstream>
25 
26 #include "ns3/core-module.h"
27 #include "ns3/abort.h"
28 
29 using namespace ns3;
30 
31 static const uint64_t US_PER_NS = (uint64_t)1000;
32 static const uint64_t US_PER_SEC = (uint64_t)1000000;
33 static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
34 
35 uint64_t
37 {
38  struct timeval tv;
39  gettimeofday (&tv, NULL);
40 
41  uint64_t nsResult = tv.tv_sec * NS_PER_SEC + tv.tv_usec * US_PER_NS;
42  return nsResult;
43 }
44 
45 void
46 PerfFile (FILE *file, uint32_t n, const char *buffer, uint32_t size)
47 {
48  for (uint32_t i = 0; i < n; ++i)
49  {
50  if (std::fwrite (buffer, 1, size, file) != size)
51  {
52  NS_ABORT_MSG ("PerfFile(): fwrite error");
53  }
54  }
55 }
56 
57 void
58 PerfStream (ostream &stream, uint32_t n, const char *buffer, uint32_t size)
59 {
60 
61  for (uint32_t i = 0; i < n; ++i)
62  {
63  stream.write (buffer, size);
64  }
65 }
66 
67 int
68 main (int argc, char *argv[])
69 {
70  uint32_t n = 100000;
71  uint32_t iter = 50;
72  bool doStream = false;
73  bool binmode = true;
74 
75 
77  cmd.AddValue ("n", "How many times to write (defaults to 100000", n);
78  cmd.AddValue ("iter", "How many times to run the test looking for a min (defaults to 50)", iter);
79  cmd.AddValue ("doStream", "Run the C++ I/O benchmark otherwise the C I/O ", doStream);
80  cmd.AddValue ("binmode", "Select binary mode for the C++ I/O benchmark (defaults to true)", binmode);
81  cmd.Parse (argc, argv);
82 
83  uint64_t result = std::numeric_limits<uint64_t>::max ();
84 
85  char buffer[1024];
86 
87  if (doStream)
88  {
89  //
90  // This will probably run on a machine doing other things. Run it some
91  // relatively large number of times and try to find a minimum, which
92  // will hopefully represent a time when it runs free of interference.
93  //
94  for (uint32_t i = 0; i < iter; ++i)
95  {
96  ofstream stream;
97  if (binmode)
98  {
99  stream.open ("streamtest", std::ios_base::binary | std::ios_base::out);
100  }
101  else
102  {
103  stream.open ("streamtest", std::ios_base::out);
104  }
105 
106  uint64_t start = GetRealtimeInNs ();
107  PerfStream (stream, n, buffer, 1024);
108  uint64_t et = GetRealtimeInNs () - start;
109  result = min (result, et);
110  stream.close ();
111  std::cout << "."; std::cout.flush ();
112  }
113  cout << std::endl;
114  }
115  else
116  {
117  //
118  // This will probably run on a machine doing other things. Run it some
119  // relatively large number of times and try to find a minimum, which
120  // will hopefully represent a time when it runs free of interference.
121  //
122  for (uint32_t i = 0; i < iter; ++i)
123  {
124  FILE *file = fopen ("filetest", "w");
125 
126  uint64_t start = GetRealtimeInNs ();
127  PerfFile (file, n, buffer, 1024);
128  uint64_t et = GetRealtimeInNs () - start;
129  result = std::min (result, et);
130  fclose (file);
131  file = 0;
132  std::cout << "."; std::cout.flush ();
133  }
134  std::cout << std::endl;
135  }
136  std::cout << argv[0] << ": " << result << "ns" << std::endl;
137 }
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
#define min(a, b)
Definition: 80211b.c:42
def start()
Definition: core.py:1855
uint64_t GetRealtimeInNs(void)
Definition: perf-io.cc:36
void PerfFile(FILE *file, uint32_t n, const char *buffer, uint32_t size)
Definition: perf-io.cc:46
cmd
Definition: second.py:35
static const uint64_t NS_PER_SEC
Definition: perf-io.cc:33
#define max(a, b)
Definition: 80211b.c:43
Parse command-line arguments.
Definition: command-line.h:213
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint64_t US_PER_NS
Definition: perf-io.cc:31
static const uint64_t US_PER_SEC
Definition: perf-io.cc:32
double min(double x, double y)
void PerfStream(ostream &stream, uint32_t n, const char *buffer, uint32_t size)
Definition: perf-io.cc:58