dump.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file dump.cpp
  9. * \author Andrey Semashev
  10. * \date 05.05.2013
  11. *
  12. * \brief This code measures performance dumping binary data
  13. */
  14. #include <cstdlib>
  15. #include <iomanip>
  16. #include <string>
  17. #include <vector>
  18. #include <iostream>
  19. #include <algorithm>
  20. #include <boost/cstdint.hpp>
  21. #include <boost/date_time/microsec_time_clock.hpp>
  22. #include <boost/date_time/posix_time/posix_time_types.hpp>
  23. #include <boost/log/utility/formatting_ostream.hpp>
  24. #include <boost/log/utility/manipulators/dump.hpp>
  25. namespace logging = boost::log;
  26. const unsigned int base_loop_count = 10000;
  27. void test(std::size_t block_size)
  28. {
  29. std::cout << "Block size: " << block_size << " bytes.";
  30. std::vector< boost::uint8_t > data;
  31. data.resize(block_size);
  32. std::generate_n(data.begin(), block_size, &std::rand);
  33. std::string str;
  34. logging::formatting_ostream strm(str);
  35. const boost::uint8_t* const p = &data[0];
  36. boost::uint64_t data_processed = 0, duration = 0;
  37. boost::posix_time::ptime start, end;
  38. start = boost::date_time::microsec_clock< boost::posix_time::ptime >::universal_time();
  39. do
  40. {
  41. for (unsigned int i = 0; i < base_loop_count; ++i)
  42. {
  43. strm << logging::dump(p, block_size);
  44. str.clear();
  45. }
  46. end = boost::date_time::microsec_clock< boost::posix_time::ptime >::universal_time();
  47. data_processed += base_loop_count * block_size;
  48. duration = (end - start).total_microseconds();
  49. }
  50. while (duration < 2000000);
  51. std::cout << " Test duration: " << duration << " us ("
  52. << std::fixed << std::setprecision(3) << static_cast< double >(data_processed) / (static_cast< double >(duration) * (1048576.0 / 1000000.0))
  53. << " MiB per second)" << std::endl;
  54. }
  55. int main(int argc, char* argv[])
  56. {
  57. test(32);
  58. test(128);
  59. test(1024);
  60. test(16384);
  61. test(1048576);
  62. return 0;
  63. }