3.5 - Time Tester
authorJustin Dickow <jjdickow@gmail.com>
Thu, 17 Jul 2014 18:48:42 +0000 (14:48 -0400)
committerJustin Dickow <jjdickow@gmail.com>
Thu, 17 Jul 2014 18:48:42 +0000 (14:48 -0400)
Author:
Alex Kutsan

Signed-off-by: Justin Dickow <jjdickow@gmail.com>
15 files changed:
src/components/time_tester/CMakeLists.txt
src/components/time_tester/include/time_tester/application_manager_metric.h
src/components/time_tester/include/time_tester/json_keys.h
src/components/time_tester/include/time_tester/metric_wrapper.h [new file with mode: 0644]
src/components/time_tester/include/time_tester/protocol_handler_metric.h
src/components/time_tester/include/time_tester/time_manager.h
src/components/time_tester/include/time_tester/transport_manager_metric.h
src/components/time_tester/src/application_manager_metric.cc
src/components/time_tester/src/application_manager_observer.cc
src/components/time_tester/src/metric_wrapper.cc [new file with mode: 0644]
src/components/time_tester/src/protocol_handler_metric.cc
src/components/time_tester/src/protocol_handler_observer.cc
src/components/time_tester/src/time_manager.cc
src/components/time_tester/src/transport_manager_metric.cc
src/components/time_tester/src/transport_manager_observer.cc

