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