| OLD | NEW |
| 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 | 26 |
| 26 namespace chrome_browser_net { | 27 namespace chrome_browser_net { |
| 27 | 28 |
| 28 // This class is used for live experiment of network connectivity (either TCP or | 29 // This class is used for live experiment of network connectivity (either TCP or |
| 29 // UDP) metrics. A small percentage of users participate in this experiment. All | 30 // UDP) metrics. A small percentage of users participate in this experiment. All |
| 30 // users (who are in the experiment) must have enabled "UMA upload". | 31 // users (who are in the experiment) must have enabled "UMA upload". |
| 31 // | 32 // |
| 32 // This class collects the following stats from users who have opted in. | 33 // This class collects the following stats from users who have opted in. |
| 33 // a) What percentage of users can get a message end-to-end to a UDP server? | 34 // a) What percentage of users can get a message end-to-end to a UDP server? |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // Starts the client, connecting to |server|. | 94 // Starts the client, connecting to |server|. |
| 94 // Client will send |bytes_to_send| bytes to |server|. | 95 // Client will send |bytes_to_send| bytes to |server|. |
| 95 // When client has received all echoed bytes from the server, or | 96 // When client has received all echoed bytes from the server, or |
| 96 // when an error occurs causing the client to stop, |Finish| will be | 97 // when an error occurs causing the client to stop, |Finish| will be |
| 97 // called with a net status code. | 98 // called with a net status code. |
| 98 // |Finish| will collect histogram stats. | 99 // |Finish| will collect histogram stats. |
| 99 // Returns true if successful in starting the client. | 100 // Returns true if successful in starting the client. |
| 100 bool Start(net::HostResolver* host_resolver, | 101 bool Start(net::HostResolver* host_resolver, |
| 101 const net::HostPortPair& server, | 102 const net::HostPortPair& server, |
| 102 HistogramPortSelector histogram_port, | 103 HistogramPortSelector histogram_port, |
| 104 bool has_no_proxy_server, |
| 103 uint32 bytes_to_send, | 105 uint32 bytes_to_send, |
| 104 const net::CompletionCallback& callback); | 106 const net::CompletionCallback& callback); |
| 105 | 107 |
| 106 protected: | 108 protected: |
| 107 // Constructs an NetworkStats object that collects metrics for network | 109 // Constructs an NetworkStats object that collects metrics for network |
| 108 // connectivity (either TCP or UDP). | 110 // connectivity (either TCP or UDP). |
| 109 NetworkStats(); | 111 NetworkStats(); |
| 110 virtual ~NetworkStats(); | 112 virtual ~NetworkStats(); |
| 111 | 113 |
| 112 // Initializes |finished_callback_| and the number of bytes to send to the | 114 // Initializes |finished_callback_| and the number of bytes to send to the |
| 113 // server. |finished_callback| is called when we are done with the test. | 115 // server. |finished_callback| is called when we are done with the test. |
| 114 // |finished_callback| is mainly useful for unittests. | 116 // |finished_callback| is mainly useful for unittests. |
| 115 void Initialize(uint32 bytes_to_send, | 117 void Initialize(uint32 bytes_to_send, |
| 116 HistogramPortSelector histogram_port, | 118 HistogramPortSelector histogram_port, |
| 119 bool has_no_proxy_server, |
| 117 const net::CompletionCallback& finished_callback); | 120 const net::CompletionCallback& finished_callback); |
| 118 | 121 |
| 119 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement | 122 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement |
| 120 // this method. They create the socket and connect to the server. | 123 // this method. They create the socket and connect to the server. |
| 121 virtual bool DoConnect(int result) = 0; | 124 virtual bool DoConnect(int result) = 0; |
| 122 | 125 |
| 123 // This method is called after socket connection is completed. It will send | 126 // This method is called after socket connection is completed. It will send |
| 124 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully | 127 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully |
| 125 // sending data to the |server|, it calls ReadData() to read/verify the data | 128 // sending data to the |server|, it calls ReadData() to read/verify the data |
| 126 // from the |server|. Returns true if successful. | 129 // from the |server|. Returns true if successful. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 encoded_message_ = message; | 209 encoded_message_ = message; |
| 207 } | 210 } |
| 208 | 211 |
| 209 // This method parses the "echo response" message in the |encoded_message_| to | 212 // This method parses the "echo response" message in the |encoded_message_| to |
| 210 // verify that the <payload> is same as what we had sent in "echo request" | 213 // verify that the <payload> is same as what we had sent in "echo request" |
| 211 // message. It returns SUCCESS, if all the bytes are verified. | 214 // message. It returns SUCCESS, if all the bytes are verified. |
| 212 NetworkStats::Status VerifyBytes(); | 215 NetworkStats::Status VerifyBytes(); |
| 213 | 216 |
| 214 // Returns the histogram names for collecting network connectivity stats. | 217 // Returns the histogram names for collecting network connectivity stats. |
| 215 // This is called by RecordHistograms. It sets the histogram names in | 218 // This is called by RecordHistograms. It sets the histogram names in |
| 216 // |rtt_histogram_name| and |status_histogram_name|. | 219 // |rtt_histogram_name| and |status_histogram_name|. It always returns |
| 220 // "NetConnectivity.<protocol>.Status.<port>.<load_size>" as histogram name |
| 221 // for status histogram in |status_histogram_name|. |
| 217 // If |result| equals to net::OK, it returns | 222 // If |result| equals to net::OK, it returns |
| 218 // "NetConnectivity.<protocol>.Success.<port>.<load_size>.RTT" as histogram | 223 // "NetConnectivity.<protocol>.Success.<port>.<load_size>.RTT" as histogram |
| 219 // name for RTT histogram and | 224 // name in |rtt_histogram_name|. If |has_no_proxy_server| is true, it returns |
| 220 // "NetConnectivity.<protocol>.Status.<port>.<load_size>" as histogram name | 225 // "NetConnectivity.<protocol>.NoProxy.Success.<port>.<load_size>.RTT" as |
| 221 // for status histogram. |protocol| argument sets <protocol> in the histogram | 226 // histogram name for RTT histogram in |rtt_no_proxy_histogram_name| and |
| 222 // name. It would be either TCP or UDP. <port> is the string representation of | 227 // "NetConnectivity.<protocol>.NoProxy.Status.<port>.<load_size>" as histogram |
| 223 // |histogram_port|. |load_size| argument determines <load_size> in the | 228 // name for status histogram in |status_no_proxy_histogram_name|. |
| 224 // histogram name. It would be either 100B or 1K. | 229 // |protocol| argument sets <protocol> in the histogram name. It would be |
| 230 // either TCP or UDP. <port> is the string representation of |histogram_port|. |
| 231 // |load_size| argument determines <load_size> in the histogram name. It would |
| 232 // be either 100B or 1K. |
| 225 static void GetHistogramNames(const ProtocolValue& protocol, | 233 static void GetHistogramNames(const ProtocolValue& protocol, |
| 226 HistogramPortSelector histogram_port, | 234 HistogramPortSelector histogram_port, |
| 235 bool has_no_proxy_server, |
| 227 uint32 load_size, | 236 uint32 load_size, |
| 228 int result, | 237 int result, |
| 229 std::string* rtt_histogram_name, | 238 std::string* rtt_histogram_name, |
| 230 std::string* status_histogram_name); | 239 std::string* rtt_no_proxy_histogram_name, |
| 240 std::string* status_histogram_name, |
| 241 std::string* status_no_proxy_histogram_name); |
| 231 | 242 |
| 232 // The socket handle for this session. | 243 // The socket handle for this session. |
| 233 scoped_ptr<net::Socket> socket_; | 244 scoped_ptr<net::Socket> socket_; |
| 234 | 245 |
| 235 // The read buffer used to read data from the socket. | 246 // The read buffer used to read data from the socket. |
| 236 scoped_refptr<net::IOBuffer> read_buffer_; | 247 scoped_refptr<net::IOBuffer> read_buffer_; |
| 237 | 248 |
| 238 // The write buffer used to write data to the socket. | 249 // The write buffer used to write data to the socket. |
| 239 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | 250 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
| 240 | 251 |
| 241 // Some counters for the session. | 252 // Some counters for the session. |
| 242 uint32 load_size_; | 253 uint32 load_size_; |
| 243 uint32 bytes_to_read_; | 254 uint32 bytes_to_read_; |
| 244 uint32 bytes_to_send_; | 255 uint32 bytes_to_send_; |
| 245 | 256 |
| 246 // The encoded message read from the server. | 257 // The encoded message read from the server. |
| 247 std::string encoded_message_; | 258 std::string encoded_message_; |
| 248 | 259 |
| 249 // |stream_| is used to generate data to be sent to the server and it is also | 260 // |stream_| is used to generate data to be sent to the server and it is also |
| 250 // used to verify the data received from the server. | 261 // used to verify the data received from the server. |
| 251 net::TestDataStream stream_; | 262 net::TestDataStream stream_; |
| 252 | 263 |
| 253 // |histogram_port_| specifies the port for which we are testing the network | 264 // |histogram_port_| specifies the port for which we are testing the network |
| 254 // connectivity. | 265 // connectivity. |
| 255 HistogramPortSelector histogram_port_; | 266 HistogramPortSelector histogram_port_; |
| 256 | 267 |
| 268 // |has_no_proxy_server_| specifies if there is a proxy server or not. |
| 269 bool has_no_proxy_server_; |
| 270 |
| 257 // HostResolver fills out the |addresses_| after host resolution is completed. | 271 // HostResolver fills out the |addresses_| after host resolution is completed. |
| 258 net::AddressList addresses_; | 272 net::AddressList addresses_; |
| 259 | 273 |
| 260 // Callback to call when echo protocol is successefully finished or whenever | 274 // Callback to call when echo protocol is successefully finished or whenever |
| 261 // there is an error (this allows unittests to wait until echo protocol's | 275 // there is an error (this allows unittests to wait until echo protocol's |
| 262 // round trip is finished). | 276 // round trip is finished). |
| 263 net::CompletionCallback finished_callback_; | 277 net::CompletionCallback finished_callback_; |
| 264 | 278 |
| 265 // The time when the session was started. | 279 // The time when the session was started. |
| 266 base::TimeTicks start_time_; | 280 base::TimeTicks start_time_; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 // Collects stats for TCP connectivity. This is called when all the data from | 331 // Collects stats for TCP connectivity. This is called when all the data from |
| 318 // server is read or when there is a failure during connect/read/write. | 332 // server is read or when there is a failure during connect/read/write. |
| 319 virtual void Finish(Status status, int result) OVERRIDE; | 333 virtual void Finish(Status status, int result) OVERRIDE; |
| 320 | 334 |
| 321 private: | 335 private: |
| 322 // Callback that is called when connect is completed and calls | 336 // Callback that is called when connect is completed and calls |
| 323 // ConnectComplete() to start the echo protocol. | 337 // ConnectComplete() to start the echo protocol. |
| 324 void OnConnectComplete(int result); | 338 void OnConnectComplete(int result); |
| 325 }; | 339 }; |
| 326 | 340 |
| 341 class ProxyClient { |
| 342 public: |
| 343 // Constructs a ProxyClient object that resolves if access to |
| 344 // network_stats_server goes through a proxy server or not. |
| 345 ProxyClient(const std::string& network_stats_server, |
| 346 NetworkStats::HistogramPortSelector histogram_port, |
| 347 IOThread* io_thread); |
| 348 virtual ~ProxyClient(); |
| 349 |
| 350 // This method uses system ProxyService to resolve the proxy for the given |
| 351 // |network_stats_server| and |histogram_port|. |
| 352 void ResolveProxy(); |
| 353 |
| 354 // Collects the network connectivity stats for UDP and TCP protocols. |
| 355 void OnResolveProxyComplete(int result); |
| 356 |
| 357 private: |
| 358 // |network_stats_server_| specifies the TCP/UDP echo server name. |
| 359 std::string network_stats_server_; |
| 360 |
| 361 // |histogram_port_| specifies the port for which we are testing the network |
| 362 // connectivity. |
| 363 NetworkStats::HistogramPortSelector histogram_port_; |
| 364 |
| 365 // |proxy_info_| holds proxy information returned by ResolveProxy. |
| 366 net::ProxyInfo proxy_info_; |
| 367 |
| 368 // The IOThread for accessing global HostResolver to resolve |
| 369 // network_stats_server_ host and for accessing global system ProxyService. |
| 370 // |io_thread_| is accessed on IO thread and it is safe to access it on IO |
| 371 // thread. |
| 372 IOThread* io_thread_; |
| 373 }; |
| 374 |
| 327 // This collects the network connectivity stats for UDP and TCP for small | 375 // This collects the network connectivity stats for UDP and TCP for small |
| 328 // percentage of users who are participating in the experiment. All users must | 376 // percentage of users who are participating in the experiment. All users must |
| 329 // have enabled "UMA upload". This method gets called only if UMA upload to the | 377 // have enabled "UMA upload". This method gets called only if UMA upload to the |
| 330 // server has succeeded. | 378 // server has succeeded. |
| 331 void CollectNetworkStats(const std::string& network_stats_server_url, | 379 void CollectNetworkStats(const std::string& network_stats_server_url, |
| 332 IOThread* io_thread); | 380 IOThread* io_thread); |
| 333 | 381 |
| 334 } // namespace chrome_browser_net | 382 } // namespace chrome_browser_net |
| 335 | 383 |
| 336 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | 384 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| OLD | NEW |