Created
August 5, 2013 20:48
-
-
Save PeterHajdu/6159490 to your computer and use it in GitHub Desktop.
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
class Rtt | |
{ | |
Rtt() | |
: m_index( 0 ) | |
{ | |
} | |
const std::string ping() | |
{ | |
Sequence temp( m_index++ ); | |
if ( temp >= N ) | |
{ | |
return std::string(); | |
} | |
std::cout << "ping: " << temp << std::endl; | |
m_started.insert( std::make_pair( temp, Clock::now() ) ); | |
return std::string( reinterpret_cast< const char* >( &temp ), sizeof( Sequence ) | |
); | |
} | |
void pong( const std::string& sequence ) | |
{ | |
if ( sequence.empty() ) | |
{ | |
return; | |
} | |
const Sequence seq( *reinterpret_cast< const Sequence* >( sequence.data() ) ); | |
std::cout << "pong: " << seq << std::endl; | |
assert( m_stopped.find( seq ) == m_stopped.end() ); | |
m_stopped.insert( std::make_pair( seq, Clock::now() ) ); | |
} | |
bool isDone() const | |
{ | |
return m_stopped.size() == N; | |
} | |
void report() const | |
{ | |
for ( Sequence i( 0 ); i < N; ++i ) | |
{ | |
TimepointContainer::const_iterator stopped( m_stopped.find( i ) ); | |
TimepointContainer::const_iterator started( m_started.find( i ) ); | |
assert( stopped != m_stopped.end() && started != m_started.end() ); | |
//std::cout << ( *stopped - *started ).count() << std::endl; | |
std::cout << "report: " << std::chrono::duration_cast< std::chrono::microsecon | |
ds >( stopped->second - started->second ).count() << std::endl; | |
} | |
} | |
private: | |
typedef std::unordered_map< Sequence, Clock::time_point > TimepointContainer; | |
std::unordered_map< Sequence, Clock::time_point > m_started; | |
std::unordered_map< Sequence, Clock::time_point > m_stopped; | |
Sequence m_index; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment