g3log/test_performance/main_threaded_mean.cpp
KjellKod 87a095e384 Renamed directories for a cleaner project structure
--HG--
rename : g2log/Build.cmake => Build.cmake
rename : g2log/CMakeLists.txt => CMakeLists.txt
rename : g2log/CPackLists.txt => CPackLists.txt
rename : g2log/Dynamic.cmake => Dynamic.cmake
rename : g2log/example/Example.cmake => example/Example.cmake
rename : g2log/example/main_contract.cpp => example/main_contract.cpp
rename : g2log/example/main_sigsegv.cpp => example/main_sigsegv.cpp
rename : g2log/src/active.hpp => src/active.hpp
rename : g2log/src/crashhandler.hpp => src/crashhandler.hpp
rename : g2log/src/crashhandler_unix.cpp => src/crashhandler_unix.cpp
rename : g2log/src/crashhandler_win.cpp => src/crashhandler_win.cpp
rename : g2log/src/g2filesink.cpp => src/g2filesink.cpp
rename : g2log/src/g2filesink.hpp => src/g2filesink.hpp
rename : g2log/src/g2filesinkhelper.ipp => src/g2filesinkhelper.ipp
rename : g2log/src/g2future.hpp => src/g2future.hpp
rename : g2log/src/g2log.cpp => src/g2log.cpp
rename : g2log/src/g2log.hpp => src/g2log.hpp
rename : g2log/src/g2loglevels.cpp => src/g2loglevels.cpp
rename : g2log/src/g2loglevels.hpp => src/g2loglevels.hpp
rename : g2log/src/g2logmessage.cpp => src/g2logmessage.cpp
rename : g2log/src/g2logmessage.hpp => src/g2logmessage.hpp
rename : g2log/src/g2logmessagecapture.cpp => src/g2logmessagecapture.cpp
rename : g2log/src/g2logmessagecapture.hpp => src/g2logmessagecapture.hpp
rename : g2log/src/g2logworker.cpp => src/g2logworker.cpp
rename : g2log/src/g2logworker.hpp => src/g2logworker.hpp
rename : g2log/src/g2moveoncopy.hpp => src/g2moveoncopy.hpp
rename : g2log/src/g2sink.hpp => src/g2sink.hpp
rename : g2log/src/g2sinkhandle.hpp => src/g2sinkhandle.hpp
rename : g2log/src/g2sinkwrapper.hpp => src/g2sinkwrapper.hpp
rename : g2log/src/g2time.cpp => src/g2time.cpp
rename : g2log/src/g2time.hpp => src/g2time.hpp
rename : g2log/src/shared_queue.hpp => src/shared_queue.hpp
rename : g2log/src/std2_make_unique.hpp => src/std2_make_unique.hpp
rename : g2log/src/stlpatch_future.hpp => src/stlpatch_future.hpp
rename : g2log/test_performance/Performance.cmake => test_performance/Performance.cmake
rename : g2log/test_performance/main_threaded_mean.cpp => test_performance/main_threaded_mean.cpp
rename : g2log/test_performance/main_threaded_worst.cpp => test_performance/main_threaded_worst.cpp
rename : g2log/test_performance/performance.h => test_performance/performance.h
rename : g2log/test_unit/Test.cmake => test_unit/Test.cmake
rename : g2log/test_unit/test_concept_sink.cpp => test_unit/test_concept_sink.cpp
rename : g2log/test_unit/test_configuration.cpp => test_unit/test_configuration.cpp
rename : g2log/test_unit/test_filechange.cpp => test_unit/test_filechange.cpp
rename : g2log/test_unit/test_io.cpp => test_unit/test_io.cpp
rename : g2log/test_unit/test_linux_dynamic_loaded_sharedlib.cpp => test_unit/test_linux_dynamic_loaded_sharedlib.cpp
rename : g2log/test_unit/test_sink.cpp => test_unit/test_sink.cpp
rename : g2log/test_unit/tester_sharedlib.cpp => test_unit/tester_sharedlib.cpp
rename : g2log/test_unit/tester_sharedlib.h => test_unit/tester_sharedlib.h
rename : g2log/test_unit/testing_helpers.cpp => test_unit/testing_helpers.cpp
rename : g2log/test_unit/testing_helpers.h => test_unit/testing_helpers.h
2014-10-03 01:20:33 -06:00

110 lines
4.5 KiB
C++

