forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ConnectionTimeObserver to provide a way to calculate times betw…
…een the different stages. The ConnectionTimeObserver saves the state of the client and the time when the client state changes. The delta times can be accessed through GetStateTransitionDelay and a start and end state. BUG= Review URL: https://codereview.chromium.org/1238343002 Cr-Commit-Position: refs/heads/master@{#341232}
- Loading branch information
tonychun
authored and
Commit bot
committed
Jul 31, 2015
1 parent
bcc7961
commit 684d141
Showing
13 changed files
with
418 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "remoting/protocol/errors.h" | ||
|
||
#include "base/logging.h" | ||
|
||
namespace remoting { | ||
namespace protocol { | ||
|
||
#define RETURN_STRING_LITERAL(x) \ | ||
case x: \ | ||
return #x; | ||
|
||
const char* ErrorCodeToString(ErrorCode error) { | ||
switch (error) { | ||
RETURN_STRING_LITERAL(OK); | ||
RETURN_STRING_LITERAL(PEER_IS_OFFLINE); | ||
RETURN_STRING_LITERAL(SESSION_REJECTED); | ||
RETURN_STRING_LITERAL(INCOMPATIBLE_PROTOCOL); | ||
RETURN_STRING_LITERAL(AUTHENTICATION_FAILED); | ||
RETURN_STRING_LITERAL(CHANNEL_CONNECTION_ERROR); | ||
RETURN_STRING_LITERAL(SIGNALING_ERROR); | ||
RETURN_STRING_LITERAL(SIGNALING_TIMEOUT); | ||
RETURN_STRING_LITERAL(HOST_OVERLOAD); | ||
RETURN_STRING_LITERAL(UNKNOWN_ERROR); | ||
} | ||
NOTREACHED(); | ||
return nullptr; | ||
} | ||
|
||
} // namespace protocol | ||
} // namespace remoting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "remoting/test/connection_time_observer.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/strings/stringprintf.h" | ||
#include "base/time/time.h" | ||
#include "base/timer/timer.h" | ||
|
||
namespace remoting { | ||
namespace test { | ||
|
||
ConnectionTimeObserver::ConnectionTimeObserver() { | ||
} | ||
|
||
ConnectionTimeObserver::~ConnectionTimeObserver() { | ||
} | ||
|
||
void ConnectionTimeObserver::SetTransitionTimesMapForTest( | ||
const std::map<protocol::ConnectionToHost::State, base::TimeTicks>& map) { | ||
transition_times_map_ = map; | ||
} | ||
|
||
void ConnectionTimeObserver::ConnectionStateChanged( | ||
protocol::ConnectionToHost::State state, | ||
protocol::ErrorCode error_code) { | ||
if (transition_times_map_.find(state) != transition_times_map_.end()) { | ||
std::string connection_state = | ||
protocol::ConnectionToHost::StateToString(state); | ||
LOG(ERROR) << connection_state << " state has already been set"; | ||
return; | ||
} | ||
transition_times_map_.insert(std::make_pair(state, base::TimeTicks::Now())); | ||
current_connection_state_ = state; | ||
} | ||
|
||
void ConnectionTimeObserver::DisplayConnectionStats() const { | ||
protocol::ConnectionToHost::State initializing = | ||
protocol::ConnectionToHost::State::INITIALIZING; | ||
protocol::ConnectionToHost::State current_state = initializing; | ||
|
||
const char kStateChangeTitleFormatString[] = "%-35s%-15s"; | ||
LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, | ||
"State to State", "Delta Time"); | ||
LOG(INFO) << base::StringPrintf(kStateChangeTitleFormatString, | ||
"--------------", "----------"); | ||
|
||
// Note: the order of |connected_states| mimics the expected order of when a | ||
// connection is made. | ||
std::vector<protocol::ConnectionToHost::State> connected_states; | ||
connected_states.push_back(protocol::ConnectionToHost::State::CONNECTING); | ||
connected_states.push_back(protocol::ConnectionToHost::State::AUTHENTICATED); | ||
connected_states.push_back(protocol::ConnectionToHost::State::CONNECTED); | ||
connected_states.push_back(protocol::ConnectionToHost::State::FAILED); | ||
|
||
const char kStateChangeFormatString[] = "%-13s to %-18s%-7dms"; | ||
auto iter_end = transition_times_map_.end(); | ||
for (protocol::ConnectionToHost::State state : connected_states) { | ||
auto iter_state = transition_times_map_.find(state); | ||
if (iter_state != iter_end) { | ||
int state_transition_time = | ||
GetStateTransitionTime(current_state, state).InMilliseconds(); | ||
LOG(INFO) << base::StringPrintf(kStateChangeFormatString, | ||
protocol::ConnectionToHost::StateToString(current_state), | ||
protocol::ConnectionToHost::StateToString(state), | ||
state_transition_time); | ||
current_state = state; | ||
} | ||
} | ||
|
||
int connected_time = | ||
GetStateTransitionTime(initializing, current_state).InMilliseconds(); | ||
|
||
// |current state| will either be FAILED or CONNECTED. | ||
LOG(INFO) << "Total Connection Duration (INITIALIZING to " | ||
<< protocol::ConnectionToHost::StateToString(current_state) << "): " | ||
<< connected_time << " ms"; | ||
} | ||
|
||
base::TimeDelta ConnectionTimeObserver::GetStateTransitionTime( | ||
protocol::ConnectionToHost::State start, | ||
protocol::ConnectionToHost::State end) const { | ||
auto iter_end = transition_times_map_.end(); | ||
|
||
auto iter_start_state = transition_times_map_.find(start); | ||
std::string start_state = protocol::ConnectionToHost::StateToString(start); | ||
if (iter_start_state == iter_end) { | ||
LOG(ERROR) << "No time found for state: " << start_state; | ||
return base::TimeDelta::Max(); | ||
} | ||
|
||
auto iter_end_state = transition_times_map_.find(end); | ||
std::string end_state = protocol::ConnectionToHost::StateToString(end); | ||
if (iter_end_state == iter_end) { | ||
LOG(ERROR) << "No time found for state: " << end_state; | ||
return base::TimeDelta::Max(); | ||
} | ||
|
||
base::TimeDelta delta = iter_end_state->second - iter_start_state->second; | ||
if (delta.InMilliseconds() < 0) { | ||
LOG(ERROR) << "Transition delay is negative. Check the state ordering: " | ||
<< "[start: " << start_state << ", end: " << end_state << "]"; | ||
return base::TimeDelta::Max(); | ||
} | ||
|
||
return delta; | ||
} | ||
|
||
} // namespace test | ||
} // namespace remoting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2015 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef REMOTING_TEST_CONNECTION_TIME_OBSERVER_H_ | ||
#define REMOTING_TEST_CONNECTION_TIME_OBSERVER_H_ | ||
|
||
#include <map> | ||
|
||
#include "remoting/test/remote_connection_observer.h" | ||
|
||
namespace base { | ||
class TimeDelta; | ||
class Timer; | ||
} | ||
|
||
namespace remoting { | ||
namespace test { | ||
|
||
// Observes and saves the times when a chromoting client changes its state. It | ||
// allows for tests to access latency times between the different states the | ||
// client transitioned through. | ||
class ConnectionTimeObserver | ||
: public RemoteConnectionObserver { | ||
public: | ||
ConnectionTimeObserver(); | ||
~ConnectionTimeObserver() override; | ||
|
||
// RemoteConnectionObserver interface. | ||
void ConnectionStateChanged(protocol::ConnectionToHost::State state, | ||
protocol::ErrorCode error_code) override; | ||
|
||
// Prints out connection performance stats to STDOUT. | ||
void DisplayConnectionStats() const; | ||
|
||
// Returns the time delta in milliseconds between |start_state| and | ||
// |end_state| stored in |transition_times_map_|. | ||
base::TimeDelta GetStateTransitionTime( | ||
protocol::ConnectionToHost::State start, | ||
protocol::ConnectionToHost::State end) const; | ||
|
||
// Used to set fake state transition times for ConnectionTimeObserver tests. | ||
void SetTransitionTimesMapForTest( | ||
const std::map<protocol::ConnectionToHost::State, base::TimeTicks>& map); | ||
|
||
private: | ||
// Saves the current connection state of client to host. | ||
protocol::ConnectionToHost::State current_connection_state_ = | ||
protocol::ConnectionToHost::State::INITIALIZING; | ||
|
||
// The TimeTicks to get to a state from the previous state. | ||
std::map<protocol::ConnectionToHost::State, base::TimeTicks> | ||
transition_times_map_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserver); | ||
}; | ||
|
||
} // namespace test | ||
} // namespace remoting | ||
|
||
#endif // REMOTING_TEST_CONNECTION_TIME_OBSERVER_H_ |
Oops, something went wrong.