A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
perf-io.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "ns3/core-module.h"
6
7#include <chrono>
8#include <cstdio>
9#include <cstdlib>
10#include <fstream>
11#include <iostream>
12
13using namespace ns3;
14
15/**
16 * \ingroup system-tests-perf
17 *
18 * Check the performance of writing to file.
19 *
20 * \param file The file to write to.
21 * \param n The number of writes to perform.
22 * \param buffer The buffer to write.
23 * \param size The buffer size.
24 */
25void
26PerfFile(FILE* file, uint32_t n, const char* buffer, uint32_t size)
27{
28 for (uint32_t i = 0; i < n; ++i)
29 {
30 if (std::fwrite(buffer, 1, size, file) != size)
31 {
32 NS_ABORT_MSG("PerfFile(): fwrite error");
33 }
34 }
35}
36
37/**
38 * \ingroup system-tests-perf
39 *
40 * Check the performance of writing to an output stream.
41 *
42 * \param stream The output stream to write to.
43 * \param n The number of writes to perform.
44 * \param buffer The buffer to write.
45 * \param size The buffer size.
46 */
47void
48PerfStream(std::ostream& stream, uint32_t n, const char* buffer, uint32_t size)
49{
50 for (uint32_t i = 0; i < n; ++i)
51 {
52 stream.write(buffer, size);
53 }
54}
55
56int
57main(int argc, char* argv[])
58{
59 uint32_t n = 100000;
60 uint32_t iter = 50;
61 bool doStream = false;
62 bool binmode = true;
63
64 CommandLine cmd(__FILE__);
65 cmd.AddValue("n", "How many times to write (defaults to 100000", n);
66 cmd.AddValue("iter", "How many times to run the test looking for a min (defaults to 50)", iter);
67 cmd.AddValue("doStream", "Run the C++ I/O benchmark otherwise the C I/O ", doStream);
68 cmd.AddValue("binmode",
69 "Select binary mode for the C++ I/O benchmark (defaults to true)",
70 binmode);
71 cmd.Parse(argc, argv);
72
73 auto minResultNs =
74 std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::nanoseconds::max());
75
76 char buffer[1024];
77
78 if (doStream)
79 {
80 //
81 // This will probably run on a machine doing other things. Run it some
82 // relatively large number of times and try to find a minimum, which
83 // will hopefully represent a time when it runs free of interference.
84 //
85 for (uint32_t i = 0; i < iter; ++i)
86 {
87 std::ofstream stream;
88 if (binmode)
89 {
90 stream.open("streamtest", std::ios_base::binary | std::ios_base::out);
91 }
92 else
93 {
94 stream.open("streamtest", std::ios_base::out);
95 }
96
97 auto start = std::chrono::steady_clock::now();
98 PerfStream(stream, n, buffer, 1024);
99 auto end = std::chrono::steady_clock::now();
100 auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
101 resultNs = std::min(resultNs, minResultNs);
102 stream.close();
103 std::cout << ".";
104 std::cout.flush();
105 }
106
107 std::cout << std::endl;
108 }
109 else
110 {
111 //
112 // This will probably run on a machine doing other things. Run it some
113 // relatively large number of times and try to find a minimum, which
114 // will hopefully represent a time when it runs free of interference.
115 //
116 for (uint32_t i = 0; i < iter; ++i)
117 {
118 FILE* file = fopen("filetest", "w");
119
120 auto start = std::chrono::steady_clock::now();
121 PerfFile(file, n, buffer, 1024);
122 auto end = std::chrono::steady_clock::now();
123 auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
124 resultNs = std::min(resultNs, minResultNs);
125 fclose(file);
126 file = nullptr;
127 std::cout << ".";
128 std::cout.flush();
129 }
130 std::cout << std::endl;
131 }
132
133 std::cout << argv[0] << ": " << minResultNs.count() << "ns" << std::endl;
134
135 return 0;
136}
Parse command-line arguments.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
void PerfFile(FILE *file, uint32_t n, const char *buffer, uint32_t size)
Check the performance of writing to file.
Definition perf-io.cc:26
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:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.