Implement time functions in tests and examples with std::chrono library
This commit is contained in:
committed by
Tommaso Pecorella
parent
a4d174f8bf
commit
ac680dd20c
@@ -38,10 +38,8 @@
|
||||
* to make very large simulations.
|
||||
*/
|
||||
|
||||
// for timing functions
|
||||
#include <cstdlib>
|
||||
#include <sys/time.h>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
|
||||
#include "ns3/core-module.h"
|
||||
#include "ns3/internet-module.h"
|
||||
@@ -55,11 +53,6 @@
|
||||
|
||||
using namespace ns3;
|
||||
|
||||
typedef struct timeval TIMER_TYPE;
|
||||
#define TIMER_NOW(_t) gettimeofday (&_t,NULL);
|
||||
#define TIMER_SECONDS(_t) ((double)(_t).tv_sec + (_t).tv_usec*1e-6)
|
||||
#define TIMER_DIFF(_t1, _t2) (TIMER_SECONDS (_t1)-TIMER_SECONDS (_t2))
|
||||
|
||||
NS_LOG_COMPONENT_DEFINE ("CampusNetworkModel");
|
||||
|
||||
void Progress ()
|
||||
@@ -157,8 +150,8 @@ private:
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
TIMER_TYPE t0, t1, t2;
|
||||
TIMER_NOW (t0);
|
||||
auto t0 = std::chrono::steady_clock::now ();
|
||||
|
||||
std::cout << " ==== DARPA NMS CAMPUS NETWORK SIMULATION ====" << std::endl;
|
||||
// LogComponentEnable ("OnOffApplication", LOG_LEVEL_INFO);
|
||||
|
||||
@@ -687,8 +680,7 @@ main (int argc, char *argv[])
|
||||
}
|
||||
|
||||
std::cout << "Created " << NodeList::GetNNodes () << " nodes." << std::endl;
|
||||
TIMER_TYPE routingStart;
|
||||
TIMER_NOW (routingStart);
|
||||
auto routingStart = std::chrono::steady_clock::now ();
|
||||
|
||||
if (nix)
|
||||
{
|
||||
@@ -702,25 +694,28 @@ main (int argc, char *argv[])
|
||||
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
|
||||
}
|
||||
|
||||
TIMER_TYPE routingEnd;
|
||||
TIMER_NOW (routingEnd);
|
||||
std::cout << "Routing tables population took "
|
||||
<< TIMER_DIFF (routingEnd, routingStart) << std::endl;
|
||||
auto routingEnd = std::chrono::steady_clock::now ();
|
||||
std::cout << "Routing tables population took "
|
||||
<< std::chrono::duration_cast<std::chrono::milliseconds> (routingEnd - routingStart).count () << "ms"
|
||||
<< std::endl;
|
||||
|
||||
Simulator::ScheduleNow (Progress);
|
||||
std::cout << "Running simulator..." << std::endl;
|
||||
TIMER_NOW (t1);
|
||||
auto t1 = std::chrono::steady_clock::now ();
|
||||
Simulator::Stop (Seconds (100.0));
|
||||
Simulator::Run ();
|
||||
TIMER_NOW (t2);
|
||||
auto t2 = std::chrono::steady_clock::now ();
|
||||
std::cout << "Simulator finished." << std::endl;
|
||||
Simulator::Destroy ();
|
||||
|
||||
double d1 = TIMER_DIFF (t1, t0), d2 = TIMER_DIFF (t2, t1);
|
||||
std::cout << "-----" << std::endl << "Runtime Stats:" << std::endl;
|
||||
std::cout << "Simulator init time: " << d1 << std::endl;
|
||||
std::cout << "Simulator run time: " << d2 << std::endl;
|
||||
std::cout << "Total elapsed time: " << d1+d2 << std::endl;
|
||||
auto d1 = std::chrono::duration_cast<std::chrono::seconds>(t1 - t0);
|
||||
auto d2 = std::chrono::duration_cast<std::chrono::seconds>(t2 - t1);
|
||||
|
||||
std::cout << "-----" << std::endl
|
||||
<< "Runtime Stats:" << std::endl;
|
||||
std::cout << "Simulator init time: " << d1.count () << "s" << std::endl;
|
||||
std::cout << "Simulator run time: " << d2.count () << "s" << std::endl;
|
||||
std::cout << "Total elapsed time: " << (d1 + d2).count () << "s" << std::endl;
|
||||
|
||||
delete[] nodes_netLR;
|
||||
return 0;
|
||||
|
||||
@@ -14,8 +14,7 @@
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <ctime>
|
||||
#include <sys/time.h>
|
||||
#include <chrono>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -24,32 +23,9 @@
|
||||
#include <fstream>
|
||||
|
||||
#include "ns3/core-module.h"
|
||||
#include "ns3/abort.h"
|
||||
|
||||
using namespace ns3;
|
||||
|
||||
/// Microseconds to nanoseconds conversion factor.
|
||||
static const uint64_t US_PER_NS = (uint64_t)1000;
|
||||
/// Nanoseconds to seconds conversion factor.
|
||||
static const uint64_t NS_PER_SEC = (uint64_t)1000000000;
|
||||
|
||||
/**
|
||||
* \ingroup system-tests-perf
|
||||
*
|
||||
* Get the system clock time in nanoseconds.
|
||||
*
|
||||
* \return the system clock time in nanoseconds.
|
||||
*/
|
||||
uint64_t
|
||||
GetRealtimeInNs (void)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday (&tv, NULL);
|
||||
|
||||
uint64_t nsResult = tv.tv_sec * NS_PER_SEC + tv.tv_usec * US_PER_NS;
|
||||
return nsResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* \ingroup system-tests-perf
|
||||
*
|
||||
@@ -85,7 +61,6 @@ PerfFile (FILE *file, uint32_t n, const char *buffer, uint32_t size)
|
||||
void
|
||||
PerfStream (std::ostream &stream, uint32_t n, const char *buffer, uint32_t size)
|
||||
{
|
||||
|
||||
for (uint32_t i = 0; i < n; ++i)
|
||||
{
|
||||
stream.write (buffer, size);
|
||||
@@ -99,7 +74,6 @@ main (int argc, char *argv[])
|
||||
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);
|
||||
@@ -108,7 +82,8 @@ main (int argc, char *argv[])
|
||||
cmd.AddValue ("binmode", "Select binary mode for the C++ I/O benchmark (defaults to true)", binmode);
|
||||
cmd.Parse (argc, argv);
|
||||
|
||||
uint64_t result = std::numeric_limits<uint64_t>::max ();
|
||||
auto minResultNs = std::chrono::duration_cast<std::chrono::nanoseconds> (
|
||||
std::chrono::nanoseconds::max ());
|
||||
|
||||
char buffer[1024];
|
||||
|
||||
@@ -131,13 +106,15 @@ main (int argc, char *argv[])
|
||||
stream.open ("streamtest", std::ios_base::out);
|
||||
}
|
||||
|
||||
uint64_t start = GetRealtimeInNs ();
|
||||
auto start = std::chrono::steady_clock::now ();
|
||||
PerfStream (stream, n, buffer, 1024);
|
||||
uint64_t et = GetRealtimeInNs () - start;
|
||||
result = std::min (result, et);
|
||||
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 ();
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
else
|
||||
@@ -151,15 +128,19 @@ main (int argc, char *argv[])
|
||||
{
|
||||
FILE *file = fopen ("filetest", "w");
|
||||
|
||||
uint64_t start = GetRealtimeInNs ();
|
||||
auto start = std::chrono::steady_clock::now ();
|
||||
PerfFile (file, n, buffer, 1024);
|
||||
uint64_t et = GetRealtimeInNs () - start;
|
||||
result = std::min (result, et);
|
||||
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 = 0;
|
||||
std::cout << "."; std::cout.flush ();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << argv[0] << ": " << result << "ns" << std::endl;
|
||||
|
||||
std::cout << argv[0] << ": " << minResultNs.count () << "ns" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user