diff --git a/bench-comparison/Makefile b/bench-comparison/Makefile new file mode 100644 index 00000000..529b9598 --- /dev/null +++ b/bench-comparison/Makefile @@ -0,0 +1,31 @@ +CXX = g++ +CXXFLAGS = -march=native -Wall -pthread -I../include --std=c++11 + +CXX_RELEASE_FLAGS = -O3 -flto + + +all: spdlog-bench spdlog-bench-mt boost-bench boost-bench-mt + +spdlog-bench: spdlog-bench.cpp + $(CXX) spdlog-bench.cpp -o spdlog-bench $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +spdlog-bench-mt: spdlog-bench-mt.cpp + $(CXX) spdlog-bench-mt.cpp -o spdlog-bench-mt $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + +BOOST_FLAGS = -DBOOST_LOG_DYN_LINK -I/home/gabi/devel/boost_1_56_0/ -L/home/gabi/devel/boost_1_56_0/stage/lib -lboost_log -lboost_log_setup -lboost_filesystem -lboost_system -lboost_thread -lboost_regex -lboost_date_time -lboost_chrono + +boost-bench: boost-bench.cpp + $(CXX) boost-bench.cpp -o boost-bench $(CXXFLAGS) $(BOOST_FLAGS) $(CXX_RELEASE_FLAGS) + +boost-bench-mt: boost-bench-mt.cpp + $(CXX) boost-bench-mt.cpp -o boost-bench-mt $(CXXFLAGS) $(BOOST_FLAGS) $(CXX_RELEASE_FLAGS) + + +clean: + rm -f *.o logs/*.txt spdlog-bench spdlog-bench-mt boost-bench boost-bench-mt + + +rebuild: clean all + + + diff --git a/bench-comparison/boost-bench-mt.cpp b/bench-comparison/boost-bench-mt.cpp new file mode 100644 index 00000000..16515ae6 --- /dev/null +++ b/bench-comparison/boost-bench-mt.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace logging = boost::log; +namespace src = boost::log::sources; +namespace sinks = boost::log::sinks; +namespace keywords = boost::log::keywords; + +void init() +{ + logging::add_file_log + ( + keywords::file_name = "logs/boost-sample_%N.log", /*< file name pattern >*/ + keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/ + keywords::auto_flush = false, + keywords::format = "[%TimeStamp%]: %Message%" + ); + + logging::core::get()->set_filter + ( + logging::trivial::severity >= logging::trivial::info + ); +} + + + +using namespace std; + +int main(int, char*[]) +{ + int thread_count = 10; + int howmany = 1000000; + + init(); + logging::add_common_attributes(); + + + using namespace logging::trivial; + + src::severity_logger_mt< severity_level > lg; + + std::atomic msg_counter {0}; + vector threads; + + for (int t = 0; t < thread_count; ++t) + { + threads.push_back(std::thread([&]() + { + while (true) + { + int counter = ++msg_counter; + if (counter > howmany) break; + BOOST_LOG_SEV(lg, info) << "Boost logger message #" << counter; + } + })); + } + + + for(auto &t:threads) + { + t.join(); + }; + + + return 0; +} diff --git a/bench-comparison/boost-bench.cpp b/bench-comparison/boost-bench.cpp new file mode 100644 index 00000000..2734b63c --- /dev/null +++ b/bench-comparison/boost-bench.cpp @@ -0,0 +1,44 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace logging = boost::log; +namespace src = boost::log::sources; +namespace sinks = boost::log::sinks; +namespace keywords = boost::log::keywords; + +void init() +{ + logging::add_file_log + ( + keywords::file_name = "logs/boost-sample_%N.log", /*< file name pattern >*/ + keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/ + keywords::auto_flush = false, + keywords::format = "[%TimeStamp%]: %Message%" + ); + + logging::core::get()->set_filter + ( + logging::trivial::severity >= logging::trivial::info + ); +} + + +int main(int, char*[]) +{ + init(); + logging::add_common_attributes(); + + using namespace logging::trivial; + src::severity_logger_mt< severity_level > lg; + for(int i = 0 ; i < 1000000; ++i) + BOOST_LOG_SEV(lg, info) << "Boost logger message #" << i; + + return 0; +} diff --git a/bench-comparison/logs/.gitignore b/bench-comparison/logs/.gitignore new file mode 100644 index 00000000..5e7d2734 --- /dev/null +++ b/bench-comparison/logs/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/bench-comparison/spdlog-bench-mt.cpp b/bench-comparison/spdlog-bench-mt.cpp new file mode 100644 index 00000000..e5da8776 --- /dev/null +++ b/bench-comparison/spdlog-bench-mt.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +#include "spdlog/spdlog.h" + + +using namespace std; + +int main(int, char*[]) +{ + int thread_count = 10; + int howmany = 1000000; + + namespace spd = spdlog; + ///Create a file rotating logger with 5mb size max and 3 rotated files + auto logger = spd::rotating_logger_mt("file_logger", "logs/spd-sample", 10 *1024 * 1024 , 5); + + logger->set_pattern("[%Y-%b-%d %T.%e]: %v"); + + std::atomic msg_counter {0}; + vector threads; + + for (int t = 0; t < thread_count; ++t) + { + threads.push_back(std::thread([&]() + { + while (true) + { + int counter = ++msg_counter; + if (counter > howmany) break; + logger->info() << "spdlog logger message #" << counter; + } + })); + } + + + for(auto &t:threads) + { + t.join(); + }; + + + + return 0; +} diff --git a/bench-comparison/spdlog-bench.cpp b/bench-comparison/spdlog-bench.cpp new file mode 100644 index 00000000..e7be6fd2 --- /dev/null +++ b/bench-comparison/spdlog-bench.cpp @@ -0,0 +1,16 @@ + +#include "spdlog/spdlog.h" + + +int main(int, char*[]) +{ + namespace spd = spdlog; + ///Create a file rotating logger with 5mb size max and 3 rotated files + auto logger = spd::rotating_logger_mt("file_logger", "logs/spd-sample", 10 *1024 * 1024 , 5); + + logger->set_pattern("[%Y-%b-%d %T.%e]: %v"); + for(int i = 0 ; i < 1000000; ++i) + logger->info() << "spdlogger message #" << i; + + return 0; +}