OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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/counter_stubs.h" | |
6 | |
7 #include "base/atomicops.h" | |
8 #include "base/callback.h" | |
9 #include "base/macros.h" | |
10 #include "base/time/time.h" | |
11 #include "remoting/proto/audio.pb.h" | |
12 #include "remoting/proto/control.pb.h" | |
13 #include "remoting/proto/video.pb.h" | |
14 | |
15 #ifndef ARCH_CPU_64_BITS | |
16 #include "base/synchronization/lock.h" | |
17 #endif | |
18 | |
19 namespace remoting { | |
20 namespace test { | |
21 namespace { | |
22 | |
23 template <typename T> | |
24 class NoBarrierAtomic { | |
25 public: | |
26 T operator++() { | |
27 return base::subtle::NoBarrier_AtomicIncrement(&i_, 1) - 1; | |
28 } | |
29 | |
30 T operator++(int) { | |
31 return base::subtle::NoBarrier_AtomicIncrement(&i_, 1); | |
32 } | |
33 | |
34 T operator--() { | |
35 return base::subtle::NoBarrier_AtomicIncrement(&i_, -1) - 1; | |
36 } | |
37 | |
38 T operator--(int) { | |
39 return base::subtle::NoBarrier_AtomicIncrement(&i_, -1); | |
40 } | |
41 | |
42 T operator+=(T other) { | |
43 return base::subtle::NoBarrier_AtomicIncrement(&i_, other); | |
44 } | |
45 | |
46 T operator-=(T other) { | |
47 return base::subtle::NoBarrier_AtomicIncrement(&i_, -other); | |
48 } | |
49 | |
50 T operator*() const { | |
51 return base::subtle::NoBarrier_Load(&i_); | |
52 } | |
53 | |
54 private: | |
55 volatile T i_; | |
56 }; | |
57 | |
58 class NoBarrierAtomicInt32 : public NoBarrierAtomic<base::subtle::Atomic32> {}; | |
59 #ifdef ARCH_CPU_64_BITS | |
60 class NoBarrierAtomicInt64 : public NoBarrierAtomic<base::subtle::Atomic64> {}; | |
61 #else // ifdef ARCH_CPU_64_BITS | |
62 | |
63 using base::AutoLock; | |
64 | |
65 // A barriered, lock based implementation | |
66 class NoBarrierAtomicInt64 { | |
67 public: | |
68 | |
69 int64_t operator++() { | |
70 AutoLock l(lock_); | |
71 return i_++; | |
72 } | |
73 | |
74 int64_t operator++(int) { | |
75 AutoLock l(lock_); | |
76 return ++i_; | |
77 } | |
78 | |
79 int64_t operator--() { | |
80 AutoLock l(lock_); | |
81 return i_--; | |
82 } | |
83 | |
84 int64_t operator--(int) { | |
85 AutoLock l(lock_); | |
86 return --i_; | |
87 } | |
88 | |
89 int64_t operator+=(int64_t other) { | |
90 AutoLock l(lock_); | |
91 return (i_ += other); | |
92 } | |
93 | |
94 int64_t operator-=(int64_t other) { | |
95 AutoLock l(lock_); | |
96 return (i_ -= other); | |
97 } | |
98 | |
99 int64_t operator*() const { | |
100 AutoLock l(lock_); | |
101 return i_; | |
102 } | |
103 | |
104 private: | |
105 volatile int64_t i_; | |
106 mutable base::Lock lock_; // field is used in operator*() const | |
107 }; | |
108 | |
109 #endif // ifdef ARCH_CPU_64_BITS | |
110 | |
111 class MessageCounter { | |
112 public: | |
113 MessageCounter() | |
114 : count_(), | |
115 size_(), | |
116 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
| |
117 start_time_(base::Time::Now()) {} | |
118 | |
119 int message_count() const { | |
120 return *count_; | |
121 } | |
122 | |
123 int64_t message_size() const { | |
124 return *size_; | |
125 } | |
126 | |
127 int last_message_size() const { | |
128 return last_size_; | |
129 } | |
130 | |
131 double DurationSeconds() const { | |
132 return (base::Time::Now() - start_time_).InSecondsF(); | |
133 } | |
134 | |
135 double MessagesPerSecond() const { | |
136 return static_cast<double>(message_count()) / DurationSeconds(); | |
137 } | |
138 double SizePerSecond() const { | |
139 return static_cast<double>(message_size()) / DurationSeconds(); | |
140 } | |
141 | |
142 double AverageMessageSize() const { | |
143 return static_cast<double>(message_size()) / message_count(); | |
144 } | |
145 | |
146 void LogMessage(const ::google::protobuf::MessageLite& message) { | |
147 count_++; | |
148 last_size_ = message.ByteSize(); | |
149 size_ += message.ByteSize(); | |
150 } | |
151 | |
152 private: | |
153 NoBarrierAtomicInt32 count_; | |
154 NoBarrierAtomicInt64 size_; | |
155 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.
| |
156 base::Time start_time_; | |
157 | |
158 DISALLOW_COPY_AND_ASSIGN(MessageCounter); | |
159 }; | |
160 | |
161 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.
| |
162 const char* name, | |
163 const MessageCounter& counter) { | |
164 os << name | |
165 << ": " | |
166 << counter.message_size() | |
167 << " bytes in " | |
168 << counter.message_count() | |
169 << " packages, last package " | |
170 << counter.last_message_size() | |
171 << " bytes, " | |
172 << counter.AverageMessageSize() | |
173 << " bytes/package, " | |
174 << counter.MessagesPerSecond() | |
175 << " packages/sec, " | |
176 << counter.SizePerSecond() | |
177 << " bytes/sec" | |
178 << std::endl; | |
179 } | |
180 | |
181 } // namespace | |
182 | |
183 class CounterStubs::CounterClientStub | |
184 : public protocol::ClientStub, public MessageCounter { | |
185 public: | |
186 void DeliverHostMessage(const protocol::ExtensionMessage& message) override { | |
187 LogMessage(message); | |
188 } | |
189 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override {} | |
190 void SetCapabilities(const protocol::Capabilities& capabilities) override {} | |
191 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override {} | |
192 void SetPairingResponse(const protocol::PairingResponse& response) override {} | |
193 void SetVideoLayout(const protocol::VideoLayout& video_layout) override {} | |
194 }; | |
195 | |
196 class CounterStubs::CounterHostStub | |
197 : public protocol::HostStub, public MessageCounter { | |
198 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.
| |
199 void ControlAudio(const protocol::AudioControl& audio_control) override {} | |
200 void ControlVideo(const protocol::VideoControl& video_control) override {} | |
201 void DeliverClientMessage( | |
202 const protocol::ExtensionMessage& message) override { | |
203 LogMessage(message); | |
204 } | |
205 void NotifyClientResolution( | |
206 const protocol::ClientResolution& resolution) override {} | |
207 void RequestPairing( | |
208 const protocol::PairingRequest& pairing_request) override {} | |
209 void SetCapabilities(const protocol::Capabilities& capabilities) override {} | |
210 }; | |
211 | |
212 class CounterStubs::CounterAudioStub | |
213 : public protocol::AudioStub, public MessageCounter { | |
214 public: | |
215 void ProcessAudioPacket(std::unique_ptr<AudioPacket> audio_packet, | |
216 const base::Closure& done) override { | |
217 if (audio_packet) { | |
218 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
| |
219 } | |
220 done.Run(); | |
221 } | |
222 }; | |
223 | |
224 class CounterStubs::CounterVideoStub | |
225 : public protocol::VideoStub, public MessageCounter { | |
226 public: | |
227 CounterVideoStub(protocol::FakeConnectionToClient* connection) | |
228 : connection_(connection) {} | |
229 | |
230 void ProcessVideoPacket(std::unique_ptr<VideoPacket> video_packet, | |
231 const base::Closure& done) override { | |
232 if (video_packet && video_packet->has_capture_overhead_time_ms()) { | |
233 // 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.
| |
234 if (connection_ && | |
235 connection_->video_feedback_stub()) { | |
236 std::unique_ptr<VideoAck> ack(new VideoAck()); | |
237 ack->set_frame_id(video_packet->frame_id()); | |
238 connection_->video_feedback_stub()->ProcessVideoAck(std::move(ack)); | |
239 } | |
240 LogMessage(*video_packet); | |
241 } | |
242 done.Run(); | |
243 } | |
244 | |
245 private: | |
246 protocol::FakeConnectionToClient* connection_ = nullptr; | |
247 }; | |
248 | |
249 CounterStubs::CounterStubs(protocol::FakeConnectionToClient* connection) | |
250 : client_stub_(new CounterClientStub()), | |
251 host_stub_(new CounterHostStub()), | |
252 audio_stub_(new CounterAudioStub()), | |
253 video_stub_(new CounterVideoStub(connection)) {} | |
254 | |
255 CounterStubs::~CounterStubs() {} | |
256 | |
257 protocol::ClientStub& CounterStubs::client_stub() { | |
258 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.
| |
259 } | |
260 | |
261 protocol::HostStub& CounterStubs::host_stub() { | |
262 return *host_stub_; | |
263 } | |
264 | |
265 protocol::AudioStub& CounterStubs::audio_stub() { | |
266 return *audio_stub_; | |
267 } | |
268 | |
269 protocol::VideoStub& CounterStubs::video_stub() { | |
270 return *video_stub_; | |
271 } | |
272 | |
273 std::ostream& operator<<(std::ostream& os, const CounterStubs& stubs) { | |
274 OutputCounter(os, "audio", *stubs.audio_stub_); | |
275 OutputCounter(os, "video", *stubs.video_stub_); | |
276 OutputCounter(os, "client", *stubs.client_stub_); | |
277 OutputCounter(os, "host", *stubs.host_stub_); | |
278 return os; | |
279 } | |
280 | |
281 } // namespace test | |
282 } // namespace remoting | |
OLD | NEW |