record_emission.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 record_emission.cpp
  9. * \author Andrey Semashev
  10. * \date 22.03.2009
  11. *
  12. * \brief This code measures performance of log record emission
  13. */
  14. // #define BOOST_LOG_USE_CHAR
  15. // #define BOOST_ALL_DYN_LINK 1
  16. // #define BOOST_LOG_DYN_LINK 1
  17. #define BOOST_NO_DYN_LINK 1
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <boost/ref.hpp>
  21. #include <boost/bind.hpp>
  22. #include <boost/smart_ptr/shared_ptr.hpp>
  23. #include <boost/smart_ptr/make_shared_object.hpp>
  24. #include <boost/date_time/microsec_time_clock.hpp>
  25. #include <boost/date_time/posix_time/posix_time_types.hpp>
  26. #include <boost/thread/thread.hpp>
  27. #include <boost/thread/barrier.hpp>
  28. #include <boost/log/core.hpp>
  29. #include <boost/log/common.hpp>
  30. #include <boost/log/attributes.hpp>
  31. #include <boost/log/sinks.hpp>
  32. #include <boost/log/sinks/basic_sink_backend.hpp>
  33. #include <boost/log/sources/logger.hpp>
  34. #include <boost/log/expressions.hpp>
  35. #include <boost/log/attributes/scoped_attribute.hpp>
  36. enum config
  37. {
  38. RECORD_COUNT = 20000000,
  39. THREAD_COUNT = 8,
  40. SINK_COUNT = 3
  41. };
  42. namespace logging = boost::log;
  43. namespace expr = boost::log::expressions;
  44. namespace sinks = boost::log::sinks;
  45. namespace attrs = boost::log::attributes;
  46. namespace src = boost::log::sources;
  47. namespace keywords = boost::log::keywords;
  48. enum severity_level
  49. {
  50. normal,
  51. warning,
  52. error
  53. };
  54. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  55. namespace {
  56. //! A fake sink backend that receives log records
  57. class fake_backend :
  58. public sinks::basic_sink_backend< sinks::concurrent_feeding >
  59. {
  60. public:
  61. void consume(logging::record_view const& rec)
  62. {
  63. }
  64. };
  65. } // namespace
  66. void test(unsigned int record_count, boost::barrier& bar)
  67. {
  68. BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
  69. src::severity_logger< severity_level > slg;
  70. // src::logger lg;
  71. bar.wait();
  72. for (unsigned int i = 0; i < record_count; ++i)
  73. {
  74. BOOST_LOG_SEV(slg, warning) << "Test record";
  75. // BOOST_LOG(lg) << "Test record";
  76. }
  77. }
  78. int main(int argc, char* argv[])
  79. {
  80. std::cout << "Test config: " << THREAD_COUNT << " threads, " << SINK_COUNT << " sinks, " << RECORD_COUNT << " records" << std::endl;
  81. //__debugbreak();
  82. // typedef sinks::unlocked_sink< fake_backend > fake_sink;
  83. // typedef sinks::synchronous_sink< fake_backend > fake_sink;
  84. typedef sinks::asynchronous_sink< fake_backend > fake_sink;
  85. for (unsigned int i = 0; i < SINK_COUNT; ++i)
  86. logging::core::get()->add_sink(boost::make_shared< fake_sink >());
  87. logging::core::get()->add_global_attribute("LineID", attrs::counter< unsigned int >(1));
  88. logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
  89. logging::core::get()->add_global_attribute("Scope", attrs::named_scope());
  90. // logging::core::get()->set_filter(severity > normal); // all records pass the filter
  91. // logging::core::get()->set_filter(severity > error); // all records don't pass the filter
  92. // logging::core::get()->set_filter(severity > error); // all records don't pass the filter
  93. const unsigned int record_count = RECORD_COUNT / THREAD_COUNT;
  94. boost::barrier bar(THREAD_COUNT);
  95. boost::thread_group threads;
  96. for (unsigned int i = 1; i < THREAD_COUNT; ++i)
  97. threads.create_thread(boost::bind(&test, record_count, boost::ref(bar)));
  98. boost::posix_time::ptime start = boost::date_time::microsec_clock< boost::posix_time::ptime >::universal_time(), end;
  99. test(record_count, bar);
  100. if (THREAD_COUNT > 1)
  101. threads.join_all();
  102. end = boost::date_time::microsec_clock< boost::posix_time::ptime >::universal_time();
  103. unsigned long long duration = (end - start).total_microseconds();
  104. std::cout << "Test duration: " << duration << " us ("
  105. << std::fixed << std::setprecision(3) << static_cast< double >(RECORD_COUNT) / (static_cast< double >(duration) / 1000000.0)
  106. << " records per second)" << std::endl;
  107. return 0;
  108. }