Chromium Code Reviews| Index: remoting/test/counter_stubs.cc |
| diff --git a/remoting/test/counter_stubs.cc b/remoting/test/counter_stubs.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..79fd4436f8ab9b13b5a378e32e731e51065cd2c4 |
| --- /dev/null |
| +++ b/remoting/test/counter_stubs.cc |
| @@ -0,0 +1,282 @@ |
| +// Copyright 2016 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/counter_stubs.h" |
| + |
| +#include "base/atomicops.h" |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/time/time.h" |
| +#include "remoting/proto/audio.pb.h" |
| +#include "remoting/proto/control.pb.h" |
| +#include "remoting/proto/video.pb.h" |
| + |
| +#ifndef ARCH_CPU_64_BITS |
| +#include "base/synchronization/lock.h" |
| +#endif |
| + |
| +namespace remoting { |
| +namespace test { |
| +namespace { |
| + |
| +template <typename T> |
| +class NoBarrierAtomic { |
| + public: |
| + T operator++() { |
| + return base::subtle::NoBarrier_AtomicIncrement(&i_, 1) - 1; |
| + } |
| + |
| + T operator++(int) { |
| + return base::subtle::NoBarrier_AtomicIncrement(&i_, 1); |
| + } |
| + |
| + T operator--() { |
| + return base::subtle::NoBarrier_AtomicIncrement(&i_, -1) - 1; |
| + } |
| + |
| + T operator--(int) { |
| + return base::subtle::NoBarrier_AtomicIncrement(&i_, -1); |
| + } |
| + |
| + T operator+=(T other) { |
| + return base::subtle::NoBarrier_AtomicIncrement(&i_, other); |
| + } |
| + |
| + T operator-=(T other) { |
| + return base::subtle::NoBarrier_AtomicIncrement(&i_, -other); |
| + } |
| + |
| + T operator*() const { |
| + return base::subtle::NoBarrier_Load(&i_); |
| + } |
| + |
| + private: |
| + volatile T i_; |
| +}; |
| + |
| +class NoBarrierAtomicInt32 : public NoBarrierAtomic<base::subtle::Atomic32> {}; |
| +#ifdef ARCH_CPU_64_BITS |
| +class NoBarrierAtomicInt64 : public NoBarrierAtomic<base::subtle::Atomic64> {}; |
| +#else // ifdef ARCH_CPU_64_BITS |
| + |
| +using base::AutoLock; |
| + |
| +// A barriered, lock based implementation |
| +class NoBarrierAtomicInt64 { |
| + public: |
| + |
| + int64_t operator++() { |
| + AutoLock l(lock_); |
| + return i_++; |
| + } |
| + |
| + int64_t operator++(int) { |
| + AutoLock l(lock_); |
| + return ++i_; |
| + } |
| + |
| + int64_t operator--() { |
| + AutoLock l(lock_); |
| + return i_--; |
| + } |
| + |
| + int64_t operator--(int) { |
| + AutoLock l(lock_); |
| + return --i_; |
| + } |
| + |
| + int64_t operator+=(int64_t other) { |
| + AutoLock l(lock_); |
| + return (i_ += other); |
| + } |
| + |
| + int64_t operator-=(int64_t other) { |
| + AutoLock l(lock_); |
| + return (i_ -= other); |
| + } |
| + |
| + int64_t operator*() const { |
| + AutoLock l(lock_); |
| + return i_; |
| + } |
| + |
| + private: |
| + volatile int64_t i_; |
| + mutable base::Lock lock_; // field is used in operator*() const |
| +}; |
| + |
| +#endif // ifdef ARCH_CPU_64_BITS |
| + |
| +class MessageCounter { |
| + public: |
| + MessageCounter() |
| + : count_(), |
| + size_(), |
| + last_size_(), |
|
joedow
2016/05/04 16:40:44
Why are these members in the init list? They shou
Hzj_jie
2016/05/04 19:18:03
I prefer to keep count_ and size_ initialized in t
|
| + start_time_(base::Time::Now()) {} |
| + |
| + int message_count() const { |
| + return *count_; |
| + } |
| + |
| + int64_t message_size() const { |
| + return *size_; |
| + } |
| + |
| + int last_message_size() const { |
| + return last_size_; |
| + } |
| + |
| + double DurationSeconds() const { |
| + return (base::Time::Now() - start_time_).InSecondsF(); |
| + } |
| + |
| + double MessagesPerSecond() const { |
| + return static_cast<double>(message_count()) / DurationSeconds(); |
| + } |
| + double SizePerSecond() const { |
| + return static_cast<double>(message_size()) / DurationSeconds(); |
| + } |
| + |
| + double AverageMessageSize() const { |
| + return static_cast<double>(message_size()) / message_count(); |
| + } |
| + |
| + void LogMessage(const ::google::protobuf::MessageLite& message) { |
| + count_++; |
| + last_size_ = message.ByteSize(); |
| + size_ += message.ByteSize(); |
| + } |
| + |
| + private: |
| + NoBarrierAtomicInt32 count_; |
| + NoBarrierAtomicInt64 size_; |
| + int last_size_; |
|
joedow
2016/05/04 16:40:44
Init basic types inline, not in the initializer li
Hzj_jie
2016/05/04 19:18:03
Done.
|
| + base::Time start_time_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MessageCounter); |
| +}; |
| + |
| +void OutputCounter(std::ostream& os, |
|
joedow
2016/05/04 16:40:44
DisplayStatistics?
Why isn't this part of the Mes
Hzj_jie
2016/05/04 19:18:04
Done.
|
| + const char* name, |
| + const MessageCounter& counter) { |
| + os << name |
| + << ": " |
| + << counter.message_size() |
| + << " bytes in " |
| + << counter.message_count() |
| + << " packages, last package " |
| + << counter.last_message_size() |
| + << " bytes, " |
| + << counter.AverageMessageSize() |
| + << " bytes/package, " |
| + << counter.MessagesPerSecond() |
| + << " packages/sec, " |
| + << counter.SizePerSecond() |
| + << " bytes/sec" |
| + << std::endl; |
| +} |
| + |
| +} // namespace |
| + |
| +class CounterStubs::CounterClientStub |
| + : public protocol::ClientStub, public MessageCounter { |
| + public: |
| + void DeliverHostMessage(const protocol::ExtensionMessage& message) override { |
| + LogMessage(message); |
| + } |
| + void InjectClipboardEvent(const protocol::ClipboardEvent& event) override {} |
| + void SetCapabilities(const protocol::Capabilities& capabilities) override {} |
| + void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override {} |
| + void SetPairingResponse(const protocol::PairingResponse& response) override {} |
| + void SetVideoLayout(const protocol::VideoLayout& video_layout) override {} |
| +}; |
| + |
| +class CounterStubs::CounterHostStub |
| + : public protocol::HostStub, public MessageCounter { |
| + public: |
|
joedow
2016/05/04 16:40:44
Add a comment to note which methods belong to the
Hzj_jie
2016/05/04 19:18:04
Done.
|
| + void ControlAudio(const protocol::AudioControl& audio_control) override {} |
| + void ControlVideo(const protocol::VideoControl& video_control) override {} |
| + void DeliverClientMessage( |
| + const protocol::ExtensionMessage& message) override { |
| + LogMessage(message); |
| + } |
| + void NotifyClientResolution( |
| + const protocol::ClientResolution& resolution) override {} |
| + void RequestPairing( |
| + const protocol::PairingRequest& pairing_request) override {} |
| + void SetCapabilities(const protocol::Capabilities& capabilities) override {} |
| +}; |
| + |
| +class CounterStubs::CounterAudioStub |
| + : public protocol::AudioStub, public MessageCounter { |
| + public: |
| + void ProcessAudioPacket(std::unique_ptr<AudioPacket> audio_packet, |
| + const base::Closure& done) override { |
| + if (audio_packet) { |
| + LogMessage(*audio_packet); |
|
joedow
2016/05/04 16:40:44
Since you are using unique_ptr, I think it wuold b
Hzj_jie
2016/05/04 19:18:03
Then I will need to pass a pointer of MessageLite
|
| + } |
| + done.Run(); |
| + } |
| +}; |
| + |
| +class CounterStubs::CounterVideoStub |
| + : public protocol::VideoStub, public MessageCounter { |
| + public: |
| + CounterVideoStub(protocol::FakeConnectionToClient* connection) |
| + : connection_(connection) {} |
| + |
| + void ProcessVideoPacket(std::unique_ptr<VideoPacket> video_packet, |
| + const base::Closure& done) override { |
| + if (video_packet && video_packet->has_capture_overhead_time_ms()) { |
| + // Not a keepalive packet |
|
joedow
2016/05/04 16:40:44
comments should end with a period.
Hzj_jie
2016/05/04 19:18:03
Done.
|
| + if (connection_ && |
| + connection_->video_feedback_stub()) { |
| + std::unique_ptr<VideoAck> ack(new VideoAck()); |
| + ack->set_frame_id(video_packet->frame_id()); |
| + connection_->video_feedback_stub()->ProcessVideoAck(std::move(ack)); |
| + } |
| + LogMessage(*video_packet); |
| + } |
| + done.Run(); |
| + } |
| + |
| + private: |
| + protocol::FakeConnectionToClient* connection_ = nullptr; |
| +}; |
| + |
| +CounterStubs::CounterStubs(protocol::FakeConnectionToClient* connection) |
| + : client_stub_(new CounterClientStub()), |
| + host_stub_(new CounterHostStub()), |
| + audio_stub_(new CounterAudioStub()), |
| + video_stub_(new CounterVideoStub(connection)) {} |
| + |
| +CounterStubs::~CounterStubs() {} |
| + |
| +protocol::ClientStub& CounterStubs::client_stub() { |
| + return *client_stub_; |
|
joedow
2016/05/04 16:40:44
client_stub_.get()...alternatively, you could retu
Hzj_jie
2016/05/04 19:18:04
Done.
|
| +} |
| + |
| +protocol::HostStub& CounterStubs::host_stub() { |
| + return *host_stub_; |
| +} |
| + |
| +protocol::AudioStub& CounterStubs::audio_stub() { |
| + return *audio_stub_; |
| +} |
| + |
| +protocol::VideoStub& CounterStubs::video_stub() { |
| + return *video_stub_; |
| +} |
| + |
| +std::ostream& operator<<(std::ostream& os, const CounterStubs& stubs) { |
| + OutputCounter(os, "audio", *stubs.audio_stub_); |
| + OutputCounter(os, "video", *stubs.video_stub_); |
| + OutputCounter(os, "client", *stubs.client_stub_); |
| + OutputCounter(os, "host", *stubs.host_stub_); |
| + return os; |
| +} |
| + |
| +} // namespace test |
| +} // namespace remoting |