From 16324fc519f40fa83a8423719d44f712ec46a28d Mon Sep 17 00:00:00 2001 From: Oran Agra Date: Thu, 26 Mar 2015 10:11:54 +0200 Subject: [PATCH] fix random seed bug. improve accuracy of benchmark statistics by using average on the client durations --- client.cpp | 30 +++++++++++++++++++----------- client.h | 2 +- memtier_benchmark.cpp | 6 ++++-- obj_gen.cpp | 1 + 4 files changed, 25 insertions(+), 14 deletions(-) mode change 100644 => 100755 client.cpp mode change 100644 => 100755 client.h mode change 100644 => 100755 memtier_benchmark.cpp diff --git a/client.cpp b/client.cpp old mode 100644 new mode 100755 index b5ea6fa9..605c59e2 --- a/client.cpp +++ b/client.cpp @@ -79,6 +79,15 @@ inline unsigned long int ts_diff_now(struct timeval a) return bval - aval; } +inline timeval timeval_factorial_avarge( timeval a, timeval b, unsigned int weight) +{ + timeval tv; + double factor = ((double)weight - 1) / weight; + tv.tv_sec = factor * a.tv_sec + (double)b.tv_sec / weight ; + tv.tv_usec = factor * a.tv_usec + (double)b.tv_usec / weight ; + return (tv); +} + client::request::request(request_type type, unsigned int size, struct timeval* sent_time, unsigned int keys) : m_type(type), m_size(size), m_keys(keys) { @@ -955,9 +964,10 @@ unsigned long int client_group::get_total_latency(void) unsigned long int client_group::get_duration_usec(void) { unsigned long int duration = 0; - for (std::vector::iterator i = m_clients.begin(); i != m_clients.end(); i++) { - if ((*i)->get_stats()->get_duration_usec() > duration) - duration = (*i)->get_stats()->get_duration_usec(); + unsigned int thread_counter = 1; + for (std::vector::iterator i = m_clients.begin(); i != m_clients.end(); i++, thread_counter++) { + float factor = ((float)(thread_counter - 1) / thread_counter); + duration = factor * duration + (float)(*i)->get_stats()->get_duration_usec() / thread_counter ; } return duration; @@ -966,10 +976,10 @@ unsigned long int client_group::get_duration_usec(void) void client_group::merge_run_stats(run_stats* target) { assert(target != NULL); - + unsigned int iteration_counter = 1; for (std::vector::iterator i = m_clients.begin(); i != m_clients.end(); i++) { - target->merge(*(*i)->get_stats()); - } + target->merge(*(*i)->get_stats(), iteration_counter++); + } } void client_group::write_client_stats(const char *prefix) @@ -1294,14 +1304,12 @@ void run_stats::aggregate_average(const std::vector& all_stats) } -void run_stats::merge(const run_stats& other) +void run_stats::merge(const run_stats& other, int iteration) { bool new_stats = false; - if (!m_start_time.tv_sec) - m_start_time = other.m_start_time; - if (!m_end_time.tv_sec) - m_end_time = other.m_end_time; + m_start_time = timeval_factorial_avarge( m_start_time, other.m_start_time, iteration ); + m_end_time = timeval_factorial_avarge( m_end_time, other.m_end_time, iteration ); // aggregate the one_second_stats vectors. this is not efficient // but it's not really important (small numbers, not realtime) diff --git a/client.h b/client.h old mode 100644 new mode 100755 index 48cad460..1e7e5d51 --- a/client.h +++ b/client.h @@ -106,7 +106,7 @@ class run_stats { void aggregate_average(const std::vector& all_stats); void summarize(totals& result) const; - void merge(const run_stats& other); + void merge(const run_stats& other, int iteration); bool save_csv(const char *filename); void debug_dump(void); void print(FILE *file, bool histogram); diff --git a/memtier_benchmark.cpp b/memtier_benchmark.cpp old mode 100644 new mode 100755 index 1fde8383..5c996a00 --- a/memtier_benchmark.cpp +++ b/memtier_benchmark.cpp @@ -735,6 +735,7 @@ run_stats run_benchmark(int run_id, benchmark_config* cfg, object_generator* obj unsigned long int total_ops = 0; unsigned long int total_bytes = 0; unsigned long int duration = 0; + unsigned int thread_counter = 0; unsigned long int total_latency = 0; for (std::vector::iterator i = threads.begin(); i != threads.end(); i++) { @@ -744,8 +745,9 @@ run_stats run_benchmark(int run_id, benchmark_config* cfg, object_generator* obj total_ops += (*i)->m_cg->get_total_ops(); total_bytes += (*i)->m_cg->get_total_bytes(); total_latency += (*i)->m_cg->get_total_latency(); - if ((*i)->m_cg->get_duration_usec() > duration) - duration = (*i)->m_cg->get_duration_usec(); + thread_counter++; + float factor = ((float)(thread_counter - 1) / thread_counter); + duration = factor * duration + (float)(*i)->m_cg->get_duration_usec() / thread_counter ; } unsigned long int cur_ops = total_ops-prev_ops; diff --git a/obj_gen.cpp b/obj_gen.cpp index 859ddfb8..7bffca7b 100644 --- a/obj_gen.cpp +++ b/obj_gen.cpp @@ -41,6 +41,7 @@ random_generator::random_generator() void random_generator::set_seed(int seed) { + seed++; //http://stackoverflow.com/questions/27386470/srand0-and-srand1-give-the-same-results #ifdef HAVE_RANDOM_R memset(&m_data_blob, 0, sizeof(m_data_blob)); memset(m_state_array, 0, sizeof(m_state_array));