From 812d7c1bf00beaeffd43c9ccffb9262cb61a30e3 Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Mon, 8 Feb 2010 23:06:31 -0800 Subject: [PATCH] add io performance benchmark --- src/test/perf/perf-io.cc | 137 +++++++++++++++++++++++++++++++++++++ src/test/perf/perf-pcap.cc | 46 ++++++++----- src/test/perf/wscript | 3 + 3 files changed, 169 insertions(+), 17 deletions(-) create mode 100644 src/test/perf/perf-io.cc diff --git a/src/test/perf/perf-io.cc b/src/test/perf/perf-io.cc new file mode 100644 index 000000000..e5ff44a00 --- /dev/null +++ b/src/test/perf/perf-io.cc @@ -0,0 +1,137 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include + +#include +#include + +#include +#include + +#include "ns3/simulator-module.h" +#include "ns3/node-module.h" +#include "ns3/core-module.h" +#include "ns3/helper-module.h" + +using namespace ns3; +using namespace std; + +static const uint64_t US_PER_NS = (uint64_t)1000; +static const uint64_t US_PER_SEC = (uint64_t)1000000; +static const uint64_t NS_PER_SEC = (uint64_t)1000000000; + +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; +} + +void +PerfFile (FILE *file, uint32_t n, const char *buffer, uint32_t size) +{ + for (uint32_t i = 0; i < n; ++i) + { + fwrite (buffer, 1, size, file); + } +} + +void +PerfStream (ostream &stream, uint32_t n, const char *buffer, uint32_t size) +{ + + for (uint32_t i = 0; i < n; ++i) + { + stream.write (buffer, size); + } +} + +int +main (int argc, char *argv[]) +{ + uint32_t n = 100000; + uint32_t iter = 50; + bool doStream = false; + bool binmode = true; + + + CommandLine cmd; + 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); + + uint64_t result = std::numeric_limits::max (); + + char buffer[1024]; + + if (doStream) + { + // + // 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) + { + ofstream stream; + if (binmode) + { + stream.open ("streamtest", std::ios_base::binary | std::ios_base::out); + } + else + { + stream.open ("streamtest", std::ios_base::out); + } + + uint64_t start = GetRealtimeInNs (); + PerfStream (stream, n, buffer, 1024); + uint64_t et = GetRealtimeInNs () - start; + result = min (result, et); + stream.close (); + cout << "."; std::cout.flush (); + } + cout << std::endl; + } + else + { + // + // 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) + { + FILE *file = fopen ("filetest", "w"); + + uint64_t start = GetRealtimeInNs (); + PerfFile (file, n, buffer, 1024); + uint64_t et = GetRealtimeInNs () - start; + result = std::min (result, et); + fclose (file); + file = 0; + std::cout << "."; std::cout.flush (); + } + std::cout << std::endl; + } + std::cout << argv[0] << ": " << result << "ns" << std::endl; +} diff --git a/src/test/perf/perf-pcap.cc b/src/test/perf/perf-pcap.cc index 4c96c370b..0623411d1 100644 --- a/src/test/perf/perf-pcap.cc +++ b/src/test/perf/perf-pcap.cc @@ -14,6 +14,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include +#include #include #include #include @@ -26,7 +28,19 @@ using namespace ns3; -//NS_LOG_COMPONENT_DEFINE ("PerfPcap"); +static const uint64_t US_PER_NS = (uint64_t)1000; +static const uint64_t US_PER_SEC = (uint64_t)1000000; +static const uint64_t NS_PER_SEC = (uint64_t)1000000000; + +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; +} bool g_passheader = true; bool g_addheader = true; @@ -94,7 +108,7 @@ main (int argc, char *argv[]) cmd.AddValue ("addheader", "Add a header to the traces to trigger a deep copy", g_addheader); cmd.AddValue ("passheader", "Pass header as reference instead of adding it", g_passheader); cmd.AddValue ("n", "How many packets to write (defaults to 100000", n); - cmd.AddValue ("iter", "How many times to run the test looking for a min (defaults to 20)", iter); + cmd.AddValue ("iter", "How many times to run the test looking for a min (defaults to 50)", iter); cmd.AddValue ("oldstyle", "run the old style pcap writer stuff if true", oldstyle); cmd.Parse (argc, argv); @@ -102,10 +116,6 @@ main (int argc, char *argv[]) if (oldstyle) { - PcapWriter pcapWriter; - pcapWriter.Open ("perf-pcap.pcap"); - pcapWriter.WriteWifiRadiotapHeader (); - Ptr p = Create (1024); // @@ -115,10 +125,13 @@ main (int argc, char *argv[]) // for (uint32_t i = 0; i < iter; ++i) { - SystemWallClockMs ms; - ms.Start (); + PcapWriter pcapWriter; + pcapWriter.Open ("perf-pcap.pcap"); + pcapWriter.WriteWifiRadiotapHeader (); + + uint64_t start = GetRealtimeInNs (); PerfPcapOld (pcapWriter, p, n); - uint64_t et = ms.End (); + uint64_t et = GetRealtimeInNs () - start; result = std::min (result, et); std::cout << "."; std::cout.flush (); } @@ -126,10 +139,6 @@ main (int argc, char *argv[]) } else { - PcapHelper pcapHelper; - Ptr file = pcapHelper.CreateFile ("perf-pcap.pcap", "w", PcapHelper::DLT_IEEE802_11_RADIO); - Ptr p = Create (1024); - g_header.SetTsft (0); g_header.SetFrameFlags (RadiotapHeader::FRAME_FLAG_SHORT_PREAMBLE); g_header.SetRate (0); @@ -142,14 +151,17 @@ main (int argc, char *argv[]) // for (uint32_t i = 0; i < iter; ++i) { - SystemWallClockMs ms; - ms.Start (); + PcapHelper pcapHelper; + Ptr file = pcapHelper.CreateFile ("perf-pcap.pcap", "w", PcapHelper::DLT_IEEE802_11_RADIO); + Ptr p = Create (1024); + + uint64_t start = GetRealtimeInNs (); PerfPcapNew (file, p, n); - uint64_t et = ms.End (); + uint64_t et = GetRealtimeInNs () - start; result = std::min (result, et); std::cout << "."; std::cout.flush (); } std::cout << std::endl; } - std::cout << "perf-pcap: " << result << "ms" << std::endl; + std::cout << argv[0] << ": " << result << "ns" << std::endl; } diff --git a/src/test/perf/wscript b/src/test/perf/wscript index 91fef671b..b4a997ccc 100644 --- a/src/test/perf/wscript +++ b/src/test/perf/wscript @@ -11,4 +11,7 @@ def build(bld): obj = bld.create_ns3_program('perf-pcap', ['core', 'simulator', 'internet-stack']) obj.source = 'perf-pcap.cc' + obj = bld.create_ns3_program('perf-io', ['core']) + obj.source = 'perf-io.cc' +