Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/time.h" | 15 #include "base/time.h" |
| 15 #include "chrome/browser/io_thread.h" | 16 #include "chrome/browser/io_thread.h" |
| 16 #include "net/base/address_list.h" | 17 #include "net/base/address_list.h" |
| 17 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
| 18 #include "net/base/host_port_pair.h" | 19 #include "net/base/host_port_pair.h" |
| 19 #include "net/base/host_resolver.h" | 20 #include "net/base/host_resolver.h" |
| 20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 21 #include "net/base/ip_endpoint.h" | 22 #include "net/base/ip_endpoint.h" |
| 22 #include "net/base/test_data_stream.h" | 23 #include "net/base/test_data_stream.h" |
| 23 #include "net/socket/socket.h" | 24 #include "net/socket/socket.h" |
| 24 | 25 |
| 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? |
| 34 // c) What is the latency for UDP and TCP. | 35 // c) What is the latency for UDP and TCP. |
| 35 // d) If connectivity failed, at what stage (Connect or Write or Read) did it | 36 // d) If connectivity failed, at what stage (Connect or Write or Read) did it |
| 36 // fail? | 37 // fail? |
| 38 // | |
| 39 // The following is the overview of the echo message protocol. | |
| 40 // | |
| 41 // We send the "echo request" to the TCP/UDP servers in the following format: | |
| 42 // <version><checksum><payload_size><payload>. <version> is the version number | |
| 43 // of the "echo request". <checksum> is the checksum of the <payload>. | |
| 44 // <payload_size> specifies the number of bytes in the <payload>. | |
| 45 // | |
| 46 // TCP/UDP servers respond to the "echo request" by returning "echo response". | |
| 47 // "echo response" is of the format: | |
| 48 // "<version><checksum><payload_size><key><encrypted_payload>". <payload_size> | |
| 49 // specifies the number of bytes in the <encrypted_payload>. <key> is used to | |
| 50 // decrypt the <encrypted_payload>. | |
| 51 | |
| 37 | 52 |
| 38 class NetworkStats { | 53 class NetworkStats { |
| 39 public: | 54 public: |
| 40 enum Status { // Used in HISTOGRAM_ENUMERATION. | 55 enum Status { // Used in HISTOGRAM_ENUMERATION. |
| 41 SUCCESS, // Successfully received bytes from the server. | 56 SUCCESS, // Successfully received bytes from the server. |
| 42 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. | 57 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. |
| 43 RESOLVE_FAILED, // Host resolution failed. | 58 RESOLVE_FAILED, // Host resolution failed. |
| 44 CONNECT_FAILED, // Connection to the server failed. | 59 CONNECT_FAILED, // Connection to the server failed. |
| 45 WRITE_FAILED, // Sending an echo message to the server failed. | 60 WRITE_FAILED, // Sending an echo message to the server failed. |
| 46 READ_FAILED, // Reading the reply from the server failed. | 61 READ_FAILED, // Reading the reply from the server failed. |
| 47 READ_VERIFY_FAILED, // Verification of data failed. | 62 READ_VERIFY_FAILED, // Verification of data failed. |
| 48 STATUS_MAX, // Bounding value. | 63 STATUS_MAX, // Bounding value. |
| 49 }; | 64 }; |
| 50 | 65 |
| 51 protected: | 66 protected: |
| 52 // Constructs an NetworkStats object that collects metrics for network | 67 // Constructs an NetworkStats object that collects metrics for network |
| 53 // connectivity (either TCP or UDP). | 68 // connectivity (either TCP or UDP). |
| 54 NetworkStats(); | 69 NetworkStats(); |
| 55 virtual ~NetworkStats(); | 70 virtual ~NetworkStats(); |
| 56 | 71 |
| 57 // Initializes |finished_callback_| and the number of bytes to send to the | 72 // Initializes |finished_callback_| and the number of bytes to send to the |
| 58 // server. |finished_callback| is called when we are done with the test. | 73 // server. |finished_callback| is called when we are done with the test. |
| 59 // |finished_callback| is mainly useful for unittests. | 74 // |finished_callback| is mainly useful for unittests. |
| 60 void Initialize(int bytes_to_send, | 75 void Initialize(uint32 bytes_to_send, |
| 61 net::CompletionCallback* finished_callback); | 76 net::CompletionCallback* finished_callback); |
| 62 | 77 |
| 63 // This method is called after socket connection is completed. It will send | 78 // This method is called after socket connection is completed. It will send |
| 64 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully | 79 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully |
| 65 // sending data to the |server|, it calls ReadData() to read/verify the data | 80 // sending data to the |server|, it calls ReadData() to read/verify the data |
| 66 // from the |server|. Returns true if successful. | 81 // from the |server|. Returns true if successful. |
| 67 bool DoStart(int result); | 82 bool DoStart(int result); |
| 68 | 83 |
| 69 // Collects network connectivity stats. This is called when all the data from | 84 // Collects network connectivity stats. This is called when all the data from |
| 70 // server is read or when there is a failure during connect/read/write. | 85 // server is read or when there is a failure during connect/read/write. |
| 71 virtual void Finish(Status status, int result) {} | 86 virtual void Finish(Status status, int result) {} |
| 72 | 87 |
| 73 // This method is called from Finish() and calls |finished_callback_| callback | 88 // This method is called from Finish() and calls |finished_callback_| callback |
| 74 // to indicate that the test has finished. | 89 // to indicate that the test has finished. |
| 75 void DoFinishCallback(int result); | 90 void DoFinishCallback(int result); |
| 76 | 91 |
| 77 // Returns the number of bytes to be sent to the |server|. | 92 // Returns the number of bytes to be sent to the |server|. |
| 78 int load_size() const { return load_size_; } | 93 uint32 load_size() const { return load_size_; } |
| 79 | 94 |
| 80 // Helper methods to get and set |socket_|. | 95 // Helper methods to get and set |socket_|. |
| 81 net::Socket* socket() { return socket_.get(); } | 96 net::Socket* socket() { return socket_.get(); } |
| 82 void set_socket(net::Socket* socket); | 97 void set_socket(net::Socket* socket); |
| 83 | 98 |
| 84 // Returns |start_time_| (used by histograms). | 99 // Returns |start_time_| (used by histograms). |
| 85 base::TimeTicks start_time() const { return start_time_; } | 100 base::TimeTicks start_time() const { return start_time_; } |
| 86 | 101 |
| 87 private: | 102 private: |
| 88 // Verifies the data and calls Finish() if there is an error or if all bytes | 103 // Verifies the data and calls Finish() if there is an error or if all bytes |
| 89 // are read. Returns true if Finish() is called otherwise returns false. | 104 // are read. Returns true if Finish() is called otherwise returns false. |
| 90 bool ReadComplete(int result); | 105 bool ReadComplete(int result); |
| 91 | 106 |
| 92 // Callbacks when an internal IO is completed. | 107 // Callbacks when an internal IO is completed. |
| 93 void OnReadComplete(int result); | 108 void OnReadComplete(int result); |
| 94 void OnWriteComplete(int result); | 109 void OnWriteComplete(int result); |
| 95 | 110 |
| 96 // Reads data from server until an error occurs. | 111 // Reads data from server until an error occurs. |
| 97 void ReadData(); | 112 void ReadData(); |
| 98 | 113 |
| 99 // Sends data to server until an error occurs. | 114 // Sends data to server until an error occurs. |
| 100 int SendData(); | 115 int SendData(); |
| 101 | 116 |
| 117 // Fills the |io_buffer| with the "echo request" message. This gets the | |
| 118 // <payload> from |stream_| and calculates the <checksum> of the <payload> and | |
| 119 // returns the "echo request" that has <version>, <checksum>, <payload_size> | |
| 120 // and <payload>. | |
| 121 void GetEchoRequest(net::IOBuffer* io_buffer); | |
| 122 | |
| 123 // This method parses the "echo response" message in the |read_buffer_| to | |
| 124 // verify that the <payload> is same as what we had sent in "echo request" | |
| 125 // message. |buffer_length| is the number of bytes read into |read_buffer_| | |
| 126 // from the socket. | |
| 127 bool VerifyBytes(uint32 buffer_length); | |
| 128 | |
| 102 // The socket handle for this session. | 129 // The socket handle for this session. |
| 103 scoped_ptr<net::Socket> socket_; | 130 scoped_ptr<net::Socket> socket_; |
| 104 | 131 |
| 105 // The read buffer used to read data from the socket. | 132 // The read buffer used to read data from the socket. |
| 106 scoped_refptr<net::IOBuffer> read_buffer_; | 133 scoped_refptr<net::IOBuffer> read_buffer_; |
| 107 | 134 |
| 108 // The write buffer used to write data to the socket. | 135 // The write buffer used to write data to the socket. |
| 109 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | 136 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
| 110 | 137 |
| 111 // Some counters for the session. | 138 // Some counters for the session. |
| 112 int load_size_; | 139 uint32 load_size_; |
| 113 int bytes_to_read_; | 140 int bytes_to_read_; |
| 114 int bytes_to_send_; | 141 int bytes_to_send_; |
| 115 | 142 |
| 143 // The key used for decrypting the message from the server. | |
| 144 std::string key_; | |
| 145 | |
| 146 // The |key_index_| is used to access the byte from |key_|. This is used | |
| 147 // during the decrypting the message from the server. | |
|
jar (doing other things)
2011/08/05 19:37:31
nit: Suggest:
key_index_ is in the range [0, key_
ramant (doing other things)
2011/08/12 01:05:08
Deleted this variable. Not needed because we are a
| |
| 148 uint32 key_index_; | |
| 149 | |
| 116 // |stream_| is used to generate data to be sent to the server and it is also | 150 // |stream_| is used to generate data to be sent to the server and it is also |
| 117 // used to verify the data received from the server. | 151 // used to verify the data received from the server. |
| 118 net::TestDataStream stream_; | 152 net::TestDataStream stream_; |
| 119 | 153 |
| 120 // Callback to call when data is read from the server. | 154 // Callback to call when data is read from the server. |
| 121 net::CompletionCallbackImpl<NetworkStats> read_callback_; | 155 net::CompletionCallbackImpl<NetworkStats> read_callback_; |
| 122 | 156 |
| 123 // Callback to call when data is sent to the server. | 157 // Callback to call when data is sent to the server. |
| 124 net::CompletionCallbackImpl<NetworkStats> write_callback_; | 158 net::CompletionCallbackImpl<NetworkStats> write_callback_; |
| 125 | 159 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 141 | 175 |
| 142 // Starts the client, connecting to |server|. | 176 // Starts the client, connecting to |server|. |
| 143 // Client will send |bytes_to_send| bytes to |server|. | 177 // Client will send |bytes_to_send| bytes to |server|. |
| 144 // When client has received all echoed bytes from the server, or | 178 // When client has received all echoed bytes from the server, or |
| 145 // when an error occurs causing the client to stop, |Finish| will be | 179 // when an error occurs causing the client to stop, |Finish| will be |
| 146 // called with a net status code. | 180 // called with a net status code. |
| 147 // |Finish| will collect histogram stats. | 181 // |Finish| will collect histogram stats. |
| 148 // Returns true if successful in starting the client. | 182 // Returns true if successful in starting the client. |
| 149 bool Start(const std::string& ip_str, | 183 bool Start(const std::string& ip_str, |
| 150 int port, | 184 int port, |
| 151 int bytes_to_send, | 185 uint32 bytes_to_send, |
| 152 net::CompletionCallback* callback); | 186 net::CompletionCallback* callback); |
| 153 | 187 |
| 154 protected: | 188 protected: |
| 155 // Allow tests to access our innards for testing purposes. | 189 // Allow tests to access our innards for testing purposes. |
| 156 friend class NetworkStatsTestUDP; | 190 friend class NetworkStatsTestUDP; |
| 157 | 191 |
| 158 // Collects stats for UDP connectivity. This is called when all the data from | 192 // Collects stats for UDP connectivity. This is called when all the data from |
| 159 // server is read or when there is a failure during connect/read/write. | 193 // server is read or when there is a failure during connect/read/write. |
| 160 virtual void Finish(Status status, int result); | 194 virtual void Finish(Status status, int result); |
| 161 }; | 195 }; |
| 162 | 196 |
| 163 class TCPStatsClient : public NetworkStats { | 197 class TCPStatsClient : public NetworkStats { |
| 164 public: | 198 public: |
| 165 // Constructs a TCPStatsClient object that collects metrics for TCP | 199 // Constructs a TCPStatsClient object that collects metrics for TCP |
| 166 // connectivity. | 200 // connectivity. |
| 167 TCPStatsClient(); | 201 TCPStatsClient(); |
| 168 virtual ~TCPStatsClient(); | 202 virtual ~TCPStatsClient(); |
| 169 | 203 |
| 170 // Starts the client, connecting to |server|. | 204 // Starts the client, connecting to |server|. |
| 171 // Client will send |bytes_to_send| bytes. | 205 // Client will send |bytes_to_send| bytes. |
| 172 // When the client has received all echoed bytes from the server, or | 206 // When the client has received all echoed bytes from the server, or |
| 173 // when an error occurs causing the client to stop, |Finish| will be | 207 // when an error occurs causing the client to stop, |Finish| will be |
| 174 // called with a net status code. | 208 // called with a net status code. |
| 175 // |Finish| will collect histogram stats. | 209 // |Finish| will collect histogram stats. |
| 176 // Returns true if successful in starting the client. | 210 // Returns true if successful in starting the client. |
| 177 bool Start(net::HostResolver* host_resolver, | 211 bool Start(net::HostResolver* host_resolver, |
| 178 const net::HostPortPair& server, | 212 const net::HostPortPair& server, |
| 179 int bytes_to_send, | 213 uint32 bytes_to_send, |
| 180 net::CompletionCallback* callback); | 214 net::CompletionCallback* callback); |
| 181 | 215 |
| 182 protected: | 216 protected: |
| 183 // Allow tests to access our innards for testing purposes. | 217 // Allow tests to access our innards for testing purposes. |
| 184 friend class NetworkStatsTestTCP; | 218 friend class NetworkStatsTestTCP; |
| 185 | 219 |
| 186 // Collects stats for TCP connectivity. This is called when all the data from | 220 // Collects stats for TCP connectivity. This is called when all the data from |
| 187 // server is read or when there is a failure during connect/read/write. | 221 // server is read or when there is a failure during connect/read/write. |
| 188 virtual void Finish(Status status, int result); | 222 virtual void Finish(Status status, int result); |
| 189 | 223 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 212 // This collects the network connectivity stats for UDP and TCP for small | 246 // This collects the network connectivity stats for UDP and TCP for small |
| 213 // percentage of users who are participating in the experiment. All users must | 247 // percentage of users who are participating in the experiment. All users must |
| 214 // have enabled "UMA upload". This method gets called only if UMA upload to the | 248 // have enabled "UMA upload". This method gets called only if UMA upload to the |
| 215 // server has succeeded. | 249 // server has succeeded. |
| 216 void CollectNetworkStats(const std::string& network_stats_server_url, | 250 void CollectNetworkStats(const std::string& network_stats_server_url, |
| 217 IOThread* io_thread); | 251 IOThread* io_thread); |
| 218 | 252 |
| 219 } // namespace chrome_browser_net | 253 } // namespace chrome_browser_net |
| 220 | 254 |
| 221 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | 255 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| OLD | NEW |