]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/obj_bencher.cc: faster object name generation 7863/head
authorPiotr Dałek <piotr.dalek@ts.fujitsu.com>
Tue, 1 Mar 2016 12:25:36 +0000 (13:25 +0100)
committerPiotr Dałek <piotr.dalek@ts.fujitsu.com>
Tue, 1 Mar 2016 12:47:10 +0000 (13:47 +0100)
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 <piotr.dalek@ts.fujitsu.com>
src/common/obj_bencher.cc

index a6dc3e8ae9d3551454ad4511098971f16a6af830..9f514c49e55278449cce5ba322e8a0956872a85f 100644 (file)
 #include <sstream>
 #include <vector>
 
-
 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) {