Separate benchmark tool from rpc-port
[platform/core/appfw/rpc-port.git] / benchmark / tool / main.cc
index b9bece1..a7b037a 100644 (file)
 #include <unistd.h>
 
 #include <chrono>
+#include <ctime>
+#include <fstream>
 #include <iostream>
+#include <sstream>
+#include <string>
 
-#include "log-private.hh"
-#include "options.hh"
 #include "BenchmarkProxy.h"
 #include "dbus-proxy.hh"
 #include "grpc-proxy.hh"
+#include "log-private.hh"
+#include "options.hh"
 
 namespace {
 
@@ -73,6 +77,8 @@ class Tester {
       return;
     }
 
+    if (options_->ShouldPrintTime()) PrintStartTime();
+
     ExecuteServer();
     if (options_->IsDbus()) {
       dbus_proxy_.Connect();
@@ -127,6 +133,8 @@ class Tester {
     bool is_dbus = options_->IsDbus();
     bool is_grpc = options_->IsGrpc();
 
+    if (is_dbus) dbus_proxy_.Start(getpid());
+
     StartTime();
     for (int i = 0; i < iters; i++) {
       if (is_func) {
@@ -166,6 +174,8 @@ class Tester {
       }
     }
     EndTime(iters, size);
+
+    if (is_dbus) dbus_proxy_.Stop(getpid());
   }
 
   int FakeFunction(std::string str) {
@@ -189,6 +199,7 @@ class Tester {
   void ExecuteServer() {
     bool is_dbus = options_->IsDbus();
     bool is_grpc = options_->IsGrpc();
+    if (!is_grpc) return;
 
     server_pid_ = fork();
     if (server_pid_ == 0) {
@@ -220,6 +231,43 @@ class Tester {
     }
   }
 
+  void PrintStartTime() {
+    std::ifstream stat_file("/proc/self/stat");
+    if (!stat_file) {
+      _E("Failed to open stat");
+      return;
+    }
+
+    std::string line;
+    getline(stat_file, line);
+    std::istringstream iss(line);
+
+    std::string value;
+    int pos = 21;
+    for (int i = 0; i < pos; ++i) iss >> value;
+
+    iss >> value;
+    stat_file.close();
+
+    long start_time = std::stol(value) / sysconf(_SC_CLK_TCK);
+    time_t start_time_seconds = start_time;
+    long start_time_microseconds = (start_time - start_time_seconds) * 1000000;
+
+    std::time_t start_time_utc = std::time(nullptr) - start_time_seconds;
+    std::tm* start_time_tm = std::localtime(&start_time_utc);
+
+    char buffer[26];
+    std::strftime(buffer, sizeof(buffer), "%Y-%m %H:%M:%S", start_time_tm);
+
+    std::string result = buffer;
+    result += ".";
+    result += std::to_string(start_time_microseconds);
+    result += " UTC";
+
+    std::cout << "Program start time: [" << value << "] " << result
+              << std::endl;
+  }
+
  private:
   std::unique_ptr<rpc_port::benchmark::Options> options_;
   std::unique_ptr<bp::Benchmark> proxy_;