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 <chrono>
18 
19 #include <cstdio>
20 #include <cstdlib>
21 
22 #include <iostream>
23 #include <fstream>
24 
25 #include "ns3/core-module.h"
26 
27 using namespace ns3;
28 
39 void
40 PerfFile (FILE *file, uint32_t n, const char *buffer, uint32_t size)
41 {
42  for (uint32_t i = 0; i < n; ++i)
43  {
44  if (std::fwrite (buffer, 1, size, file) != size)
45  {
46  NS_ABORT_MSG ("PerfFile(): fwrite error");
47  }
48  }
49 }
50 
61 void
62 PerfStream (std::ostream &stream, uint32_t n, const char *buffer, uint32_t size)
63 {
64  for (uint32_t i = 0; i < n; ++i)
65  {
66  stream.write (buffer, size);
67  }
68 }
69 
70 int
71 main (int argc, char *argv[])
72 {
73  uint32_t n = 100000;
74  uint32_t iter = 50;
75  bool doStream = false;
76  bool binmode = true;
77 
78  CommandLine cmd (__FILE__);
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  auto minResultNs = std::chrono::duration_cast<std::chrono::nanoseconds> (
87 
88  char buffer[1024];
89 
90  if (doStream)
91  {
92  //
93  // This will probably run on a machine doing other things. Run it some
94  // relatively large number of times and try to find a minimum, which
95  // will hopefully represent a time when it runs free of interference.
96  //
97  for (uint32_t i = 0; i < iter; ++i)
98  {
99  std::ofstream stream;
100  if (binmode)
101  {
102  stream.open ("streamtest", std::ios_base::binary | std::ios_base::out);
103  }
104  else
105  {
106  stream.open ("streamtest", std::ios_base::out);
107  }
108 
109  auto start = std::chrono::steady_clock::now ();
110  PerfStream (stream, n, buffer, 1024);
111  auto end = std::chrono::steady_clock::now ();
112  auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds> (end - start);
113  resultNs = std::min (resultNs, minResultNs);
114  stream.close ();
115  std::cout << "."; 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 = 0;
138  std::cout << "."; std::cout.flush ();
139  }
140  std::cout << std::endl;
141  }
142 
143  std::cout << argv[0] << ": " << minResultNs.count () << "ns" << std::endl;
144 
145  return 0;
146 }
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
Parse command-line arguments.
Definition: command-line.h:229
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
void PerfFile(FILE *file, uint32_t n, const char *buffer, uint32_t size)
Check the performance of writing to file.
Definition: perf-io.cc:40
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:62
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35
def start()
Definition: core.py:1853