2011-11-22 00:04:02 +01:00
/** ==========================================================================
* 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 .
2015-07-16 09:55:23 +02:00
*
2014-07-03 23:42:19 +02:00
* For more information see g3log / LICENSE or refer refer to http : //unlicense.org
2011-11-22 00:04:02 +01:00
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2011-11-05 17:36:07 +01:00
2015-07-20 07:10:56 +02:00
// through CMakeLists.txt #define of GOOGLE_GLOG_PERFORMANCE and G3LOG_PERFORMANCE
2016-03-18 17:50:59 +01:00
# include <algorithm>
2023-12-01 00:17:45 +01:00
# include <iostream>
# include <thread>
# include "performance.h"
2011-11-05 17:36:07 +01:00
2015-07-20 07:10:56 +02:00
# if defined(G3LOG_PERFORMANCE)
const std : : string title = " G3LOG " ;
2011-11-05 17:36:07 +01:00
# elif defined(GOOGLE_GLOG_PERFORMANCE)
const std : : string title = " GOOGLE__GLOG " ;
# else
2015-07-20 07:10:56 +02:00
# error G3LOG_PERFORMANCE or GOOGLE_GLOG_PERFORMANCE was not defined
2011-11-05 17:36:07 +01:00
# endif
2011-11-17 22:46:38 +01:00
# if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
2015-07-16 09:55:23 +02:00
const std : : string g_path = " ./ " ;
2011-11-17 22:46:38 +01:00
# else
2015-07-16 09:55:23 +02:00
const std : : string g_path = " /tmp/ " ;
2011-11-17 22:46:38 +01:00
# endif
2015-07-20 07:10:56 +02:00
using namespace g3_test ;
2011-11-05 17:36:07 +01:00
2023-12-01 00:17:45 +01:00
int main ( int argc , char * * argv ) {
2015-07-20 07:10:56 +02:00
# ifdef G3_DYNAMIC_LOGGING
std : : cerr < < " G3_DYNAMIC_LOGGING is enabled " < < std : : endl ;
2015-07-16 09:55:23 +02:00
# else
2015-07-20 07:10:56 +02:00
std : : cerr < < " G3_DYNAMIC_LOGGING is DISABLED " < < std : : endl ;
2013-11-13 09:19:33 +01:00
# endif
2011-11-15 01:46:07 +01:00
2015-07-16 09:55:23 +02:00
size_t number_of_threads = 0 ;
2023-12-01 00:17:45 +01:00
if ( argc = = 2 ) {
2015-07-16 09:55:23 +02:00
number_of_threads = atoi ( argv [ 1 ] ) ;
}
2023-12-01 00:17:45 +01:00
if ( argc ! = 2 | | number_of_threads = = 0 ) {
2015-07-16 09:55:23 +02:00
std : : cerr < < " USAGE is: " < < argv [ 0 ] < < " number_threads " < < std : : endl ;
return 1 ;
}
std : : ostringstream thread_count_oss ;
thread_count_oss < < number_of_threads ;
2023-12-01 00:17:45 +01:00
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 " ;
2011-11-15 01:46:07 +01:00
2015-07-16 09:55:23 +02:00
std : : ostringstream oss ;
const uint64_t us_to_s = 1000000 ;
2023-12-01 00:17:45 +01:00
oss < < " \n \n "
< < title < < " performance " < < number_of_threads < < " threads MEAN times \n " ;
2015-07-16 09:55:23 +02:00
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 ;
2023-12-01 00:17:45 +01:00
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 ;
2015-07-16 09:55:23 +02:00
writeTextToFile ( g_measurement_dump , oss . str ( ) , kAppend ) ;
2023-12-01 00:17:45 +01:00
oss . str ( " " ) ; // clear the stream
2011-11-05 17:36:07 +01:00
2015-07-20 07:10:56 +02:00
# if defined(G3LOG_PERFORMANCE)
2015-08-19 18:08:41 +02:00
auto worker = g3 : : LogWorker : : createLogWorker ( ) ;
2023-12-01 00:17:45 +01:00
auto handle = worker - > addDefaultLogger ( g_prefix_log_name , g_path ) ;
2015-08-19 18:08:41 +02:00
g3 : : initializeLogging ( worker . get ( ) ) ;
2011-11-05 17:36:07 +01:00
# elif defined(GOOGLE_GLOG_PERFORMANCE)
2015-07-16 09:55:23 +02:00
google : : InitGoogleLogging ( argv [ 0 ] ) ;
2011-11-05 17:36:07 +01:00
# endif
2015-07-16 09:55:23 +02:00
auto start_time = std : : chrono : : high_resolution_clock : : now ( ) ;
2011-11-15 01:46:07 +01:00
2023-12-01 00:17:45 +01:00
std : : thread * threads = new std : : thread [ number_of_threads ] ;
2015-07-16 09:55:23 +02:00
// kiss: just loop, create threads, store them then join
// could probably do this more elegant with lambdas
2023-12-01 00:17:45 +01:00
for ( size_t idx = 0 ; idx < number_of_threads ; + + idx ) {
2015-07-16 09:55:23 +02:00
std : : ostringstream count ;
count < < idx + 1 ;
2023-12-01 00:17:45 +01:00
std : : string thread_name = title + " _T " + count . str ( ) ;
2015-07-16 09:55:23 +02:00
std : : cout < < " Creating thread: " < < thread_name < < std : : endl ;
threads [ idx ] = std : : thread ( doLogWrites , thread_name ) ;
}
2023-12-01 00:17:45 +01:00
for ( size_t idx = 0 ; idx < number_of_threads ; + + idx ) {
2015-07-16 09:55:23 +02:00
threads [ idx ] . join ( ) ;
}
auto application_end_time = std : : chrono : : high_resolution_clock : : now ( ) ;
2023-12-01 00:17:45 +01:00
delete [ ] threads ;
2011-11-15 01:46:07 +01:00
2015-07-20 07:10:56 +02:00
# if defined(G3LOG_PERFORMANCE)
2023-12-01 00:17:45 +01:00
worker . reset ( ) ; // will flush anything in the queue to file
2011-11-05 17:36:07 +01:00
# elif defined(GOOGLE_GLOG_PERFORMANCE)
2015-07-16 09:55:23 +02:00
google : : ShutdownGoogleLogging ( ) ;
2011-11-05 17:36:07 +01:00
# endif
2015-07-16 09:55:23 +02:00
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 ( ) ;
2011-11-05 17:36:07 +01:00
2023-12-01 00:17:45 +01:00
oss < < " \n "
< < number_of_threads < < " * " < < g_iterations < < " log entries took: [ " < < total_time_us / 1000000 < < " s] to write to disk " < < std : : endl ;
2015-07-16 09:55:23 +02:00
oss < < " [Application( " < < number_of_threads < < " ): \t \t : " < < application_time_us / 1000 < < " ms] " < < std : : endl ;
2023-12-01 00:17:45 +01:00
oss < < " [Background thread to finish \t : " < < total_time_us / uint64_t ( 1000 ) < < " ms] " < < std : : endl ;
2015-07-16 09:55:23 +02:00
oss < < " \n Average 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 ;
2011-11-05 17:36:07 +01:00
2015-07-16 09:55:23 +02:00
return 0 ;
2011-11-05 17:36:07 +01:00
}