A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/network-module.h"
28 #include "ns3/abort.h"
29 
30 using namespace ns3;
31 
32 static const uint64_t US_PER_NS = (uint64_t)1000;
33 static const uint64_t US_PER_SEC = (uint64_t)1000000;
34 static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
35 
36 uint64_t
38 {
39  struct timeval tv;
40  gettimeofday (&tv, NULL);
41 
42  uint64_t nsResult = tv.tv_sec * NS_PER_SEC + tv.tv_usec * US_PER_NS;
43  return nsResult;
44 }
45 
46 void
47 PerfFile (FILE *file, uint32_t n, const char *buffer, uint32_t size)
48 {
49  for (uint32_t i = 0; i < n; ++i)
50  {
51  if (std::fwrite (buffer, 1, size, file) != size)
52  {
53  NS_ABORT_MSG ("PerfFile(): fwrite error");
54  }
55  }
56 }
57 
58 void
59 PerfStream (ostream &stream, uint32_t n, const char *buffer, uint32_t size)
60 {
61 
62  for (uint32_t i = 0; i < n; ++i)
63  {
64  stream.write (buffer, size);
65  }
66 }
67 
68 int
69 main (int argc, char *argv[])
70 {
71  uint32_t n = 100000;
72  uint32_t iter = 50;
73  bool doStream = false;
74  bool binmode = true;
75 
76 
77  CommandLine cmd;
78  cmd.AddValue ("n", "How many times to write (defaults to 100000", n);
79  cmd.AddValue ("iter", "How many times to run the test looking for a min (defaults to 50)", iter);
80  cmd.AddValue ("doStream", "Run the C++ I/O benchmark otherwise the C I/O ", doStream);
81  cmd.AddValue ("binmode", "Select binary mode for the C++ I/O benchmark (defaults to true)", binmode);
82  cmd.Parse (argc, argv);
83 
84  uint64_t result = std::numeric_limits<uint64_t>::max ();
85 
86  char buffer[1024];
87 
88  if (doStream)
89  {
90  //
91  // This will probably run on a machine doing other things. Run it some
92  // relatively large number of times and try to find a minimum, which
93  // will hopefully represent a time when it runs free of interference.
94  //
95  for (uint32_t i = 0; i < iter; ++i)
96  {
97  ofstream stream;
98  if (binmode)
99  {
100  stream.open ("streamtest", std::ios_base::binary | std::ios_base::out);
101  }
102  else
103  {
104  stream.open ("streamtest", std::ios_base::out);
105  }
106 
107  uint64_t start = GetRealtimeInNs ();
108  PerfStream (stream, n, buffer, 1024);
109  uint64_t et = GetRealtimeInNs () - start;
110  result = min (result, et);
111  stream.close ();
112  std::cout << "."; std::cout.flush ();
113  }
114  cout << std::endl;
115  }
116  else
117  {
118  //
119  // This will probably run on a machine doing other things. Run it some
120  // relatively large number of times and try to find a minimum, which
121  // will hopefully represent a time when it runs free of interference.
122  //
123  for (uint32_t i = 0; i < iter; ++i)
124  {
125  FILE *file = fopen ("filetest", "w");
126 
127  uint64_t start = GetRealtimeInNs ();
128  PerfFile (file, n, buffer, 1024);
129  uint64_t et = GetRealtimeInNs () - start;
130  result = std::min (result, et);
131  fclose (file);
132  file = 0;
133  std::cout << "."; std::cout.flush ();
134  }
135  std::cout << std::endl;
136  }
137  std::cout << argv[0] << ": " << result << "ns" << std::endl;
138 }
uint64_t GetRealtimeInNs(void)
Definition: perf-io.cc:37
void PerfFile(FILE *file, uint32_t n, const char *buffer, uint32_t size)
Definition: perf-io.cc:47
static const uint64_t NS_PER_SEC
Definition: perf-io.cc:34
Parse command-line arguments.
Definition: command-line.h:152
static const uint64_t US_PER_NS
Definition: perf-io.cc:32
static const uint64_t US_PER_SEC
Definition: perf-io.cc:33
#define NS_ABORT_MSG(msg)
Abnormal program termination.
Definition: abort.h:43
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
int main(int argc, char *argv[])
Definition: perf-io.cc:69
void Parse(int argc, char *argv[])
Parse the program arguments.
void PerfStream(ostream &stream, uint32_t n, const char *buffer, uint32_t size)
Definition: perf-io.cc:59