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/test/test_chromoting_client.h" | |
joedow
2015/07/27 21:45:44
I don't think this class should know about the Tes
tonychun
2015/07/28 17:53:44
Moved helper method to ConnectionToHostImpl.
| |
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::Initialize() { | |
29 if (!transition_times_map_.empty()) { | |
30 LOG(ERROR) << "INITIALIZING state is already initialized"; | |
joedow
2015/07/27 21:45:44
nit: s/is already initialized/has already been set
tonychun
2015/07/28 17:53:44
Done.
| |
31 return; | |
32 } | |
33 transition_times_map_.insert(std::make_pair( | |
34 protocol::ConnectionToHost::State::INITIALIZING, | |
joedow
2015/07/27 21:45:44
why not call just call ConectionStateChanged() wit
tonychun
2015/07/28 17:53:44
Done.
| |
35 base::TimeTicks::Now())); | |
36 } | |
37 | |
38 void ConnectionTimeObserver::ConnectionStateChanged( | |
39 protocol::ConnectionToHost::State state, | |
40 protocol::ErrorCode error_code) { | |
41 if (transition_times_map_.find(state) != transition_times_map_.end()) { | |
42 LOG(ERROR) << TestChromotingClient::ConnectionStateToFriendlyString(state) | |
43 << " state is already initialized"; | |
44 return; | |
45 } | |
46 transition_times_map_.insert(std::make_pair(state, base::TimeTicks::Now())); | |
47 current_connection_state_ = state; | |
48 } | |
49 | |
50 void ConnectionTimeObserver::ConnectionReady(bool ready) { | |
51 transition_times_map_.insert( | |
52 std::make_pair(protocol::ConnectionToHost::State::CLOSED, | |
53 base::TimeTicks::Now())); | |
joedow
2015/07/27 21:45:44
What if 'ready' is true? Then you will still log
tonychun
2015/07/28 17:53:45
For right now, since I don't have the helper class
| |
54 } | |
55 | |
56 void ConnectionTimeObserver::DisplayConnectionStats() const { | |
57 protocol::ConnectionToHost::State initializing = | |
58 protocol::ConnectionToHost::State::INITIALIZING; | |
59 protocol::ConnectionToHost::State current_state = initializing; | |
60 | |
61 const char kStateChangeTitleFormatString[] = "%-35s%-15s"; | |
62 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, | |
63 "State to State", "Delta Time"); | |
64 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, | |
65 "--------------", "----------"); | |
66 | |
67 std::vector<protocol::ConnectionToHost::State> list_of_states; | |
joedow
2015/07/27 21:45:44
if you don't include 'closed' here, this should pr
tonychun
2015/07/28 17:53:45
Done.
| |
68 list_of_states.push_back(protocol::ConnectionToHost::State::CONNECTING); | |
69 list_of_states.push_back(protocol::ConnectionToHost::State::AUTHENTICATED); | |
70 list_of_states.push_back(protocol::ConnectionToHost::State::CONNECTED); | |
71 list_of_states.push_back(protocol::ConnectionToHost::State::FAILED); | |
joedow
2015/07/27 21:45:44
nit: a comment that the order of the list is impor
tonychun
2015/07/28 17:53:44
Done.
| |
72 | |
73 const char kStateChangeFormatString[] = "%-13s to %-18s%-7dms"; | |
74 auto iter_end = transition_times_map_.end(); | |
75 for (protocol::ConnectionToHost::State state : list_of_states) { | |
76 auto iter_state = transition_times_map_.find(state); | |
77 if (iter_state != iter_end) { | |
78 LOG(INFO) << base::StringPrintf(kStateChangeFormatString, | |
79 TestChromotingClient::ConnectionStateToFriendlyString(current_state), | |
80 TestChromotingClient::ConnectionStateToFriendlyString(state), | |
81 static_cast<int>(GetStateTransitionTime(current_state, | |
82 state).InMilliseconds())); | |
83 current_state = state; | |
84 } | |
85 } | |
86 | |
87 // |current state| will either be FAILED or CONNECTED. | |
88 LOG(INFO) << "Total Connection Duration (INITIALIZING to " | |
89 << TestChromotingClient::ConnectionStateToFriendlyString( | |
90 current_state) << "): " | |
91 << GetStateTransitionTime(initializing, | |
92 current_state).InMilliseconds() | |
93 << " ms"; | |
94 LOG(INFO) << "Total Connection Time (INITIALIZING to CLOSED): " | |
95 << GetStateTransitionTime( | |
96 initializing, | |
97 protocol::ConnectionToHost::State::CLOSED).InMilliseconds() | |
98 << " ms"; | |
joedow
2015/07/27 21:45:44
Do you want to make sure there is a closed state b
tonychun
2015/07/28 17:53:45
Removed. I will implement closing with the helper
| |
99 } | |
100 | |
101 base::TimeDelta ConnectionTimeObserver::GetStateTransitionTime( | |
102 protocol::ConnectionToHost::State start_state, | |
103 protocol::ConnectionToHost::State end_state) const { | |
104 auto iter_end = transition_times_map_.end(); | |
105 | |
106 auto iter_start_state = transition_times_map_.find(start_state); | |
107 if (iter_start_state == iter_end) { | |
108 LOG(ERROR) << "No time found for state " | |
joedow
2015/07/27 21:45:44
nit: I like to add a colon (or similar) to the end
tonychun
2015/07/28 17:53:45
Done.
| |
109 << TestChromotingClient::ConnectionStateToFriendlyString( | |
110 start_state); | |
111 return base::TimeDelta::Max(); | |
112 } | |
113 | |
114 auto iter_end_state = transition_times_map_.find(end_state); | |
115 if (iter_end_state == iter_end) { | |
116 LOG(ERROR) << "No time found for state " | |
117 << TestChromotingClient::ConnectionStateToFriendlyString( | |
118 end_state); | |
119 return base::TimeDelta::Max(); | |
120 } | |
121 | |
122 base::TimeDelta delta = iter_end_state->second - iter_start_state->second; | |
123 if (delta.InMilliseconds() < 0) { | |
124 LOG(ERROR) << "Transition delay is negative. Check the state ordering: " | |
125 << "start " | |
126 << TestChromotingClient::ConnectionStateToFriendlyString( | |
127 start_state) << ", end " | |
128 << TestChromotingClient::ConnectionStateToFriendlyString( | |
129 end_state); | |
130 return base::TimeDelta::Max(); | |
131 } | |
132 | |
133 return delta; | |
134 } | |
135 | |
136 } // namespace test | |
137 } // namespace remoting | |
OLD | NEW |