index fea8743..3884199 100644 (file)
@@ -16,6 +16,7 @@ include_directories (
 )
 
 set (SOURCES
+    ./src/metric_wrapper.cc
     ./src/time_manager.cc
     ./src/application_manager_observer.cc
     ./src/transport_manager_observer.cc
index a0c8bf6..1c21c0f 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_APPLICATION_MANAGER_MECTRIC_H_
-#define SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_APPLICATION_MANAGER_MECTRIC_H_
+#ifndef SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_APPLICATION_MANAGER_MECTRIC_WRAPPER_H_
+#define SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_APPLICATION_MANAGER_MECTRIC_WRAPPER_H_
 
 #include <string>
 
-#include "metric.h"
+#include "metric_wrapper.h"
 #include "application_manager_observer.h"
 
 
@@ -43,12 +43,14 @@ namespace time_tester {
 
 class ApplicationManagerObserver;
 
-class ApplicationManagerMetric: public Metric {
+class ApplicationManagerMetricWrapper: public MetricWrapper {
 
   public:
     utils::SharedPtr<application_manager::AMMetricObserver::MessageMetric> message_metric;
-    std::string GetStyledString();
+
+  protected:
+    virtual Json::Value GetJsonMetric();
 };
 
 }
-#endif  // SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_APPLICATION_MANAGER_MECTRIC_H_
+#endif  // SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_APPLICATION_MANAGER_MECTRIC_WRAPPER_H_
index 7540991..d1ace3f 100644 (file)
@@ -44,8 +44,9 @@ namespace time_tester {
     const char session_id[] = "session_id";
     const char correlation_id[] = "correlation_id";
     const char connection_key[] = "connection_key";
-
-
+    const char stime[] = "stime";
+    const char utime[] = "utime";
+    const char memory[] = "RAM";
   }
 }
 #endif  // SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_JSON_KEYS_H_
diff --git a/src/components/time_tester/include/time_tester/metric_wrapper.h b/src/components/time_tester/include/time_tester/metric_wrapper.h
new file mode 100644 (file)
index 0000000..705316a
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014, Ford Motor Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the Ford Motor Company nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_MECTRIC_H_
+#define SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_MECTRIC_H_
+
+#include <string>
+#include "utils/resource_usage.h"
+#include "json/json.h"
+
+namespace time_tester {
+
+class MetricWrapper {
+  utils::ResourseUsage* resources;
+  public:
+    MetricWrapper();
+    /*
+     * @brief get current cpu and memory info and store it as member
+     */
+    bool grabResources();
+    virtual std::string GetStyledString();
+    ~MetricWrapper();
+  protected:
+    virtual Json::Value GetJsonMetric();
+    void Clear();
+};
+
+
+}  // namespace time_tester
+#endif  // SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_MECTRIC_H_
index 507f054..47db117 100644 (file)
 
 #include <string>
 #include "utils/shared_ptr.h"
-#include "metric.h"
+#include "metric_wrapper.h"
 #include "protocol_handler_observer.h"
 
 namespace time_tester {
 
-class ProtocolHandlerMectic: public Metric {
+class ProtocolHandlerMecticWrapper: public MetricWrapper {
 
   public:
     utils::SharedPtr<protocol_handler::PHMetricObserver::MessageMetric> message_metric;
-    std::string GetStyledString();
+  protected:
+    virtual Json::Value GetJsonMetric();
 };
 }  // namespace time_tester
 #endif  // SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_PROTOCOL_HANDLER_MECTRIC_H_
index a4f26dc..cd8bc4f 100644 (file)
@@ -40,7 +40,7 @@
 #include "utils/threads/thread.h"
 #include "utils/singleton.h"
 #include "utils/threads/thread_delegate.h"
-#include "metric.h"
+#include "metric_wrapper.h"
 #include "application_manager_observer.h"
 #include "application_manager/application_manager_impl.h"
 #include "transport_manager_observer.h"
@@ -56,19 +56,12 @@ class TimeManager {
   ~TimeManager();
   void Init(protocol_handler::ProtocolHandlerImpl* ph);
   void Stop();
-  void SendMetric(utils::SharedPtr<Metric> metric);
+  void SendMetric(utils::SharedPtr<MetricWrapper> metric);
  private:
   ApplicationManagerObserver app_observer;
   TransportManagerObserver tm_observer;
   ProtocolHandlerObserver ph_observer;
 
-  int16_t port_;
-  std::string ip_;
-  int32_t socket_fd_;
-  bool is_ready_;
-  threads::Thread* thread_;
-  MessageQueue<utils::SharedPtr<Metric> > messages_;
-
   class Streamer : public threads::ThreadDelegate {
    public:
     explicit Streamer(TimeManager* const server);
@@ -79,13 +72,22 @@ class TimeManager {
     void Start();
     void Stop();
     bool Send(const std::string &msg);
+  volatile bool is_client_connected_;
   private:
     TimeManager* const server_;
-    int32_t new_socket_fd_;
-    volatile bool is_client_connected_;
+    int32_t new_socket_fd_;  
     volatile bool stop_flag_;
     DISALLOW_COPY_AND_ASSIGN(Streamer);
   };
+
+  int16_t port_;
+  std::string ip_;
+  int32_t socket_fd_;
+  bool is_ready_;
+  threads::Thread* thread_;
+  MessageQueue<utils::SharedPtr<MetricWrapper> > messages_;
+  Streamer* streamer_;
+
   DISALLOW_COPY_AND_ASSIGN(TimeManager);
 };
 }  // namespace time_manager
index c2c9c4a..25261b0 100644 (file)
 #define SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_TRANSPORT_MANAGER_MECTRIC_H_
 
 #include <string>
-#include "metric.h"
+#include "metric_wrapper.h"
 #include "transport_manager_observer.h"
 
 namespace time_tester {
 
-class TransportManagerMectic: public Metric {
+class TransportManagerMecticWrapper: public MetricWrapper {
  public:
   utils::SharedPtr<transport_manager::TMMetricObserver::MessageMetric> message_metric;
-  std::string GetStyledString();
+  protected:
+    virtual Json::Value GetJsonMetric();
 };
 }  // namespace time_tester
 #endif  // SRC_COMPONENTS_TIME_TESTER_INCLUDE_TIME_TESTER_TRANSPORT_MANAGER_MECTRIC_H_
index 6978e72..251323d 100644 (file)
@@ -36,8 +36,8 @@
 
 namespace time_tester {
 
-std::string ApplicationManagerMetric::GetStyledString() {
-  Json::Value result;
+Json::Value ApplicationManagerMetricWrapper::GetJsonMetric() {
+  Json::Value result = MetricWrapper::GetJsonMetric();
   result[strings::logger] = "ApplicationManager";
   result[strings::begin] =
       Json::Int64(date_time::DateTime::getuSecs(message_metric->begin));
@@ -49,6 +49,6 @@ std::string ApplicationManagerMetric::GetStyledString() {
       params[application_manager::strings::correlation_id].asInt();
   result[strings::connection_key] =
       params[application_manager::strings::connection_key].asInt();
-  return result.toStyledString();
+  return result;
 }
 }  // namespace time_tester
index fa768e2..67d7268 100644 (file)
@@ -41,8 +41,9 @@ ApplicationManagerObserver::ApplicationManagerObserver(TimeManager* time_manager
 }
 
 void ApplicationManagerObserver::OnMessage(utils::SharedPtr<MessageMetric> metric) {
-  ApplicationManagerMetric* m = new ApplicationManagerMetric();
+  ApplicationManagerMetricWrapper* m = new ApplicationManagerMetricWrapper();
   m->message_metric = metric;
+  m->grabResources();
   time_manager_->SendMetric(m);
 }
 }  // namespace time_tester
diff --git a/src/components/time_tester/src/metric_wrapper.cc b/src/components/time_tester/src/metric_wrapper.cc
new file mode 100644 (file)
index 0000000..2922bf3
--- /dev/null
@@ -0,0 +1,46 @@
+#include "metric_wrapper.h"
+#include "json_keys.h"
+
+namespace time_tester {
+
+  MetricWrapper::MetricWrapper():
+    resources(NULL) {
+  }
+
+bool MetricWrapper::grabResources() {
+  Clear();
+  resources = utils::Resources::getCurrentResourseUsage();
+  if (NULL != resources) {
+    return true;
+  } else {
+    return false;
+  }
+  flush(std::cout);
+}
+
+std::string MetricWrapper::GetStyledString() {
+  return GetJsonMetric().toStyledString();
+}
+
+Json::Value MetricWrapper::GetJsonMetric() {
+  Json::Value result;
+  if (resources) {
+    result[strings::stime] = resources->stime;
+    result[strings::utime] = resources->utime;
+    result[strings::memory] = resources->memory;
+  }
+  return result;
+}
+
+void MetricWrapper::Clear() {
+  if (NULL != resources) {
+    delete resources;
+    resources = NULL;
+  }
+}
+
+MetricWrapper::~MetricWrapper() {
+  Clear();
+}
+
+}
index 278cab2..93e0864 100644 (file)
@@ -36,8 +36,8 @@
 
 namespace time_tester {
 
-std::string ProtocolHandlerMectic::GetStyledString() {
-  Json::Value result;
+Json::Value ProtocolHandlerMecticWrapper::GetJsonMetric() {
+  Json::Value result = MetricWrapper::GetJsonMetric();
   result[strings::logger] = "ProtocolHandler";
   result[strings::begin] =
       Json::Int64(date_time::DateTime::getuSecs(message_metric->begin));
@@ -45,6 +45,7 @@ std::string ProtocolHandlerMectic::GetStyledString() {
       Json::Int64(date_time::DateTime::getuSecs(message_metric->end));
   result[strings::message_id] = message_metric->message_id;
   result[strings::connection_key] = message_metric->connection_key;
-  return result.toStyledString();
+  return result;
 }
+
 }  // namespace time_tester
index 1ddf084..a5ca678 100644 (file)
@@ -63,8 +63,9 @@ void ProtocolHandlerObserver::EndMessageProcess(utils::SharedPtr<MessageMetric>
   }
   m->begin= time_starts[message_id];
   m->end = date_time::DateTime::getCurrentTime();
-  ProtocolHandlerMectic* metric = new ProtocolHandlerMectic();
+  ProtocolHandlerMecticWrapper* metric = new ProtocolHandlerMecticWrapper();
   metric->message_metric = m;
+  metric->grabResources();
   time_manager_->SendMetric(metric);
 }
 }  //namespace time_tester
index d7c4589..c68dd9d 100644 (file)
@@ -43,6 +43,7 @@
 
 #include "transport_manager/transport_manager_default.h"
 #include "config_profile/profile.h"
+#include "utils/resource_usage.h"
 
 namespace time_tester {
 
@@ -54,7 +55,8 @@ TimeManager::TimeManager():
   thread_(NULL),
   app_observer(this),
   tm_observer(this),
-  ph_observer(this) {
+  ph_observer(this),
+  streamer_(NULL) {
     ip_ = profile::Profile::instance()->server_address();
     port_ = profile::Profile::instance()->time_testing_port();
 }
@@ -67,7 +69,8 @@ TimeManager::~TimeManager() {
 void TimeManager::Init(protocol_handler::ProtocolHandlerImpl* ph) {
   DCHECK(ph);
   if (!thread_) {
-    thread_ = new threads::Thread("SocketAdapter", new Streamer(this));
+    streamer_ = new Streamer(this);
+    thread_ = new threads::Thread("SocketAdapter", streamer_ );
     application_manager::ApplicationManagerImpl::instance()->SetTimeMetricObserver(&app_observer);
     transport_manager::TransportManagerDefault::instance()->SetTimeMetricObserver(&tm_observer);
     ph->SetTimeMetricObserver(&ph_observer);
@@ -85,11 +88,14 @@ void TimeManager::Stop() {
       ::close(socket_fd_);
     }
   }
+  messages_.Reset();
   LOG4CXX_INFO(logger_, "TimeManager stopped");
 }
 
-void TimeManager::SendMetric(utils::SharedPtr<Metric> metric) {
-  messages_.push(metric);
+void TimeManager::SendMetric(utils::SharedPtr<MetricWrapper> metric) {
+  if ((NULL != streamer_ )&& streamer_->is_client_connected_) {
+    messages_.push(metric);
+  }
 }
 
 TimeManager::Streamer::Streamer(
@@ -120,7 +126,7 @@ void TimeManager::Streamer::threadMain() {
     is_client_connected_ = true;
     while (is_client_connected_) {
       while (!server_->messages_.empty()) {
-        utils::SharedPtr<Metric> metric = server_->messages_.pop();
+        utils::SharedPtr<MetricWrapper> metric = server_->messages_.pop();
         is_client_connected_ = Send(metric->GetStyledString());
       }
 
index cb2567f..a7c9ecb 100644 (file)
 
 namespace time_tester {
 
-std::string TransportManagerMectic::GetStyledString() {
-  Json::Value result;
+Json::Value TransportManagerMecticWrapper::GetJsonMetric() {
+  Json::Value result = MetricWrapper::GetJsonMetric();
   result[strings::logger] = "TransportManager";
   result[strings::begin] =
       Json::Int64(date_time::DateTime::getuSecs(message_metric->begin));
   result[strings::end] =
       Json::Int64(date_time::DateTime::getuSecs(message_metric->end));
   result[strings::data_size] = static_cast<uint32_t>(message_metric->data_size);
-  return result.toStyledString();
+  return result;
 }
+
 }  // namespace time_tester
index ad86281..41cb301 100644 (file)
@@ -50,11 +50,12 @@ void TransportManagerObserver::StopRawMsg(const protocol_handler::RawMessage* pt
     std::map<const protocol_handler::RawMessage*, TimevalStruct>::const_iterator it;
     it = time_starts.find(ptr);
     if (it != time_starts.end()) {
-      TransportManagerMectic* m = new TransportManagerMectic();
+      TransportManagerMecticWrapper* m = new TransportManagerMecticWrapper();
       m->message_metric = new transport_manager::TMMetricObserver::MessageMetric();
       m->message_metric->begin = it->second;
       m->message_metric->end = date_time::DateTime::getCurrentTime();
       m->message_metric->data_size = ptr->data_size();
+      m->grabResources();
       time_manager_->SendMetric(m);
     }
 }