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

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: Added unittest, display connection stats ability, and error check for transition delay. Created 5 years, 5 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
12 namespace remoting {
13 namespace test {
14
15 std::string GetStateAsString(const protocol::ConnectionToHost::State& state) {
joedow 2015/07/24 13:38:28 no need for const ref on the state param.
tonychun 2015/07/27 16:03:37 Done.
16 switch (state) {
17 case protocol::ConnectionToHost::State::INITIALIZING:
18 return "INITIALIZING";
19 case protocol::ConnectionToHost::State::CONNECTING:
20 return "CONNECTING";
21 case protocol::ConnectionToHost::State::AUTHENTICATED:
22 return "AUTHENTICATED";
23 case protocol::ConnectionToHost::State::CONNECTED:
24 return "CONNECTED";
25 case protocol::ConnectionToHost::State::FAILED:
26 return "FAILED";
27 case protocol::ConnectionToHost::State::CLOSED:
28 return "CLOSED";
29 default:
30 LOG(ERROR) << "Unknown state: " << state;
31 NOTREACHED();
32 return std::string();
33 }
34 }
35
36 ConnectionTimeObserver::ConnectionTimeObserver() {
37 }
38
39 ConnectionTimeObserver::~ConnectionTimeObserver() {
40 }
41
42 void ConnectionTimeObserver::SetTransitionTimesMapForTest(
43 const std::map<protocol::ConnectionToHost::State, base::TimeTicks>& map) {
44 transition_times_map_ = map;
45 }
46
47 void ConnectionTimeObserver::ConnectionStateChanged(
48 protocol::ConnectionToHost::State state,
49 protocol::ErrorCode error_code) {
50 transition_times_map_.insert(std::make_pair(state, base::TimeTicks::Now()));
joedow 2015/07/24 13:38:28 Seems like you would want to protect against addin
tonychun 2015/07/27 16:03:37 Done.
51 current_connection_state_ = state;
52 }
53
54 void ConnectionTimeObserver::DisplayConnectionStats() {
55 protocol::ConnectionToHost::State initializing =
56 protocol::ConnectionToHost::State::INITIALIZING;
57 protocol::ConnectionToHost::State current_state = initializing;
58
59 const char kStateChangeTitleFormatString[] = "%-20s%-15s%-15s";
60 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString,
61 "State", "Total Time", "Delta Time");
62 LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString,
63 "-----", "----------", "----------");
64
65 std::vector<protocol::ConnectionToHost::State> list_of_states;
66 list_of_states.push_back(initializing);
67 list_of_states.push_back(protocol::ConnectionToHost::State::CONNECTING);
68 list_of_states.push_back(protocol::ConnectionToHost::State::AUTHENTICATED);
69 list_of_states.push_back(protocol::ConnectionToHost::State::CONNECTED);
70 list_of_states.push_back(protocol::ConnectionToHost::State::FAILED);
71 list_of_states.push_back(protocol::ConnectionToHost::State::CLOSED);
72
73 const char kStateChangeFormatString[] = "%-20s%-7ld%-8s%-7ld%-8s";
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 GetStateAsString(state).c_str(),
80 GetStateTransitionDelay(initializing, state).InMilliseconds(), "ms",
81 GetStateTransitionDelay(current_state, state).InMilliseconds(), "ms");
joedow 2015/07/24 13:38:28 This loop prints the difference between initializi
tonychun 2015/07/27 16:03:37 I have added these suggestions. However, I don't t
82 current_state = state;
83 }
84 }
85 }
86
87 base::TimeDelta ConnectionTimeObserver::GetStateTransitionDelay(
88 const protocol::ConnectionToHost::State& start_state,
89 const protocol::ConnectionToHost::State& end_state) const {
90 auto iter_end = transition_times_map_.end();
91
92 auto iter_start_state = transition_times_map_.find(start_state);
93 if (iter_start_state == iter_end) {
94 LOG(ERROR) << "No time found for state " << GetStateAsString(start_state);
95 return base::TimeDelta::Max();
96 }
97
98 auto iter_end_state = transition_times_map_.find(end_state);
99 if (iter_end_state == iter_end) {
100 LOG(ERROR) << "No time found for state " << GetStateAsString(end_state);
101 return base::TimeDelta::Max();
102 }
103
104 base::TimeDelta delta = iter_end_state->second - iter_start_state->second;
105 if (delta.InMilliseconds() < 0) {
106 LOG(ERROR) << "Transition delay is negative. Did you switch the states?";
joedow 2015/07/24 13:38:28 nit: 'Did you switch the states?' is a little stro
tonychun 2015/07/27 16:03:37 Done.
107 return base::TimeDelta::Max();
108 }
109
110 return delta;
111 }
112
113 } // namespace test
114 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698