Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: remoting/test/connection_time_observer.cc

Issue 1238343002: Added ConnectionTimeObserver to calculate the times to authenticate and connect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Modified display stats and added quit after connected. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 std::string GetStateAsString(protocol::ConnectionToHost::State state) {
anandc 2015/07/27 18:22:29 ConnectionStateToFriendlyString, as defined here:
tonychun 2015/07/27 21:20:42 Done.
17 switch (state) {
18 case protocol::ConnectionToHost::State::INITIALIZING:
19 return "INITIALIZING";
20 case protocol::ConnectionToHost::State::CONNECTING:
21 return "CONNECTING";
22 case protocol::ConnectionToHost::State::AUTHENTICATED:
23 return "AUTHENTICATED";
24 case protocol::ConnectionToHost::State::CONNECTED:
25 return "CONNECTED";
26 case protocol::ConnectionToHost::State::FAILED:
27 return "FAILED";
28 case protocol::ConnectionToHost::State::CLOSED:
29 return "CLOSED";
30 default:
31 LOG(ERROR) << "Unknown state: " << state;
32 NOTREACHED();
33 return std::string();
34 }
35 }
36
37 ConnectionTimeObserver::ConnectionTimeObserver(base::Timer* timer)
38 : timer_(timer) {
39 }
40
41 ConnectionTimeObserver::~ConnectionTimeObserver() {
42 }
43
44 void ConnectionTimeObserver::SetTransitionTimesMapForTest(
45 const std::map<protocol::ConnectionToHost::State, base::TimeTicks>& map) {
46 transition_times_map_ = map;
47 }
48
49 void ConnectionTimeObserver::SetInitializingState() {
50 if (!transition_times_map_.empty()) {
51 LOG(ERROR) << "INITIALIZING state is already initialized";
52 return;
53 }
54 transition_times_map_.insert(std::make_pair(
55 protocol::ConnectionToHost::State::INITIALIZING,
56 base::TimeTicks::Now()));
57 }
58
59 void ConnectionTimeObserver::ConnectionStateChanged(
60 protocol::ConnectionToHost::State state,
61 protocol::ErrorCode error_code) {
62 if (transition_times_map_.find(state) != transition_times_map_.end()) {
63 LOG(ERROR) << GetStateAsString(state) << " state is already initialized";
64 return;
65 }
66 transition_times_map_.insert(std::make_pair(state, base::TimeTicks::Now()));
67 current_connection_state_ = state;
68 }
69
70 void ConnectionTimeObserver::ConnectionReady(bool ready) {
71 // TODO(TonyChun): Currently, this performance check does not have any tests
72 // to run, so I will save the CLOSED state here. Normally, the CLOSED state
73 // will be saved when a test completes a chromoting connection to a host.
anandc 2015/07/27 18:22:29 Do you mean "when a test terminates a Chromoting c
tonychun 2015/07/27 21:20:42 Done.
74 transition_times_map_.insert(
75 std::make_pair(protocol::ConnectionToHost::State::CLOSED,
76 base::TimeTicks::Now()));
77 timer_->user_task().Run();
joedow 2015/07/27 16:25:39 If you are trying to get to the QuitClosure() to s
tonychun 2015/07/27 21:20:42 I will remove this and create a helper class to ha
78 }
79
80 void ConnectionTimeObserver::DisplayConnectionStats() const {
81 protocol::ConnectionToHost::State initializing =
82 protocol::ConnectionToHost::State::INITIALIZING;
83 protocol::ConnectionToHost::State current_state = initializing;
84
85 const char kStateChangeTitleFormatString[] = "%-35s%-15s";
86 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString,
87 "State to State", "Delta Time");
88 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString,
89 "--------------", "----------");
90
91 std::vector<protocol::ConnectionToHost::State> list_of_states;
92 list_of_states.push_back(protocol::ConnectionToHost::State::CONNECTING);
93 list_of_states.push_back(protocol::ConnectionToHost::State::AUTHENTICATED);
94 list_of_states.push_back(protocol::ConnectionToHost::State::CONNECTED);
95 list_of_states.push_back(protocol::ConnectionToHost::State::FAILED);
96
97 const char kStateChangeFormatString[] = "%-13s to %-18s%-7dms";
98 auto iter_end = transition_times_map_.end();
99 for (protocol::ConnectionToHost::State state : list_of_states) {
100 auto iter_state = transition_times_map_.find(state);
101 if (iter_state != iter_end) {
102 LOG(INFO) << base::StringPrintf(kStateChangeFormatString,
103 GetStateAsString(current_state).c_str(),
104 GetStateAsString(state).c_str(),
105 static_cast<int>(GetStateTransitionDelayTimeDelta(current_state,
106 state).InMilliseconds()));
107 current_state = state;
108 }
109 }
110
111 // |current state| will either be FAILED or CONNECTED.
112 LOG(INFO) << "Total Connection Duration (INITIALIZING to "
113 << GetStateAsString(current_state) << "): "
114 << GetStateTransitionDelayTimeDelta(initializing,
115 current_state).InMilliseconds()
116 << " ms";
117 LOG(INFO) << "Total Connection Time (INITIALIZING to CLOSED): "
118 << GetStateTransitionDelayTimeDelta(
119 initializing,
120 protocol::ConnectionToHost::State::CLOSED).InMilliseconds()
121 << " ms";
122 }
123
124 base::TimeDelta ConnectionTimeObserver::GetStateTransitionDelayTimeDelta(
125 protocol::ConnectionToHost::State start_state,
126 protocol::ConnectionToHost::State end_state) const {
127 auto iter_end = transition_times_map_.end();
128
129 auto iter_start_state = transition_times_map_.find(start_state);
130 if (iter_start_state == iter_end) {
131 LOG(ERROR) << "No time found for state " << GetStateAsString(start_state);
132 return base::TimeDelta::Max();
133 }
134
135 auto iter_end_state = transition_times_map_.find(end_state);
136 if (iter_end_state == iter_end) {
137 LOG(ERROR) << "No time found for state " << GetStateAsString(end_state);
138 return base::TimeDelta::Max();
139 }
140
141 base::TimeDelta delta = iter_end_state->second - iter_start_state->second;
142 if (delta.InMilliseconds() < 0) {
143 LOG(ERROR) << "Transition delay is negative. Check the state ordering: "
144 << "start " << GetStateAsString(start_state)
145 << ", end " << GetStateAsString(end_state);
146 return base::TimeDelta::Max();
147 }
148
149 return delta;
150 }
151
152 } // namespace test
153 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698