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 | |
13 namespace remoting { | |
14 namespace test { | |
15 | |
16 ConnectionTimeObserver::ConnectionTimeObserver() { | |
17 } | |
18 | |
19 ConnectionTimeObserver::~ConnectionTimeObserver() { | |
20 } | |
21 | |
22 void ConnectionTimeObserver::SetTransitionTimesMapForTest( | |
23 const std::map<protocol::ConnectionToHost::State, base::TimeTicks>& map) { | |
24 transition_times_map_ = map; | |
25 } | |
26 | |
27 void ConnectionTimeObserver::ConnectionStateChanged( | |
28 protocol::ConnectionToHost::State state, | |
29 protocol::ErrorCode error_code) { | |
30 if (transition_times_map_.find(state) != transition_times_map_.end()) { | |
31 LOG(ERROR) << protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
32 state) << " state has already been set"; | |
joedow
2015/07/29 17:07:02
Can you fix the indentation here. Alternatively y
tonychun
2015/07/29 18:30:33
Done.
| |
33 return; | |
34 } | |
35 transition_times_map_.insert(std::make_pair(state, base::TimeTicks::Now())); | |
36 current_connection_state_ = state; | |
37 } | |
38 | |
39 void ConnectionTimeObserver::DisplayConnectionStats() const { | |
40 protocol::ConnectionToHost::State initializing = | |
41 protocol::ConnectionToHost::State::INITIALIZING; | |
42 protocol::ConnectionToHost::State current_state = initializing; | |
43 | |
44 const char kStateChangeTitleFormatString[] = "%-35s%-15s"; | |
45 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, | |
46 "State to State", "Delta Time"); | |
47 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, | |
48 "--------------", "----------"); | |
49 | |
50 // Note: the order of |connected_states| mimics the expected order of when a | |
51 // connection is made. | |
52 std::vector<protocol::ConnectionToHost::State> connected_states; | |
53 connected_states.push_back(protocol::ConnectionToHost::State::CONNECTING); | |
54 connected_states.push_back(protocol::ConnectionToHost::State::AUTHENTICATED); | |
55 connected_states.push_back(protocol::ConnectionToHost::State::CONNECTED); | |
56 connected_states.push_back(protocol::ConnectionToHost::State::FAILED); | |
57 | |
58 const char kStateChangeFormatString[] = "%-13s to %-18s%-7dms"; | |
59 auto iter_end = transition_times_map_.end(); | |
60 for (protocol::ConnectionToHost::State state : connected_states) { | |
61 auto iter_state = transition_times_map_.find(state); | |
62 if (iter_state != iter_end) { | |
63 LOG(INFO) << base::StringPrintf(kStateChangeFormatString, | |
64 protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
65 current_state), | |
66 protocol::ConnectionToHost::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 << protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
76 current_state) << "): " | |
77 << GetStateTransitionTime(initializing, | |
78 current_state).InMilliseconds() << " ms"; | |
joedow
2015/07/29 17:07:02
The indentation is kind of distracting here, can y
tonychun
2015/07/29 18:30:33
Done.
| |
79 } | |
80 | |
81 base::TimeDelta ConnectionTimeObserver::GetStateTransitionTime( | |
82 protocol::ConnectionToHost::State start_state, | |
83 protocol::ConnectionToHost::State end_state) const { | |
84 auto iter_end = transition_times_map_.end(); | |
85 | |
86 auto iter_start_state = transition_times_map_.find(start_state); | |
87 if (iter_start_state == iter_end) { | |
88 LOG(ERROR) << "No time found for state: " | |
89 << protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
90 start_state); | |
91 return base::TimeDelta::Max(); | |
92 } | |
93 | |
94 auto iter_end_state = transition_times_map_.find(end_state); | |
95 if (iter_end_state == iter_end) { | |
96 LOG(ERROR) << "No time found for state: " | |
97 << protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
joedow
2015/07/29 17:07:02
Using a static method on ConnectionToHost to gener
tonychun
2015/07/29 18:30:33
Done.
| |
98 end_state); | |
99 return base::TimeDelta::Max(); | |
100 } | |
101 | |
102 base::TimeDelta delta = iter_end_state->second - iter_start_state->second; | |
103 if (delta.InMilliseconds() < 0) { | |
104 LOG(ERROR) << "Transition delay is negative. Check the state ordering: " | |
105 << "[start: " | |
106 << protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
107 start_state) << ", end: " | |
108 << protocol::ConnectionToHost::ConnectionStateToFriendlyString( | |
109 end_state) << "]"; | |
110 return base::TimeDelta::Max(); | |
111 } | |
112 | |
113 return delta; | |
114 } | |
115 | |
116 } // namespace test | |
117 } // namespace remoting | |
OLD | NEW |