From 42d81bffc4d57bceb98872dadc8e57400981588c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Piotr=20Da=C5=82ek?= Date: Tue, 1 Mar 2016 13:25:36 +0100 Subject: [PATCH] common/obj_bencher.cc: faster object name generation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit generate_object_name() uses std::ostringstream to build object name and calls generate_object_prefix() (which uses another std::ostringstream), making it quite inefficient and actually being one of limiting factors in small-object benchmarks. New version that does everything on one char array and within one function is up to 3,8x faster (in my testing bumping throughput from 35MB/s to 40MB/s on rados bench rand -t 128 on 1KB objects). Signed-off-by: Piotr Dałek --- src/common/obj_bencher.cc | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/common/obj_bencher.cc b/src/common/obj_bencher.cc index a6dc3e8ae9d35..9f514c49e5527 100644 --- a/src/common/obj_bencher.cc +++ b/src/common/obj_bencher.cc @@ -28,28 +28,43 @@ #include #include - const std::string BENCH_LASTRUN_METADATA = "benchmark_last_metadata"; const std::string BENCH_PREFIX = "benchmark_data"; +static char cached_hostname[30] = {0}; +int cached_pid = 0; static std::string generate_object_prefix(int pid = 0) { - char hostname[30]; - gethostname(hostname, sizeof(hostname)-1); - hostname[sizeof(hostname)-1] = 0; + if (cached_hostname[0] == 0) { + gethostname(cached_hostname, sizeof(cached_hostname)-1); + cached_hostname[sizeof(cached_hostname)-1] = 0; + } - if (!pid) - pid = getpid(); + if (!cached_pid) { + if (!pid) + pid = getpid(); + cached_pid = pid; + } std::ostringstream oss; - oss << BENCH_PREFIX << "_" << hostname << "_" << pid; + oss << BENCH_PREFIX << "_" << cached_hostname << "_" << cached_pid; return oss.str(); } static std::string generate_object_name(int objnum, int pid = 0) { - std::ostringstream oss; - oss << generate_object_prefix(pid) << "_object" << objnum; - return oss.str(); + if (cached_hostname[0] == 0) { + gethostname(cached_hostname, sizeof(cached_hostname)-1); + cached_hostname[sizeof(cached_hostname)-1] = 0; + } + if (!cached_pid) { + if (!pid) + pid = getpid(); + cached_pid = pid; + } + + char name[1024]; + size_t l = sprintf(&name[0], "%s_%s_%d_object%d", BENCH_PREFIX.c_str(), cached_hostname, cached_pid, objnum); + return string(&name[0], l); } static void sanitize_object_contents (bench_data *data, size_t length) { -- 2.39.5