Files
unison/utils/perf/perf-io.cc

137 lines
3.8 KiB
C++
Raw Normal View History

2010-02-08 23:06:31 -08:00
/*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
2010-02-08 23:06:31 -08:00
*/
2022-10-07 20:08:35 +00:00
#include "ns3/core-module.h"
2010-02-08 23:06:31 -08:00
2022-10-07 20:08:35 +00:00
#include <chrono>
#include <cstdio>
#include <cstdlib>
2010-02-08 23:06:31 -08:00
#include <fstream>
2022-10-07 20:08:35 +00:00
#include <iostream>
2010-02-08 23:06:31 -08:00
using namespace ns3;
2022-01-13 17:07:08 -06:00
/**
2024-11-08 18:05:46 +00:00
* @ingroup system-tests-perf
*
2022-01-13 17:07:08 -06:00
* Check the performance of writing to file.
*
2024-11-08 18:05:46 +00:00
* @param file The file to write to.
* @param n The number of writes to perform.
* @param buffer The buffer to write.
* @param size The buffer size.
2022-01-13 17:07:08 -06:00
*/
2010-02-08 23:06:31 -08:00
void
2022-10-07 20:08:35 +00:00
PerfFile(FILE* file, uint32_t n, const char* buffer, uint32_t size)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
for (uint32_t i = 0; i < n; ++i)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
if (std::fwrite(buffer, 1, size, file) != size)
2010-04-19 10:10:49 -07:00
{
2022-10-07 20:08:35 +00:00
NS_ABORT_MSG("PerfFile(): fwrite error");
2010-04-19 10:10:49 -07:00
}
2010-02-08 23:06:31 -08:00
}
}
2022-01-13 17:07:08 -06:00
/**
2024-11-08 18:05:46 +00:00
* @ingroup system-tests-perf
*
2022-01-13 17:07:08 -06:00
* Check the performance of writing to an output stream.
*
2024-11-08 18:05:46 +00:00
* @param stream The output stream to write to.
* @param n The number of writes to perform.
* @param buffer The buffer to write.
* @param size The buffer size.
2022-01-13 17:07:08 -06:00
*/
2010-02-08 23:06:31 -08:00
void
2022-10-07 20:08:35 +00:00
PerfStream(std::ostream& stream, uint32_t n, const char* buffer, uint32_t size)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
for (uint32_t i = 0; i < n; ++i)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
stream.write(buffer, size);
2010-02-08 23:06:31 -08:00
}
}
int
2022-10-07 20:08:35 +00:00
main(int argc, char* argv[])
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
uint32_t n = 100000;
uint32_t iter = 50;
bool doStream = false;
bool binmode = true;
CommandLine cmd(__FILE__);
cmd.AddValue("n", "How many times to write (defaults to 100000", n);
cmd.AddValue("iter", "How many times to run the test looking for a min (defaults to 50)", iter);
cmd.AddValue("doStream", "Run the C++ I/O benchmark otherwise the C I/O ", doStream);
cmd.AddValue("binmode",
"Select binary mode for the C++ I/O benchmark (defaults to true)",
binmode);
cmd.Parse(argc, argv);
auto minResultNs =
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::nanoseconds::max());
char buffer[1024];
if (doStream)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
//
// This will probably run on a machine doing other things. Run it some
// relatively large number of times and try to find a minimum, which
// will hopefully represent a time when it runs free of interference.
//
for (uint32_t i = 0; i < iter; ++i)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
std::ofstream stream;
if (binmode)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
stream.open("streamtest", std::ios_base::binary | std::ios_base::out);
2010-02-08 23:06:31 -08:00
}
2022-10-07 20:08:35 +00:00
else
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
stream.open("streamtest", std::ios_base::out);
2010-02-08 23:06:31 -08:00
}
2022-10-07 20:08:35 +00:00
auto start = std::chrono::steady_clock::now();
PerfStream(stream, n, buffer, 1024);
auto end = std::chrono::steady_clock::now();
auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
resultNs = std::min(resultNs, minResultNs);
stream.close();
std::cout << ".";
std::cout.flush();
2010-02-08 23:06:31 -08:00
}
2022-10-07 20:08:35 +00:00
std::cout << std::endl;
2010-02-08 23:06:31 -08:00
}
2022-10-07 20:08:35 +00:00
else
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
//
// This will probably run on a machine doing other things. Run it some
// relatively large number of times and try to find a minimum, which
// will hopefully represent a time when it runs free of interference.
//
for (uint32_t i = 0; i < iter; ++i)
2010-02-08 23:06:31 -08:00
{
2022-10-07 20:08:35 +00:00
FILE* file = fopen("filetest", "w");
auto start = std::chrono::steady_clock::now();
PerfFile(file, n, buffer, 1024);
auto end = std::chrono::steady_clock::now();
auto resultNs = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
resultNs = std::min(resultNs, minResultNs);
fclose(file);
file = nullptr;
std::cout << ".";
std::cout.flush();
2010-02-08 23:06:31 -08:00
}
2022-10-07 20:08:35 +00:00
std::cout << std::endl;
2010-02-08 23:06:31 -08:00
}
2022-10-07 20:08:35 +00:00
std::cout << argv[0] << ": " << minResultNs.count() << "ns" << std::endl;
2022-10-07 20:08:35 +00:00
return 0;
2010-02-08 23:06:31 -08:00
}