/** ==========================================================================
* 2011 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
* with no warranties. This code is yours to share, use and modify with no
* strings attached and no restrictions or obligations.
*
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
* ============================================================================*/
// through CMakeLists.txt #define of GOOGLE_GLOG_PERFORMANCE and G2LOG_PERFORMANCE
#include "performance.h"
#include <thread>
#include <iostream>
#if defined(G2LOG_PERFORMANCE)
const std::string title = "G2LOG";
#elif defined(GOOGLE_GLOG_PERFORMANCE)
const std::string title = "GOOGLE__GLOG";
#else
#error G2LOG_PERFORMANCE or GOOGLE_GLOG_PERFORMANCE was not defined
#endif
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
const std::string g_path = "./";
#else
const std::string g_path = "/tmp/";
#endif
using namespace g2_test;
int main(int argc, char** argv)
{
#ifdef G2_DYNAMIC_LOGGING
std::cerr << "G2_DYNAMIC_LOGGING is enabled" << std::endl;
#else
std::cerr << "G2_DYNAMIC_LOGGING is DISABLED" << std::endl;
#endif
size_t number_of_threads =0;
if(argc == 2)
{
number_of_threads = atoi(argv[1]);
}
if(argc != 2 || number_of_threads == 0)
{
std::cerr << "USAGE is: " << argv[0] << " number_threads" << std::endl;
return 1;
}
std::ostringstream thread_count_oss;
thread_count_oss << number_of_threads;
const std::string g_prefix_log_name = title + "-performance-" + thread_count_oss.str() + "threads-MEAN_LOG";
const std::string g_measurement_dump= g_path + g_prefix_log_name + "_RESULT.txt";
std::ostringstream oss;
const uint64_t us_to_s = 1000000;
oss << "\n\n" << title << " performance " << number_of_threads << " threads MEAN times\n";
oss << "Each thread running #: " << g_loop << " * " << g_iterations << " iterations of log entries" << std::endl; // worst mean case is about 10us per log entry
const uint64_t xtra_margin = 2;
oss << "*** It can take som time. Please wait: Approximate wait time on MY PC was: " <<number_of_threads* (uint64_t) (g_iterations * 10 * xtra_margin / us_to_s ) << " seconds" << std::endl;
writeTextToFile(g_measurement_dump, oss.str(), kAppend);
oss.str(""); // clear the stream
#if defined(G2LOG_PERFORMANCE)
auto logger_n_handle = g2::LogWorker::createWithDefaultLogger(g_prefix_log_name, g_path);
g2::initializeLogging(logger_n_handle.worker.get());
#elif defined(GOOGLE_GLOG_PERFORMANCE)
google::InitGoogleLogging(argv[0]);
#endif
auto start_time = std::chrono::high_resolution_clock::now();
std::thread* threads = new std::thread[number_of_threads];
// kiss: just loop, create threads, store them then join
// could probably do this more elegant with lambdas
for(size_t idx = 0; idx < number_of_threads; ++idx)
{
std::ostringstream count;
count << idx+1;
std::string thread_name = title + "_T" + count.str();
std::cout << "Creating thread: " << thread_name << std::endl;
threads[idx] = std::thread(doLogWrites,thread_name);
}
for(size_t idx = 0; idx < number_of_threads; ++idx)
{
threads[idx].join();
}
auto application_end_time = std::chrono::high_resolution_clock::now();
delete [] threads;
#if defined(G2LOG_PERFORMANCE)
logger_n_handle.worker.reset(); // will flush anything in the queue to file
#elif defined(GOOGLE_GLOG_PERFORMANCE)
google::ShutdownGoogleLogging();
#endif
auto worker_end_time = std::chrono::high_resolution_clock::now();
uint64_t application_time_us = std::chrono::duration_cast<microsecond>(application_end_time - start_time).count();
uint64_t total_time_us = std::chrono::duration_cast<microsecond>(worker_end_time - start_time).count();
oss << "\n" << number_of_threads << "*" << g_iterations << " log entries took: [" << total_time_us / 1000000 << " s] to write to disk"<< std::endl;
oss << "[Application(" << number_of_threads << "):\t\t:" << application_time_us/1000 << " ms]" << std::endl;
oss << "[Background thread to finish\t:" << total_time_us/uint64_t(1000 )<< " ms]" << std::endl;
oss << "\nAverage time per log entry:" << std::endl;
oss << "[Application: " << application_time_us/(number_of_threads*g_iterations) << " us]" << std::endl;
oss << "[Background+Application: " << total_time_us/(number_of_threads*g_iterations) << " us]" << std::endl;
writeTextToFile(g_measurement_dump,oss.str(), kAppend);
std::cout << "Result can be found at:" << g_measurement_dump << std::endl;
return 0;
}