int main(int argc, char * argv[]) { // command line options if (cmdline_option_exists(argv, argv + argc, "-h")) help_exit(argc, argv); const char * n_threads_str = get_cmdline_option(argv, argv + argc, "-p"); if (!n_threads_str) help_exit(argc, argv); const size_t n_threads = std::atoi(n_threads_str); const char * n_expected_columns_str = get_cmdline_option(argv, argv + argc, "-c"); if (!n_expected_columns_str) help_exit(argc, argv); const size_t n_expected_columns = std::atoi(n_expected_columns_str); const char * filepath = get_cmdline_option(argv, argv + argc, "-f"); if (!filepath) help_exit(argc, argv); // instantiate CsvConfig BENCH_START; PCP::CsvConfig csv_config(filepath, false); BENCH_STOP("mmap(2)+madvise(2) file"); // setup range each thread parse size_t size_per_thread = (csv_config.filesize() - csv_config.body_offset()) / n_threads; std::vector<parser_thread_arg_t> parser_thread_args(n_threads); for (size_t i = 0; i < n_threads; ++i) { parser_thread_arg_t & parser_thread_arg = parser_thread_args[i]; parser_thread_arg.n_columns = 0; PCP::partial_csv_t & partial_csv = parser_thread_arg.partial_csv; partial_csv.csv_config = &csv_config; partial_csv.parse_from = csv_config.body_offset() + i * size_per_thread; partial_csv.parse_to = csv_config.body_offset() + (i + 1) * size_per_thread - 1; } // create threads std::vector<pthread_t> tids(n_threads); BENCH_START; for (size_t i = 0; i < n_threads; ++i) pthread_create(&tids[i], NULL, (void *(*)(void *))partial_parse, &parser_thread_args[i]); // join threads for (size_t i = 0; i < n_threads; ++i) pthread_join(tids[i], NULL); BENCH_STOP("join parsing threads"); // calculate total number of columns size_t n_total_columns = 0; for (size_t i = 0; i < n_threads; ++i) n_total_columns += parser_thread_args[i].n_columns; // check the answer if (n_total_columns == n_expected_columns) { std::cout << "OK. Parsed " << n_total_columns << " columns." << std::endl; return 0; } else { std::cout << "NG. Parsed " << n_total_columns << " columns, while " << n_expected_columns << " columns are expected." << std::endl; return 1; } }
static void bench_mutex_threaded() { int i; pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; /* mutex lock/unlock (2-5 threads) */ for (i=2; i<=5; i++) { int t; pthread_t threads[i]; BENCH_START(); for (t=0; t<i; t++) pthread_create( &threads[t], NULL, mutex_lock_unlock_loop, &lock ); for (t=0; t<i; t++) pthread_join( threads[t], NULL ); BENCH_STOP(); printf( "mutex lock/unlock (rec., %d threads) -> %8.2f k/sec\n", i, BENCH_RESULT() ); } pthread_mutex_destroy( &lock ); printf( "\n" ); }
static void bench_flock() { int fd; FILE *tmp; tmp = tmpfile(); if (!tmp) { perror( "tmpfile()" ); return; } fd = fileno( tmp ); if (fd < 0) { perror( "fileno()" ); fclose( tmp ); return; } BENCH_START(); BENCH_LOOP() { flock( fd, LOCK_EX ); flock( fd, LOCK_UN ); } BENCH_STOP(); printf( "flock lock/unlock -> %8.2f k/sec\n", BENCH_RESULT() ); printf( "\n" ); fclose( tmp ); }
static void bench_skirmish() { DirectResult ret; FusionSkirmish skirmish; ret = fusion_skirmish_init( &skirmish, "Benchmark", world ); if (ret) { fprintf( stderr, "Fusion Error %d\n", ret ); return; } /* skirmish prevail/dismiss */ BENCH_START(); BENCH_LOOP() { fusion_skirmish_prevail( &skirmish ); fusion_skirmish_dismiss( &skirmish ); } BENCH_STOP(); printf( "skirmish prevail/dismiss -> %8.2f k/sec\n", BENCH_RESULT() ); fusion_skirmish_destroy( &skirmish ); printf( "\n" ); }
static void bench_property() { DirectResult ret; FusionProperty property; ret = fusion_property_init( &property, world ); if (ret) { fprintf( stderr, "Fusion Error %d\n", ret ); return; } /* property lease/cede */ BENCH_START(); BENCH_LOOP() { fusion_property_lease( &property ); fusion_property_cede( &property ); } BENCH_STOP(); printf( "property lease/cede -> %8.2f k/sec\n", BENCH_RESULT() ); fusion_property_destroy( &property ); printf( "\n" ); }
static void bench_ref() { DirectResult ret; FusionRef ref; ret = fusion_ref_init( &ref, "Benchmark", world ); if (ret) { fprintf( stderr, "Fusion Error %d\n", ret ); return; } /* ref up/down (local) */ BENCH_START(); BENCH_LOOP() { fusion_ref_up( &ref, false ); fusion_ref_down( &ref, false ); } BENCH_STOP(); printf( "ref up/down (local) -> %8.2f k/sec\n", BENCH_RESULT() ); /* ref up/down (global) */ BENCH_START(); BENCH_LOOP() { fusion_ref_up( &ref, true ); fusion_ref_down( &ref, true ); } BENCH_STOP(); printf( "ref up/down (global) -> %8.2f k/sec\n", BENCH_RESULT() ); fusion_ref_destroy( &ref ); printf( "\n" ); }
static void bench_mutex() { pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t rmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; /* pthread_mutex lock/unlock */ BENCH_START(); BENCH_LOOP() { pthread_mutex_lock( &mutex ); pthread_mutex_unlock( &mutex ); } BENCH_STOP(); printf( "mutex lock/unlock -> %8.2f k/sec\n", BENCH_RESULT() ); /* pthread_mutex lock/unlock */ BENCH_START(); BENCH_LOOP() { pthread_mutex_lock( &rmutex ); pthread_mutex_unlock( &rmutex ); } BENCH_STOP(); printf( "mutex lock/unlock (recursive) -> %8.2f k/sec\n", BENCH_RESULT() ); pthread_mutex_destroy( &mutex ); pthread_mutex_destroy( &rmutex ); printf( "\n" ); }
static void bench_shmpool( bool debug ) { DirectResult ret; void *mem[256]; const int sizes[8] = { 12, 36, 200, 120, 39, 3082, 8, 1040 }; FusionSHMPoolShared *pool; ret = fusion_shm_pool_create( world, "Benchmark Pool", 524288, debug, &pool ); if (ret) { DirectFBError( "fusion_shm_pool_create() failed", ret ); return; } BENCH_START(); BENCH_LOOP() { int i; for (i=0; i<128; i++) mem[i] = SHMALLOC( pool, sizes[i&7] ); for (i=0; i<64; i++) SHFREE( pool, mem[i] ); for (i=128; i<192; i++) mem[i] = SHMALLOC( pool, sizes[i&7] ); for (i=64; i<128; i++) SHFREE( pool, mem[i] ); for (i=192; i<256; i++) mem[i] = SHMALLOC( pool, sizes[i&7] ); for (i=128; i<256; i++) SHFREE( pool, mem[i] ); } BENCH_STOP(); printf( "shm pool alloc/free %s -> %8.2f k/sec\n", debug ? "(debug)" : " ", BENCH_RESULT_BY(256) ); fusion_shm_pool_destroy( world, pool ); }
static void bench_skirmish_threaded() { int i; DirectResult ret; FusionSkirmish skirmish; ret = fusion_skirmish_init( &skirmish, "Threaded Benchmark", world ); if (ret) { fprintf( stderr, "Fusion Error %d\n", ret ); return; } /* skirmish prevail/dismiss (2-5 threads) */ for (i=2; i<=5; i++) { int t; pthread_t threads[i]; BENCH_START(); for (t=0; t<i; t++) pthread_create( &threads[t], NULL, prevail_dismiss_loop, &skirmish ); for (t=0; t<i; t++) pthread_join( threads[t], NULL ); BENCH_STOP(); printf( "skirmish prevail/dismiss (%d threads) -> %8.2f k/sec\n", i, BENCH_RESULT() ); } fusion_skirmish_destroy( &skirmish ); printf( "\n" ); }
static void bench_reactor() { FusionReactor *reactor; Reaction reaction; Reaction reaction2; GlobalReaction global_reaction; reactor = fusion_reactor_new( 16, "Benchmark", world ); if (!reactor) { fprintf( stderr, "Fusion Error\n" ); return; } /* reactor attach/detach */ BENCH_START(); BENCH_LOOP() { fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction ); fusion_reactor_detach( reactor, &reaction ); } BENCH_STOP(); printf( "reactor attach/detach -> %8.2f k/sec\n", BENCH_RESULT() ); /* reactor attach/detach (2nd) */ fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction ); BENCH_START(); BENCH_LOOP() { fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction2 ); fusion_reactor_detach( reactor, &reaction2 ); } BENCH_STOP(); fusion_reactor_detach( reactor, &reaction ); printf( "reactor attach/detach (2nd) -> %8.2f k/sec\n", BENCH_RESULT() ); /* reactor attach/detach (global) */ fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction ); BENCH_START(); BENCH_LOOP() { fusion_reactor_attach_global( reactor, 0, NULL, &global_reaction ); fusion_reactor_detach_global( reactor, &global_reaction ); } BENCH_STOP(); fusion_reactor_detach( reactor, &reaction ); printf( "reactor attach/detach (global) -> %8.2f k/sec\n", BENCH_RESULT() ); /* reactor dispatch */ fusion_reactor_attach( reactor, reaction_callback, NULL, &reaction ); BENCH_START(); BENCH_LOOP() { char msg[16]; fusion_reactor_dispatch( reactor, msg, true, NULL ); } BENCH_STOP(); printf( "reactor dispatch -> %8.2f k/sec\n", BENCH_RESULT() ); fusion_reactor_detach( reactor, &reaction ); fusion_reactor_free( reactor ); printf( "\n" ); }