A Discrete-Event Network Simulator
API
perf-io.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 #include "ns3/core-module.h"
17 
18 #include <chrono>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <fstream>
22 #include <iostream>
23 
24 using namespace ns3;
25 
36 void
37 PerfFile(FILE* file, uint32_t n, const char* buffer, uint32_t size)
38 {
39  for (uint32_t i = 0; i < n; ++i)
40  {
41  if (std::fwrite(buffer, 1, size, file) != size)
42  {
43  NS_ABORT_MSG("PerfFile(): fwrite error");
44  }
45  }
46 }
47 
58 void
59 PerfStream(std::ostream& stream, uint32_t n, const char* buffer, uint32_t size)
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  CommandLine cmd(__FILE__);
76  cmd.AddValue("n", "How many times to write (defaults to 100000", n);
77  cmd.AddValue("iter", "How many times to run the test looking for a min (defaults to 50)", iter);
78  cmd.AddValue("doStream", "Run the C++ I/O benchmark otherwise the C I/O ", doStream);
79  cmd.AddValue("binmode",
80  "Select binary mode for the C++ I/O benchmark (defaults to true)",
81  binmode);
82  cmd.Parse(argc, argv);
83 
84  auto minResultNs =
85  std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::nanoseconds::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  std::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  auto start = std::chrono::steady_clock::now();
109  PerfStream(stream, n, buffer, 1024);
110  auto end = std::chrono::steady_clock::now();
111  auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
112  resultNs = std::min(resultNs, minResultNs);
113  stream.close();
114  std::cout << ".";
115  std::cout.flush();
116  }
117 
118  std::cout << std::endl;
119  }
120  else
121  {
122  //
123  // This will probably run on a machine doing other things. Run it some
124  // relatively large number of times and try to find a minimum, which
125  // will hopefully represent a time when it runs free of interference.
126  //
127  for (uint32_t i = 0; i < iter; ++i)
128  {
129  FILE* file = fopen("filetest", "w");
130 
131  auto start = std::chrono::steady_clock::now();
132  PerfFile(file, n, buffer, 1024);
133  auto end = std::chrono::steady_clock::now();
134  auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
135  resultNs = std::min(resultNs, minResultNs);
136  fclose(file);
137  file = nullptr;
138  std::cout << ".";
139  std::cout.flush();
140  }
141  std::cout << std::endl;
142  }
143 
144  std::cout << argv[0] << ": " << minResultNs.count() << "ns" << std::endl;
145 
146  return 0;
147 }
#define min(a, b)
Definition: 80211b.c:41
#define max(a, b)
Definition: 80211b.c:42
Parse command-line arguments.
Definition: command-line.h:232
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
void PerfFile(FILE *file, uint32_t n, const char *buffer, uint32_t size)
Check the performance of writing to file.
Definition: perf-io.cc:37
void PerfStream(std::ostream &stream, uint32_t n, const char *buffer, uint32_t size)
Check the performance of writing to an output stream.
Definition: perf-io.cc:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40