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

Side by Side Diff: chrome/browser/net/network_stats.h

Issue 10206035: NetConnectivity - Collect stats for TCP/UDP network (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/net/network_stats.cc » ('j') | chrome/browser/net/network_stats.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_ 5 #ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_
6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_ 6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/time.h" 15 #include "base/time.h"
16 #include "chrome/browser/io_thread.h" 16 #include "chrome/browser/io_thread.h"
17 #include "net/base/address_list.h" 17 #include "net/base/address_list.h"
18 #include "net/base/completion_callback.h" 18 #include "net/base/completion_callback.h"
19 #include "net/base/host_port_pair.h" 19 #include "net/base/host_port_pair.h"
20 #include "net/base/host_resolver.h" 20 #include "net/base/host_resolver.h"
21 #include "net/base/io_buffer.h" 21 #include "net/base/io_buffer.h"
22 #include "net/base/ip_endpoint.h" 22 #include "net/base/ip_endpoint.h"
23 #include "net/base/test_data_stream.h" 23 #include "net/base/test_data_stream.h"
24 #include "net/proxy/proxy_info.h"
24 #include "net/socket/socket.h" 25 #include "net/socket/socket.h"
25 namespace chrome_browser_net { 26 namespace chrome_browser_net {
26 27
27 // This class is used for live experiment of network connectivity (either TCP or 28 // This class is used for live experiment of network connectivity (either TCP or
28 // UDP) metrics. A small percentage of users participate in this experiment. All 29 // UDP) metrics. A small percentage of users participate in this experiment. All
29 // users (who are in the experiment) must have enabled "UMA upload". 30 // users (who are in the experiment) must have enabled "UMA upload".
30 // 31 //
31 // This class collects the following stats from users who have opted in. 32 // This class collects the following stats from users who have opted in.
32 // a) What percentage of users can get a message end-to-end to a UDP server? 33 // a) What percentage of users can get a message end-to-end to a UDP server?
33 // b) What percentage of users can get a message end-to-end to a TCP server? 34 // b) What percentage of users can get a message end-to-end to a TCP server?
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 // Starts the client, connecting to |server|. 95 // Starts the client, connecting to |server|.
95 // Client will send |bytes_to_send| bytes to |server|. 96 // Client will send |bytes_to_send| bytes to |server|.
96 // When client has received all echoed bytes from the server, or 97 // When client has received all echoed bytes from the server, or
97 // when an error occurs causing the client to stop, |Finish| will be 98 // when an error occurs causing the client to stop, |Finish| will be
98 // called with a net status code. 99 // called with a net status code.
99 // |Finish| will collect histogram stats. 100 // |Finish| will collect histogram stats.
100 // Returns true if successful in starting the client. 101 // Returns true if successful in starting the client.
101 bool Start(net::HostResolver* host_resolver, 102 bool Start(net::HostResolver* host_resolver,
102 const net::HostPortPair& server, 103 const net::HostPortPair& server,
103 HistogramPortSelector histogram_port, 104 HistogramPortSelector histogram_port,
105 bool has_proxy_server,
104 uint32 bytes_to_send, 106 uint32 bytes_to_send,
105 uint32 packets_to_send, 107 uint32 packets_to_send,
106 const net::CompletionCallback& callback); 108 const net::CompletionCallback& callback);
107 109
108 protected: 110 protected:
109 // Constructs an NetworkStats object that collects metrics for network 111 // Constructs an NetworkStats object that collects metrics for network
110 // connectivity (either TCP or UDP). 112 // connectivity (either TCP or UDP).
111 NetworkStats(); 113 NetworkStats();
114 // NetworkStats is deleted when Finish() is called.
112 virtual ~NetworkStats(); 115 virtual ~NetworkStats();
113 116
114 // Initializes |finished_callback_| and the number of bytes to send to the 117 // Initializes |finished_callback_| and the number of bytes to send to the
115 // server. |finished_callback| is called when we are done with the test. 118 // server. |finished_callback| is called when we are done with the test.
116 // |finished_callback| is mainly useful for unittests. 119 // |finished_callback| is mainly useful for unittests.
117 void Initialize(uint32 bytes_to_send, 120 void Initialize(uint32 bytes_to_send,
118 HistogramPortSelector histogram_port, 121 HistogramPortSelector histogram_port,
122 bool has_proxy_server,
119 uint32 packets_to_send, 123 uint32 packets_to_send,
120 const net::CompletionCallback& finished_callback); 124 const net::CompletionCallback& finished_callback);
121 125
122 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement 126 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement
123 // this method. They create the socket and connect to the server. 127 // this method. They create the socket and connect to the server.
124 virtual bool DoConnect(int result) = 0; 128 virtual bool DoConnect(int result) = 0;
125 129
126 // This method is called after socket connection is completed. It will start 130 // This method is called after socket connection is completed. It will start
127 // the process of sending packets to |server| by calling SendPacket(). Returns 131 // the process of sending packets to |server| by calling SendPacket(). Returns
128 // false if connection is not established (result is less than 0). 132 // false if connection is not established (result is less than 0).
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 223
220 // This method parses the "echo response" message in the |response| to verify 224 // This method parses the "echo response" message in the |response| to verify
221 // that the <payload> is same as what we had sent in "echo request" message. 225 // that the <payload> is same as what we had sent in "echo request" message.
222 // As it verifies the response in each packet, it also extracts the packet 226 // As it verifies the response in each packet, it also extracts the packet
223 // number, and records that said packet number responded. It returns SUCCESS, 227 // number, and records that said packet number responded. It returns SUCCESS,
224 // if all the bytes are verified. 228 // if all the bytes are verified.
225 NetworkStats::Status VerifyBytes(const std::string& response); 229 NetworkStats::Status VerifyBytes(const std::string& response);
226 230
227 // Returns the histogram names for collecting network connectivity stats. 231 // Returns the histogram names for collecting network connectivity stats.
228 // This is called by RecordHistograms. It sets the histogram names in 232 // This is called by RecordHistograms. It sets the histogram names in
229 // |rtt_histogram_name| and |status_histogram_name|. 233 // |rtt_histogram_name|, |status_histogram_name| and
230 // If |result| equals to net::OK, it returns 234 // |packet_loss_histogram_name| and the corresponding no_proxy versions if
235 // there is no proxy server. It returns
236 // "NetConnectivity.<protocol>.Status.<port>.<load_size>" as histogram name
237 // for status histogram in |status_histogram_name| and
238 // "NetConnectivity.<protocol>.PacketLoss.<port>.<load_size>" as histogram
239 // name for packet loss histogram. If |result| equals to net::OK, it returns
231 // "NetConnectivity.<protocol>.Success.<port>.<load_size>.RTT" as histogram 240 // "NetConnectivity.<protocol>.Success.<port>.<load_size>.RTT" as histogram
232 // name for RTT histogram, 241 // name in |rtt_histogram_name|. If |has_proxy_server| is false, it returns
233 // "NetConnectivity.<protocol>.Status.<port>.<load_size>" as histogram name 242 // "NetConnectivity.<protocol>.NoProxy.Success.<port>.<load_size>.RTT" as
234 // for status histogram and 243 // no proxy RTT histogram,
235 // "NetConnectivity.<protocol>.PacketLoss.<port>.<load_size>" as histogram 244 // "NetConnectivity.<protocol>.NoProxy.Status.<port>.<load_size>" as no proxy
jar (doing other things) 2012/05/03 16:41:34 I expect that you'll be most interesting in compar
ramant (doing other things) 2012/05/04 03:53:11 Done.
236 // name for packet loss histogram. 245 // status histogram and
246 // "NetConnectivity.<protocol>.NoProxy.PacketLoss.<port>.<load_size>" as no
247 // proxy packet loss histogram.
237 // |protocol| argument sets <protocol> in the histogram name. It would be 248 // |protocol| argument sets <protocol> in the histogram name. It would be
238 // either TCP or UDP. <port> is the string representation of |histogram_port|. 249 // either TCP or UDP. <port> is the string representation of |histogram_port|.
239 // |load_size| argument determines <load_size> in the histogram name. It would 250 // |load_size| argument determines <load_size> in the histogram name. It would
240 // be either 100B or 1K. 251 // be either 100B or 1K.
241 static void GetHistogramNames(const ProtocolValue& protocol, 252 static void GetHistogramNames(
242 HistogramPortSelector histogram_port, 253 const ProtocolValue& protocol,
243 uint32 load_size, 254 HistogramPortSelector histogram_port,
244 int result, 255 bool has_proxy_server,
245 std::string* rtt_histogram_name, 256 uint32 load_size,
246 std::string* status_histogram_name, 257 int result,
247 std::string* packet_loss_histogram_name); 258 std::string* rtt_histogram_name,
259 std::string* rtt_no_proxy_histogram_name,
260 std::string* status_histogram_name,
261 std::string* status_no_proxy_histogram_name,
262 std::string* packet_loss_histogram_name,
263 std::string* packet_loss_no_proxy_histogram_name);
248 264
249 // The socket handle for this session. 265 // The socket handle for this session.
250 scoped_ptr<net::Socket> socket_; 266 scoped_ptr<net::Socket> socket_;
251 267
252 // The read buffer used to read data from the socket. 268 // The read buffer used to read data from the socket.
253 scoped_refptr<net::IOBuffer> read_buffer_; 269 scoped_refptr<net::IOBuffer> read_buffer_;
254 270
255 // The write buffer used to write data to the socket. 271 // The write buffer used to write data to the socket.
256 scoped_refptr<net::DrainableIOBuffer> write_buffer_; 272 scoped_refptr<net::DrainableIOBuffer> write_buffer_;
257 273
258 // Some counters for the session. 274 // Some counters for the session.
259 uint32 load_size_; 275 uint32 load_size_;
260 uint32 bytes_to_read_; 276 uint32 bytes_to_read_;
261 uint32 bytes_to_send_; 277 uint32 bytes_to_send_;
262 278
263 // The encoded message read from the server. 279 // The encoded message read from the server.
264 std::string encoded_message_; 280 std::string encoded_message_;
265 281
266 // |stream_| is used to generate data to be sent to the server and it is also 282 // |stream_| is used to generate data to be sent to the server and it is also
267 // used to verify the data received from the server. 283 // used to verify the data received from the server.
268 net::TestDataStream stream_; 284 net::TestDataStream stream_;
269 285
270 // |histogram_port_| specifies the port for which we are testing the network 286 // |histogram_port_| specifies the port for which we are testing the network
271 // connectivity. 287 // connectivity.
272 HistogramPortSelector histogram_port_; 288 HistogramPortSelector histogram_port_;
273 289
290 // |has_proxy_server_| specifies if there is a proxy server or not.
291 bool has_proxy_server_;
292
274 // HostResolver fills out the |addresses_| after host resolution is completed. 293 // HostResolver fills out the |addresses_| after host resolution is completed.
275 net::AddressList addresses_; 294 net::AddressList addresses_;
276 295
277 // Callback to call when echo protocol is successefully finished or whenever 296 // Callback to call when echo protocol is successefully finished or whenever
278 // there is an error (this allows unittests to wait until echo protocol's 297 // there is an error (this allows unittests to wait until echo protocol's
279 // round trip is finished). 298 // round trip is finished).
280 net::CompletionCallback finished_callback_; 299 net::CompletionCallback finished_callback_;
281 300
282 // The time when the session was started. 301 // The time when the session was started.
283 base::TimeTicks start_time_; 302 base::TimeTicks start_time_;
284 303
285 // Data to track number of packets to send to the server and the packets we 304 // Data to track number of packets to send to the server and the packets we
286 // have received from the server. 305 // have received from the server.
287 uint32 packets_to_send_; 306 uint32 packets_to_send_;
288 uint32 packets_sent_; 307 uint32 packets_sent_;
289 uint32 base_packet_number_; 308 uint32 base_packet_number_;
290 uint32 packets_received_mask_; 309 uint32 packets_received_mask_;
291 310
292 // We use this factory to create timeout tasks for socket's ReadData. 311 // We use this factory to create timeout tasks for socket's ReadData.
293 base::WeakPtrFactory<NetworkStats> weak_factory_; 312 base::WeakPtrFactory<NetworkStats> weak_factory_;
294 }; 313 };
295 314
296 class UDPStatsClient : public NetworkStats { 315 class UDPStatsClient : public NetworkStats {
297 public: 316 public:
298 // Constructs an UDPStatsClient object that collects metrics for UDP 317 // Constructs an UDPStatsClient object that collects metrics for UDP
299 // connectivity. 318 // connectivity.
300 UDPStatsClient(); 319 UDPStatsClient();
320 // UDPStatsClient is deleted when Finish() is called.
301 virtual ~UDPStatsClient(); 321 virtual ~UDPStatsClient();
302 322
303 protected: 323 protected:
304 // Allow tests to access our innards for testing purposes. 324 // Allow tests to access our innards for testing purposes.
305 friend class NetworkStatsTestUDP; 325 friend class NetworkStatsTestUDP;
306 326
307 // Called after host is resolved. Creates UDClientSocket and connects to the 327 // Called after host is resolved. Creates UDClientSocket and connects to the
308 // server. If successfully connected, then calls ConnectComplete() to start 328 // server. If successfully connected, then calls ConnectComplete() to start
309 // the echo protocol. Returns |false| if there is any error. 329 // the echo protocol. Returns |false| if there is any error.
310 virtual bool DoConnect(int result) OVERRIDE; 330 virtual bool DoConnect(int result) OVERRIDE;
311 331
312 // This method calls NetworkStats::ReadComplete() to verify the data and calls 332 // This method calls NetworkStats::ReadComplete() to verify the data and calls
313 // Finish() if there is an error or if read callback didn't return any data 333 // Finish() if there is an error or if read callback didn't return any data
314 // (|result| is less than or equal to 0). 334 // (|result| is less than or equal to 0).
315 virtual bool ReadComplete(int result) OVERRIDE; 335 virtual bool ReadComplete(int result) OVERRIDE;
316 336
317 // Collects stats for UDP connectivity. This is called when all the data from 337 // Collects stats for UDP connectivity. This is called when all the data from
318 // server is read or when there is a failure during connect/read/write. 338 // server is read or when there is a failure during connect/read/write. This
339 // object is deleted at the end of this method.
319 virtual void Finish(Status status, int result) OVERRIDE; 340 virtual void Finish(Status status, int result) OVERRIDE;
320 }; 341 };
321 342
322 class TCPStatsClient : public NetworkStats { 343 class TCPStatsClient : public NetworkStats {
323 public: 344 public:
324 // Constructs a TCPStatsClient object that collects metrics for TCP 345 // Constructs a TCPStatsClient object that collects metrics for TCP
325 // connectivity. 346 // connectivity.
326 TCPStatsClient(); 347 TCPStatsClient();
348 // TCPStatsClient is deleted when Finish() is called.
327 virtual ~TCPStatsClient(); 349 virtual ~TCPStatsClient();
328 350
329 protected: 351 protected:
330 // Allow tests to access our innards for testing purposes. 352 // Allow tests to access our innards for testing purposes.
331 friend class NetworkStatsTestTCP; 353 friend class NetworkStatsTestTCP;
332 354
333 // Called after host is resolved. Creates TCPClientSocket and connects to the 355 // Called after host is resolved. Creates TCPClientSocket and connects to the
334 // server. 356 // server.
335 virtual bool DoConnect(int result) OVERRIDE; 357 virtual bool DoConnect(int result) OVERRIDE;
336 358
337 // This method calls NetworkStats::ReadComplete() to verify the data and calls 359 // This method calls NetworkStats::ReadComplete() to verify the data and calls
338 // Finish() if there is an error (|result| is less than 0). 360 // Finish() if there is an error (|result| is less than 0).
339 virtual bool ReadComplete(int result) OVERRIDE; 361 virtual bool ReadComplete(int result) OVERRIDE;
340 362
341 // Collects stats for TCP connectivity. This is called when all the data from 363 // Collects stats for TCP connectivity. This is called when all the data from
342 // server is read or when there is a failure during connect/read/write. 364 // server is read or when there is a failure during connect/read/write. This
365 // object is deleted at the end of this method.
343 virtual void Finish(Status status, int result) OVERRIDE; 366 virtual void Finish(Status status, int result) OVERRIDE;
344 367
345 private: 368 private:
346 // Callback that is called when connect is completed and calls 369 // Callback that is called when connect is completed and calls
347 // ConnectComplete() to start the echo protocol. 370 // ConnectComplete() to start the echo protocol.
348 void OnConnectComplete(int result); 371 void OnConnectComplete(int result);
349 }; 372 };
350 373
374 class ProxyClient {
jar (doing other things) 2012/05/03 16:41:34 I'm surprised to see this much work. Is the inten
ramant (doing other things) 2012/05/04 03:53:11 No. This code to detect the proxy runs before we r
375 public:
376 // Constructs a ProxyClient object that resolves if access to
377 // network_stats_server goes through a proxy server or not.
378 ProxyClient(const std::string& network_stats_server,
379 NetworkStats::HistogramPortSelector histogram_port,
380 IOThread* io_thread);
381 virtual ~ProxyClient();
382
383 // This method uses system ProxyService to resolve the proxy for the given
384 // |network_stats_server| and |histogram_port|.
385 void ResolveProxy();
386
387 // Collects the network connectivity stats for UDP and TCP protocols.
388 void OnResolveProxyComplete(int result);
389
390 private:
391 // |network_stats_server_| specifies the TCP/UDP echo server name.
392 std::string network_stats_server_;
393
394 // |histogram_port_| specifies the port for which we are testing the network
395 // connectivity.
396 NetworkStats::HistogramPortSelector histogram_port_;
397
398 // |proxy_info_| holds proxy information returned by ResolveProxy.
399 net::ProxyInfo proxy_info_;
400
401 // The IOThread for accessing global HostResolver to resolve
402 // network_stats_server_ host and for accessing global system ProxyService.
403 // |io_thread_| is accessed on IO thread and it is safe to access it on IO
404 // thread.
405 IOThread* io_thread_;
406 };
407
351 // This collects the network connectivity stats for UDP and TCP for small 408 // This collects the network connectivity stats for UDP and TCP for small
352 // percentage of users who are participating in the experiment. All users must 409 // percentage of users who are participating in the experiment. All users must
353 // have enabled "UMA upload". This method gets called only if UMA upload to the 410 // have enabled "UMA upload". This method gets called only if UMA upload to the
354 // server has succeeded. 411 // server has succeeded.
355 void CollectNetworkStats(const std::string& network_stats_server_url, 412 void CollectNetworkStats(const std::string& network_stats_server_url,
356 IOThread* io_thread); 413 IOThread* io_thread);
357 414
358 } // namespace chrome_browser_net 415 } // namespace chrome_browser_net
359 416
360 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ 417 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/net/network_stats.cc » ('j') | chrome/browser/net/network_stats.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698