OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/test/connection_time_observer.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/time/time.h" |
| 11 #include "base/timer/timer.h" |
| 12 #include "remoting/protocol/connection_to_host_impl.h" |
| 13 |
| 14 namespace remoting { |
| 15 namespace test { |
| 16 |
| 17 ConnectionTimeObserver::ConnectionTimeObserver() { |
| 18 } |
| 19 |
| 20 ConnectionTimeObserver::~ConnectionTimeObserver() { |
| 21 } |
| 22 |
| 23 void ConnectionTimeObserver::SetTransitionTimesMapForTest( |
| 24 const std::map<protocol::ConnectionToHost::State, base::TimeTicks>& map) { |
| 25 transition_times_map_ = map; |
| 26 } |
| 27 |
| 28 void ConnectionTimeObserver::ConnectionStateChanged( |
| 29 protocol::ConnectionToHost::State state, |
| 30 protocol::ErrorCode error_code) { |
| 31 if (transition_times_map_.find(state) != transition_times_map_.end()) { |
| 32 LOG(ERROR) << ConnectionStateToFriendlyString(state) |
| 33 << " state has already been set"; |
| 34 return; |
| 35 } |
| 36 transition_times_map_.insert(std::make_pair(state, base::TimeTicks::Now())); |
| 37 current_connection_state_ = state; |
| 38 } |
| 39 |
| 40 void ConnectionTimeObserver::DisplayConnectionStats() const { |
| 41 protocol::ConnectionToHost::State initializing = |
| 42 protocol::ConnectionToHost::State::INITIALIZING; |
| 43 protocol::ConnectionToHost::State current_state = initializing; |
| 44 |
| 45 const char kStateChangeTitleFormatString[] = "%-35s%-15s"; |
| 46 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, |
| 47 "State to State", "Delta Time"); |
| 48 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, |
| 49 "--------------", "----------"); |
| 50 |
| 51 // Note: the order of |connected_states| mimics the expected order of when a |
| 52 // connection is made. |
| 53 std::vector<protocol::ConnectionToHost::State> connected_states; |
| 54 connected_states.push_back(protocol::ConnectionToHost::State::CONNECTING); |
| 55 connected_states.push_back(protocol::ConnectionToHost::State::AUTHENTICATED); |
| 56 connected_states.push_back(protocol::ConnectionToHost::State::CONNECTED); |
| 57 connected_states.push_back(protocol::ConnectionToHost::State::FAILED); |
| 58 |
| 59 const char kStateChangeFormatString[] = "%-13s to %-18s%-7dms"; |
| 60 auto iter_end = transition_times_map_.end(); |
| 61 for (protocol::ConnectionToHost::State state : connected_states) { |
| 62 auto iter_state = transition_times_map_.find(state); |
| 63 if (iter_state != iter_end) { |
| 64 LOG(INFO) << base::StringPrintf(kStateChangeFormatString, |
| 65 ConnectionStateToFriendlyString(current_state), |
| 66 ConnectionStateToFriendlyString(state), |
| 67 static_cast<int>(GetStateTransitionTime(current_state, |
| 68 state).InMilliseconds())); |
| 69 current_state = state; |
| 70 } |
| 71 } |
| 72 |
| 73 // |current state| will either be FAILED or CONNECTED. |
| 74 LOG(INFO) << "Total Connection Duration (INITIALIZING to " |
| 75 << ConnectionStateToFriendlyString(current_state) << "): " |
| 76 << GetStateTransitionTime(initializing, |
| 77 current_state).InMilliseconds() << " ms"; |
| 78 } |
| 79 |
| 80 base::TimeDelta ConnectionTimeObserver::GetStateTransitionTime( |
| 81 protocol::ConnectionToHost::State start_state, |
| 82 protocol::ConnectionToHost::State end_state) const { |
| 83 auto iter_end = transition_times_map_.end(); |
| 84 |
| 85 auto iter_start_state = transition_times_map_.find(start_state); |
| 86 if (iter_start_state == iter_end) { |
| 87 LOG(ERROR) << "No time found for state: " |
| 88 << ConnectionStateToFriendlyString(start_state); |
| 89 return base::TimeDelta::Max(); |
| 90 } |
| 91 |
| 92 auto iter_end_state = transition_times_map_.find(end_state); |
| 93 if (iter_end_state == iter_end) { |
| 94 LOG(ERROR) << "No time found for state: " |
| 95 << ConnectionStateToFriendlyString(end_state); |
| 96 return base::TimeDelta::Max(); |
| 97 } |
| 98 |
| 99 base::TimeDelta delta = iter_end_state->second - iter_start_state->second; |
| 100 if (delta.InMilliseconds() < 0) { |
| 101 LOG(ERROR) << "Transition delay is negative. Check the state ordering: " |
| 102 << "[start: " << ConnectionStateToFriendlyString(start_state) |
| 103 << ", end: " << ConnectionStateToFriendlyString(end_state) |
| 104 << "]"; |
| 105 return base::TimeDelta::Max(); |
| 106 } |
| 107 |
| 108 return delta; |
| 109 } |
| 110 |
| 111 } // namespace test |
| 112 } // namespace remoting |
OLD | NEW |