| 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 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/string_util.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/proxy/proxy_info.h" | 24 #include "net/proxy/proxy_info.h" |
| 24 #include "net/socket/socket.h" | 25 #include "net/socket/socket.h" |
| 26 |
| 25 namespace chrome_browser_net { | 27 namespace chrome_browser_net { |
| 26 | 28 |
| 27 // This class is used for live experiment of network connectivity (either TCP or | 29 // This class is used for live experiment of network connectivity (for UDP) |
| 28 // UDP) metrics. A small percentage of users participate in this experiment. All | 30 // metrics. A small percentage of users participate in this experiment. All |
| 29 // users (who are in the experiment) must have enabled "UMA upload". | 31 // users (who are in the experiment) must have enabled "UMA upload". |
| 30 // | 32 // |
| 31 // 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. |
| 32 // a) What percentage of users can get a message end-to-end to a UDP server? | 34 // a) Success/failure, to estimate reachability for users. |
| 33 // b) What percentage of users can get a message end-to-end to a TCP server? | 35 // b) RTT for some select set of messages. |
| 34 // c) What is the latency for UDP and TCP. | 36 // c) Perform packet loss tests for correlation and FEC experiments. |
| 35 // d) If connectivity failed, at what stage (Connect or Write or Read) did it | |
| 36 // fail? | |
| 37 // | 37 // |
| 38 // The following is the overview of the echo message protocol. | 38 // The following is the overview of the echo message protocol. |
| 39 // | 39 // |
| 40 // We send the "echo request" to the TCP/UDP servers in the following format: | 40 // We send the "echo request" to the UDP echo servers in the following format: |
| 41 // <version><checksum><payload_size><payload>. <version> is the version number | 41 // <version><checksum><payload_size><payload>. <version> is the version number |
| 42 // of the "echo request". <checksum> is the checksum of the <payload>. | 42 // of the "echo request". <checksum> is the checksum of the <payload>. |
| 43 // <payload_size> specifies the number of bytes in the <payload>. | 43 // <payload_size> specifies the number of bytes in the <payload>. |
| 44 // | 44 // |
| 45 // TCP/UDP servers respond to the "echo request" by returning "echo response". | 45 // UDP echo servers respond to the "echo request" by returning "echo response". |
| 46 // "echo response" is of the format: | 46 // "echo response" is of the format: |
| 47 // "<version><checksum><payload_size><key><encoded_payload>". <payload_size> | 47 // "<version><checksum><payload_size><key><encoded_payload>". <payload_size> |
| 48 // specifies the number of bytes in the <encoded_payload>. <key> is used to | 48 // specifies the number of bytes in the <encoded_payload>. <key> is used to |
| 49 // decode the <encoded_payload>. | 49 // decode the <encoded_payload>. |
| 50 | 50 |
| 51 class NetworkStats { | 51 class NetworkStats { |
| 52 public: | 52 public: |
| 53 enum Status { // Used in HISTOGRAM_ENUMERATION. | 53 enum Status { // Used in HISTOGRAM_ENUMERATION. |
| 54 SUCCESS, // Successfully received bytes from the server. | 54 SUCCESS, // Successfully received bytes from the server. |
| 55 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. | 55 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. |
| 56 SOCKET_CREATE_FAILED, // Socket creation failed. | 56 SOCKET_CREATE_FAILED, // Socket creation failed. |
| 57 RESOLVE_FAILED, // Host resolution failed. | 57 RESOLVE_FAILED, // Host resolution failed. |
| 58 CONNECT_FAILED, // Connection to the server failed. | 58 CONNECT_FAILED, // Connection to the server failed. |
| 59 WRITE_FAILED, // Sending an echo message to the server failed. | 59 WRITE_FAILED, // Sending an echo message to the server failed. |
| 60 READ_TIMED_OUT, // Reading the reply from the server timed out. | 60 READ_TIMED_OUT, // Reading the reply from the server timed out. |
| 61 READ_FAILED, // Reading the reply from the server failed. | 61 READ_FAILED, // Reading the reply from the server failed. |
| 62 ZERO_LENGTH_ERROR, // Zero length message. | 62 ZERO_LENGTH_ERROR, // Zero length message. |
| 63 NO_CHECKSUM_ERROR, // Message doesn't have a checksum. | 63 NO_CHECKSUM_ERROR, // Message doesn't have a checksum. |
| 64 NO_KEY_ERROR, // Message doesn't have a key. | 64 NO_KEY_ERROR, // Message doesn't have a key. |
| 65 NO_PAYLOAD_SIZE_ERROR, // Message doesn't have a payload size. | 65 NO_PAYLOAD_SIZE_ERROR, // Message doesn't have a payload size. |
| 66 NO_PAYLOAD_ERROR, // Message doesn't have a payload. | 66 NO_PAYLOAD_ERROR, // Message doesn't have a payload. |
| 67 INVALID_KEY_ERROR, // Invalid key in the message. | 67 INVALID_KEY_ERROR, // Invalid key in the message. |
| 68 TOO_SHORT_PAYLOAD, // Message is shorter than payload. | 68 TOO_SHORT_PAYLOAD, // Message is shorter than payload. |
| 69 TOO_LONG_PAYLOAD, // Message is longer than payload. | 69 TOO_LONG_PAYLOAD, // Message is longer than payload. |
| 70 INVALID_CHECKSUM, // Checksum verification failed. | 70 INVALID_CHECKSUM, // Checksum verification failed. |
| 71 PATTERN_CHANGED, // Pattern in payload has changed. | 71 PATTERN_CHANGED, // Pattern in payload has changed. |
| 72 INVALID_PACKET_NUMBER, // Packet number didn't match. | 72 INVALID_PACKET_NUMBER, // Packet number didn't match. |
| 73 TOO_MANY_PACKETS, // Received more packets than the packets sent. | 73 TOO_MANY_PACKETS, // Received more packets than the packets sent. |
| 74 STATUS_MAX, // Bounding value. | 74 PREVIOUS_PACKET_NUMBER, // Received a packet from an earlier test. |
| 75 DUPLICATE_PACKET, // Received a duplicate packet. |
| 76 SOME_PACKETS_NOT_VERIFIED, // Some packets have failed verification. |
| 77 STATUS_MAX, // Bounding value. |
| 75 }; | 78 }; |
| 76 | 79 |
| 77 // |ProtocolValue| enumerates different protocols that are being tested. | 80 // |ProtocolValue| enumerates different protocols that are being tested. |
| 78 enum ProtocolValue { | 81 enum ProtocolValue { |
| 79 PROTOCOL_TCP, | |
| 80 PROTOCOL_UDP, | 82 PROTOCOL_UDP, |
| 81 }; | 83 }; |
| 82 | 84 |
| 83 // |HistogramPortSelector| enumerates list of ports that are used for network | 85 // |HistogramPortSelector| enumerates list of ports that are used for network |
| 84 // connectivity tests (either TCP or UDP). | 86 // connectivity tests (for UDP). Currently we are testing port 6121 only. |
| 85 enum HistogramPortSelector { | 87 enum HistogramPortSelector { |
| 86 PORT_53 = 0, // DNS | 88 PORT_6121 = 0, // SPDY |
| 87 PORT_80, // HTTP | |
| 88 PORT_587, // SMTP Submission. | |
| 89 PORT_6121, // SPDY | |
| 90 PORT_8080, // High order webserver. | |
| 91 HISTOGRAM_PORT_MAX, | 89 HISTOGRAM_PORT_MAX, |
| 92 }; | 90 }; |
| 93 | 91 |
| 92 // |TestType| specifies the |current_test_| test we are currently running or |
| 93 // the |next_test_| test we need to run next. |
| 94 enum TestType { |
| 95 START_PACKET_TEST, // Initial packet loss test. |
| 96 NON_PACED_PACKET_TEST, // Packet loss test with no pacing. |
| 97 PACED_PACKET_TEST, // Packet loss test with pacing. |
| 98 TEST_TYPE_MAX, |
| 99 }; |
| 100 |
| 101 // |PacketStatus| collects the Status and RTT statistics for each packet. |
| 102 struct PacketStatus { |
| 103 base::TimeTicks start_time_; |
| 104 base::TimeTicks end_time_; |
| 105 }; |
| 106 |
| 107 // Constructs a NetworkStats object that collects metrics for network |
| 108 // connectivity (for UDP). |
| 109 NetworkStats(); |
| 110 // NetworkStats is deleted when Finish() is called. |
| 111 ~NetworkStats(); |
| 112 |
| 94 // Starts the client, connecting to |server|. | 113 // Starts the client, connecting to |server|. |
| 95 // Client will send |bytes_to_send| bytes to |server|. | 114 // Client will send |bytes_to_send| bytes to |server|. |
| 96 // When client has received all echoed bytes from the server, or | 115 // When client has received all echoed bytes from the server, or |
| 97 // when an error occurs causing the client to stop, |Finish| will be | 116 // when an error occurs causing the client to stop, |Finish| will be |
| 98 // called with a net status code. | 117 // called with a net status code. |
| 99 // |Finish| will collect histogram stats. | 118 // |Finish| will collect histogram stats. |
| 100 // Returns true if successful in starting the client. | 119 // Returns true if successful in starting the client. |
| 101 bool Start(net::HostResolver* host_resolver, | 120 bool Start(net::HostResolver* host_resolver, |
| 102 const net::HostPortPair& server, | 121 const net::HostPortPair& server, |
| 103 HistogramPortSelector histogram_port, | 122 HistogramPortSelector histogram_port, |
| 104 bool has_proxy_server, | 123 bool has_proxy_server, |
| 105 uint32 bytes_to_send, | 124 uint32 bytes_to_send, |
| 106 uint32 packets_to_send, | 125 uint32 packets_to_send, |
| 107 const net::CompletionCallback& callback); | 126 const net::CompletionCallback& callback); |
| 108 | 127 |
| 109 protected: | 128 private: |
| 110 // Constructs an NetworkStats object that collects metrics for network | 129 friend class NetworkStatsTest; |
| 111 // connectivity (either TCP or UDP). | 130 |
| 112 NetworkStats(); | 131 // Allow tests to access our innards for testing purposes. |
| 113 // NetworkStats is deleted when Finish() is called. | 132 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTest, GetHistogramNames); |
| 114 virtual ~NetworkStats(); | 133 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestUDP, VerifyBytesInvalidHeaders); |
| 134 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestUDP, VerifyBytesInvalidChecksum); |
| 135 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestUDP, VerifyBytesPreviousPacket); |
| 136 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestUDP, VerifyBytesInvalidPacketNumber); |
| 137 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestUDP, VerifyBytesPatternChanged); |
| 138 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestUDP, VerifyBytes); |
| 139 |
| 140 // Starts the test specified by the |next_test_|. It also resets all the book |
| 141 // keeping data, before starting the new test. |
| 142 void RestartPacketTest(); |
| 115 | 143 |
| 116 // Initializes |finished_callback_| and the number of bytes to send to the | 144 // Initializes |finished_callback_| and the number of bytes to send to the |
| 117 // server. |finished_callback| is called when we are done with the test. | 145 // server. |finished_callback| is called when we are done with the test. |
| 118 // |finished_callback| is mainly useful for unittests. | 146 // |finished_callback| is mainly useful for unittests. |
| 119 void Initialize(uint32 bytes_to_send, | 147 void Initialize(uint32 bytes_to_send, |
| 120 HistogramPortSelector histogram_port, | 148 HistogramPortSelector histogram_port, |
| 121 bool has_proxy_server, | 149 bool has_proxy_server, |
| 122 uint32 packets_to_send, | 150 uint32 packets_to_send, |
| 123 const net::CompletionCallback& finished_callback); | 151 const net::CompletionCallback& finished_callback); |
| 124 | 152 |
| 125 // Called after host is resolved. UDPStatsClient and TCPStatsClient implement | 153 // Resets all the counters and the collected stats. |
| 126 // this method. They create the socket and connect to the server. | 154 void ResetData(); |
| 127 virtual bool DoConnect(int result) = 0; | 155 |
| 156 // Callback that is called when host resolution is completed. |
| 157 void OnResolveComplete(int result); |
| 158 |
| 159 // Called after host is resolved. Creates UDPClientSocket and connects to the |
| 160 // server. If successfully connected, then calls ConnectComplete() to start |
| 161 // the network connectivity tests. Returns |false| if there is any error. |
| 162 bool DoConnect(int result); |
| 128 | 163 |
| 129 // This method is called after socket connection is completed. It will start | 164 // This method is called after socket connection is completed. It will start |
| 130 // the process of sending packets to |server| by calling SendPacket(). Returns | 165 // the process of sending packets to |server| by calling SendPacket(). Returns |
| 131 // false if connection is not established (result is less than 0). | 166 // false if connection is not established (result is less than 0). |
| 132 bool ConnectComplete(int result); | 167 bool ConnectComplete(int result); |
| 133 | 168 |
| 134 // Collects network connectivity stats. This is called when all the data from | |
| 135 // server is read or when there is a failure during connect/read/write. | |
| 136 virtual void Finish(Status status, int result) {} | |
| 137 | |
| 138 // This method is called from Finish() and calls |finished_callback_| callback | |
| 139 // to indicate that the test has finished. | |
| 140 void DoFinishCallback(int result); | |
| 141 | |
| 142 // Verifies the data and calls Finish() if there is an error or if all bytes | |
| 143 // are read. Returns true if Finish() is called otherwise returns false. | |
| 144 virtual bool ReadComplete(int result); | |
| 145 | |
| 146 // Returns the number of bytes to be sent to the |server|. | |
| 147 uint32 load_size() const { return load_size_; } | |
| 148 | |
| 149 // Helper methods to get and set |socket_|. | |
| 150 net::Socket* socket() { return socket_.get(); } | |
| 151 void set_socket(net::Socket* socket); | |
| 152 | |
| 153 // Returns |start_time_| (used by histograms). | |
| 154 base::TimeTicks start_time() const { return start_time_; } | |
| 155 | |
| 156 // Returns |addresses_|. | |
| 157 const net::AddressList& addresses() const { return addresses_; } | |
| 158 | |
| 159 // Returns packets_received_mask_ (used by unit tests). | |
| 160 uint32 packets_received_mask() const { return packets_received_mask_; } | |
| 161 | |
| 162 // Collect the following network connectivity stats. | |
| 163 // a) What percentage of users can get a message end-to-end to a TCP/UDP | |
| 164 // server and if connectivity failed, at what stage (Connect or Write or Read) | |
| 165 // did it fail? | |
| 166 // b) What is RTT for the echo message. | |
| 167 // c) Packet loss correlation and other network connectivity data by calling | |
| 168 // RecordAcksReceivedHistograms() and RecordStatusAndRTTHistograms(). | |
| 169 void RecordHistograms(const ProtocolValue& protocol, | |
| 170 const Status& status, | |
| 171 int result); | |
| 172 | |
| 173 private: | |
| 174 friend class NetworkStatsTest; | |
| 175 | |
| 176 // Allow tests to access our innards for testing purposes. | |
| 177 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTest, GetHistogramNames); | |
| 178 FRIEND_TEST_ALL_PREFIXES(NetworkStatsTestTCP, VerifyBytes); | |
| 179 | |
| 180 // Callback that is called when host resolution is completed. | |
| 181 void OnResolveComplete(int result); | |
| 182 | |
| 183 // This method is called whenever we need to send a packet. It is called from | 169 // This method is called whenever we need to send a packet. It is called from |
| 184 // either ConnectComplete or OnWriteComplete. It will send a packet, based on | 170 // either ConnectComplete or OnWriteComplete. It will send a packet, based on |
| 185 // load_size_, to |server| by calling SendData(). If there are no more packets | 171 // load_size_, to |server| by calling SendData(). If there are no more packets |
| 186 // to send, it calls ReadData() to read/verify the data from the |server|. | 172 // to send, it calls ReadData() to read/verify the data from the |server|. |
| 187 void SendPacket(); | 173 void SendPacket(); |
| 188 | 174 |
| 175 // Sends the next packet by calling |SendPacket| after a delay. Delay time is |
| 176 // calculated based on the remaining packets and the remaining time. |
| 177 // For START_PACKET_TEST and NON_PACED_PACKET_TEST delay is zero. It also |
| 178 // yields for packets received between sends. |
| 179 void SendNextPacketAfterDelay(); |
| 180 |
| 181 // Verifies the data and calls Finish() if there is a significant network |
| 182 // error or if all packets are sent. Returns true if Finish() is called |
| 183 // otherwise returns false. |
| 184 bool ReadComplete(int result); |
| 185 |
| 189 // Callbacks when an internal IO is completed. | 186 // Callbacks when an internal IO is completed. |
| 190 void OnReadComplete(int result); | 187 void OnReadComplete(int result); |
| 191 void OnWriteComplete(int result); | 188 void OnWriteComplete(int result); |
| 192 | 189 |
| 193 // Reads data from server until an error occurs. | 190 // Reads data from server until an error occurs. |
| 194 void ReadData(); | 191 void ReadData(); |
| 195 | 192 |
| 196 // Sends data to server until an error occurs. | 193 // Sends data to server until an error occurs. |
| 197 int SendData(); | 194 int SendData(); |
| 198 | 195 |
| 199 // Determine the size of the packet from |load_size_|. The packet size | 196 // Determine the size of the packet from |load_size_|. The packet size |
| 200 // includes |load_size_| plus the header size. | 197 // includes |load_size_| plus the header size. |
| 201 uint32 SendingPacketSize() const; | 198 uint32 SendingPacketSize() const; |
| 202 uint32 ReceivingPacketSize() const; | 199 uint32 ReceivingPacketSize() const; |
| 203 | 200 |
| 204 // This method decrements the |bytes_to_send_| by the |bytes_sent| and updates | 201 // This method decrements the |bytes_to_send_| by the |bytes_sent| and updates |
| 205 // |packets_sent_| if all the bytes are sent. It also informs |write_buffer_| | 202 // |packets_sent_| if all the bytes are sent. It also informs |write_buffer_| |
| 206 // that data has been consumed. | 203 // that data has been consumed. |
| 207 void DidSendData(int bytes_sent); | 204 void DidSendData(int bytes_sent); |
| 208 | 205 |
| 209 // We set a timeout for responses from the echo servers. | 206 // We set a timeout for responses from the echo servers. |
| 210 void StartReadDataTimer(int milliseconds); | 207 void StartReadDataTimer(int milliseconds); |
| 211 void OnReadDataTimeout(); // Called when the ReadData Timer fires. | 208 |
| 209 // Called when the ReadData Timer fires. |test_base_packet_number| specifies |
| 210 // the |base_packet_number_| when we have started the timer. If we haven't |
| 211 // received all the packets for the test (test took longer than 30 secs to |
| 212 // run), then we call Finish() to finish processing. If we have already |
| 213 // received all packets from the server, then this method is a no-op. |
| 214 void OnReadDataTimeout(uint32 test_base_packet_number); |
| 212 | 215 |
| 213 // Returns the checksum for the message. | 216 // Returns the checksum for the message. |
| 214 uint32 GetChecksum(const char* message, uint32 message_length); | 217 uint32 GetChecksum(const char* message, uint32 message_length); |
| 215 | 218 |
| 216 // Encrypts/decrypts the data with the key and returns encrypted/decrypted | 219 // Encrypts/decrypts the data with the key and returns encrypted/decrypted |
| 217 // data in |encoded_data|. | 220 // data in |encoded_data|. |
| 218 void Crypt(const char* key, | 221 void Crypt(const char* key, |
| 219 uint32 key_length, | 222 uint32 key_length, |
| 220 const char* data, | 223 const char* data, |
| 221 uint32 data_length, | 224 uint32 data_length, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 232 // that is in it. It returns SUCCESS, if all the packets are verified. | 235 // that is in it. It returns SUCCESS, if all the packets are verified. |
| 233 NetworkStats::Status VerifyPackets(); | 236 NetworkStats::Status VerifyPackets(); |
| 234 | 237 |
| 235 // This method parses the "echo response" message in the |response| to verify | 238 // This method parses the "echo response" message in the |response| to verify |
| 236 // that the <payload> is same as what we had sent in "echo request" message. | 239 // that the <payload> is same as what we had sent in "echo request" message. |
| 237 // As it verifies the response in each packet, it also extracts the packet | 240 // As it verifies the response in each packet, it also extracts the packet |
| 238 // number, and records that said packet number responded. It returns SUCCESS, | 241 // number, and records that said packet number responded. It returns SUCCESS, |
| 239 // if all the bytes are verified. | 242 // if all the bytes are verified. |
| 240 NetworkStats::Status VerifyBytes(const std::string& response); | 243 NetworkStats::Status VerifyBytes(const std::string& response); |
| 241 | 244 |
| 245 // Collects network connectivity stats. This is called when all the data from |
| 246 // server is read or when there is a failure during connect/read/write. It |
| 247 // will either restart the second phase of the test, or it will self destruct |
| 248 // at the end of this method. |
| 249 void Finish(Status status, int result); |
| 250 |
| 251 // This method is called from Finish() and calls |finished_callback_| callback |
| 252 // to indicate that the test has finished. |
| 253 void DoFinishCallback(int result); |
| 254 |
| 255 // Collect the network connectivity stats by calling RecordRTTHistograms(), |
| 256 // RecordAcksReceivedHistograms() and RecordPacketLossSeriesHistograms(). See |
| 257 // below for details. |
| 258 void RecordHistograms(const ProtocolValue& protocol, |
| 259 const Status& status, |
| 260 int result); |
| 261 |
| 242 // Collect the following network connectivity stats when | 262 // Collect the following network connectivity stats when |
| 243 // kMaximumSequentialPackets (21) packets are sent. | 263 // kMaximumSequentialPackets (21) packets are sent. |
| 244 // a) Received the "echo response" for at least one packet. | 264 // a) Received the "echo response" for at least one packet. |
| 245 // b) Received the "echo response" for the nth packet. | 265 // b) Received the "echo response" for the nth packet. |
| 246 // c) Count the number of "echo responses" received for each of the initial | 266 // c) Count the number of "echo responses" received for each of the initial |
| 247 // sequences of packets 1...n. | 267 // sequences of packets 1...n. |
| 248 void RecordAcksReceivedHistograms(const char* load_size_string); | 268 void RecordAcksReceivedHistograms(const std::string& load_size_string); |
| 249 | 269 |
| 250 // Collect the following network connectivity stats. | 270 // Collect the following network connectivity stats for |
| 251 // a) What percentage of users can get a message end-to-end to a TCP/UDP | 271 // kMaximumCorrelationPackets (6) packets test. |
| 252 // server and if connectivity failed, at what stage (Connect or Write or Read) | 272 // a) Success/failure of each packet, to estimate reachability for users. |
| 253 // did it fail? | 273 // b) Records if there is a probabalistic dependency in packet loss when |
| 254 // b) What is RTT for the echo message. | |
| 255 // c) Records if there is a probabalistic dependency in packet loss when | |
| 256 // kMaximumCorrelationPackets packets are sent consecutively. | 274 // kMaximumCorrelationPackets packets are sent consecutively. |
| 257 void RecordPacketLossSeriesHistograms(const ProtocolValue& protocol, | 275 void RecordPacketLossSeriesHistograms(const ProtocolValue& protocol, |
| 276 const std::string& load_size_string, |
| 258 const Status& status, | 277 const Status& status, |
| 259 const char* load_size_string, | |
| 260 int result); | 278 int result); |
| 279 |
| 280 // Collects the RTT for the packet specified by the |index|. |
| 281 void RecordRTTHistograms(const ProtocolValue& protocol, |
| 282 const std::string& load_size_string, |
| 283 uint32 index); |
| 284 |
| 285 uint32 load_size() const { return load_size_; } |
| 286 |
| 287 // Returns string representation of test_type_. |
| 288 const char* TestName(); |
| 289 |
| 290 uint32 packet_number() const { return packet_number_; } |
| 291 uint32 base_packet_number() const { return base_packet_number_; } |
| 292 uint32 set_base_packet_number(uint32 packet_number) { |
| 293 return base_packet_number_ = packet_number; |
| 294 } |
| 295 |
| 296 net::Socket* socket() { return socket_.get(); } |
| 297 void set_socket(net::Socket* socket); |
| 298 |
| 299 const net::AddressList& addresses() const { return addresses_; } |
| 300 |
| 301 uint32 packets_received_mask() const { return packets_received_mask_; } |
| 302 |
| 303 TestType next_test() const { return next_test_; } |
| 304 |
| 261 // The socket handle for this session. | 305 // The socket handle for this session. |
| 262 scoped_ptr<net::Socket> socket_; | 306 scoped_ptr<net::Socket> socket_; |
| 263 | 307 |
| 264 // The read buffer used to read data from the socket. | 308 // The read buffer used to read data from the socket. |
| 265 scoped_refptr<net::IOBuffer> read_buffer_; | 309 scoped_refptr<net::IOBuffer> read_buffer_; |
| 266 | 310 |
| 267 // The write buffer used to write data to the socket. | 311 // The write buffer used to write data to the socket. |
| 268 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | 312 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
| 269 | 313 |
| 270 // Some counters for the session. | 314 // Some counters for the session. |
| 271 uint32 load_size_; | 315 uint32 load_size_; |
| 272 uint32 bytes_to_read_; | 316 uint32 bytes_to_read_; |
| 273 uint32 bytes_to_send_; | 317 uint32 bytes_to_send_; |
| 274 | 318 |
| 275 // The encoded message read from the server. | |
| 276 std::string encoded_message_; | |
| 277 | |
| 278 // |stream_| is used to generate data to be sent to the server and it is also | 319 // |stream_| is used to generate data to be sent to the server and it is also |
| 279 // used to verify the data received from the server. | 320 // used to verify the data received from the server. |
| 280 net::TestDataStream stream_; | 321 net::TestDataStream stream_; |
| 281 | 322 |
| 282 // |histogram_port_| specifies the port for which we are testing the network | 323 // |histogram_port_| specifies the port for which we are testing the network |
| 283 // connectivity. | 324 // connectivity. |
| 284 HistogramPortSelector histogram_port_; | 325 HistogramPortSelector histogram_port_; |
| 285 | 326 |
| 286 // |has_proxy_server_| specifies if there is a proxy server or not. | 327 // |has_proxy_server_| specifies if there is a proxy server or not. |
| 287 bool has_proxy_server_; | 328 bool has_proxy_server_; |
| 288 | 329 |
| 289 // HostResolver fills out the |addresses_| after host resolution is completed. | 330 // HostResolver fills out the |addresses_| after host resolution is completed. |
| 290 net::AddressList addresses_; | 331 net::AddressList addresses_; |
| 291 | 332 |
| 292 // Callback to call when echo protocol is successefully finished or whenever | 333 // Callback to call when echo protocol is successefully finished or whenever |
| 293 // there is an error (this allows unittests to wait until echo protocol's | 334 // there is an error (this allows unittests to wait until echo protocol's |
| 294 // round trip is finished). | 335 // round trip is finished). |
| 295 net::CompletionCallback finished_callback_; | 336 net::CompletionCallback finished_callback_; |
| 296 | 337 |
| 297 // The time when the session was started. | 338 // Collects the Status and RTT for each packet. |
| 298 base::TimeTicks start_time_; | 339 std::vector<PacketStatus> packet_status_; |
| 340 |
| 341 // The time when we have received 1st byte from the server. |
| 342 base::TimeTicks packet_1st_byte_read_time_; |
| 343 |
| 344 // The last time when we have received data from the server. |
| 345 base::TimeTicks packet_last_byte_read_time_; |
| 346 |
| 347 // This is the average time it took to receive a packet from the server. |
| 348 base::TimeDelta average_time_; |
| 299 | 349 |
| 300 // Data to track number of packets to send to the server and the packets we | 350 // Data to track number of packets to send to the server and the packets we |
| 301 // have received from the server. | 351 // have received from the server. |
| 302 uint32 packets_to_send_; | 352 uint32 packets_to_send_; // Numbers of packets that are to be sent. |
| 303 uint32 packets_sent_; | 353 uint32 packets_sent_; // Numbers of packets sent to the server. |
| 304 uint32 base_packet_number_; | 354 uint32 packets_received_; // Number of packets successfully received. |
| 305 uint32 packets_received_mask_; | 355 uint32 packets_received_mask_; // Tracks the received status of each packet. |
| 356 uint32 packet_number_; // The packet number being sent to the server. |
| 357 uint32 base_packet_number_; // The packet number before the test. |
| 358 bool sending_complete_; // Finished sending all packets. |
| 359 |
| 360 TestType current_test_; // Current test that is running. |
| 361 TestType next_test_; // Next test that is to be run. |
| 306 | 362 |
| 307 // We use this factory to create timeout tasks for socket's ReadData. | 363 // We use this factory to create timeout tasks for socket's ReadData. |
| 308 base::WeakPtrFactory<NetworkStats> weak_factory_; | 364 base::WeakPtrFactory<NetworkStats> weak_factory_; |
| 309 }; | 365 }; |
| 310 | 366 |
| 311 class UDPStatsClient : public NetworkStats { | |
| 312 public: | |
| 313 // Constructs an UDPStatsClient object that collects metrics for UDP | |
| 314 // connectivity. | |
| 315 UDPStatsClient(); | |
| 316 // UDPStatsClient is deleted when Finish() is called. | |
| 317 virtual ~UDPStatsClient(); | |
| 318 | |
| 319 protected: | |
| 320 // Allow tests to access our innards for testing purposes. | |
| 321 friend class NetworkStatsTestUDP; | |
| 322 | |
| 323 // Called after host is resolved. Creates UDClientSocket and connects to the | |
| 324 // server. If successfully connected, then calls ConnectComplete() to start | |
| 325 // the echo protocol. Returns |false| if there is any error. | |
| 326 virtual bool DoConnect(int result) OVERRIDE; | |
| 327 | |
| 328 // This method calls NetworkStats::ReadComplete() to verify the data and calls | |
| 329 // Finish() if there is an error or if read callback didn't return any data | |
| 330 // (|result| is less than or equal to 0). | |
| 331 virtual bool ReadComplete(int result) OVERRIDE; | |
| 332 | |
| 333 // Collects stats for UDP connectivity. This is called when all the data from | |
| 334 // server is read or when there is a failure during connect/read/write. This | |
| 335 // object is deleted at the end of this method. | |
| 336 virtual void Finish(Status status, int result) OVERRIDE; | |
| 337 }; | |
| 338 | |
| 339 class TCPStatsClient : public NetworkStats { | |
| 340 public: | |
| 341 // Constructs a TCPStatsClient object that collects metrics for TCP | |
| 342 // connectivity. | |
| 343 TCPStatsClient(); | |
| 344 // TCPStatsClient is deleted when Finish() is called. | |
| 345 virtual ~TCPStatsClient(); | |
| 346 | |
| 347 protected: | |
| 348 // Allow tests to access our innards for testing purposes. | |
| 349 friend class NetworkStatsTestTCP; | |
| 350 | |
| 351 // Called after host is resolved. Creates TCPClientSocket and connects to the | |
| 352 // server. | |
| 353 virtual bool DoConnect(int result) OVERRIDE; | |
| 354 | |
| 355 // This method calls NetworkStats::ReadComplete() to verify the data and calls | |
| 356 // Finish() if there is an error (|result| is less than 0). | |
| 357 virtual bool ReadComplete(int result) OVERRIDE; | |
| 358 | |
| 359 // Collects stats for TCP connectivity. This is called when all the data from | |
| 360 // server is read or when there is a failure during connect/read/write. This | |
| 361 // object is deleted at the end of this method. | |
| 362 virtual void Finish(Status status, int result) OVERRIDE; | |
| 363 | |
| 364 private: | |
| 365 // Callback that is called when connect is completed and calls | |
| 366 // ConnectComplete() to start the echo protocol. | |
| 367 void OnConnectComplete(int result); | |
| 368 }; | |
| 369 | |
| 370 class ProxyDetector { | 367 class ProxyDetector { |
| 371 public: | 368 public: |
| 372 // Used for the callback that is called from |OnResolveProxyComplete|. | 369 // Used for the callback that is called from |OnResolveProxyComplete|. |
| 373 typedef base::Callback<void(bool)> OnResolvedCallback; | 370 typedef base::Callback<void(bool)> OnResolvedCallback; |
| 374 | 371 |
| 375 // Constructs a ProxyDetector object that finds out if access to | 372 // Constructs a ProxyDetector object that finds out if access to |
| 376 // |server_address| goes through a proxy server or not. Calls the |callback| | 373 // |server_address| goes through a proxy server or not. Calls the |callback| |
| 377 // after proxy resolution is completed by currying the proxy resolution | 374 // after proxy resolution is completed by currying the proxy resolution |
| 378 // status. | 375 // status. |
| 379 ProxyDetector(net::ProxyService* proxy_service, | 376 ProxyDetector(net::ProxyService* proxy_service, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 403 OnResolvedCallback callback_; | 400 OnResolvedCallback callback_; |
| 404 | 401 |
| 405 // |proxy_info_| holds proxy information returned by ResolveProxy. | 402 // |proxy_info_| holds proxy information returned by ResolveProxy. |
| 406 net::ProxyInfo proxy_info_; | 403 net::ProxyInfo proxy_info_; |
| 407 | 404 |
| 408 // Indicates if there is a pending a proxy resolution. We use this to assert | 405 // Indicates if there is a pending a proxy resolution. We use this to assert |
| 409 // that there is no in-progress proxy resolution request. | 406 // that there is no in-progress proxy resolution request. |
| 410 bool has_pending_proxy_resolution_; | 407 bool has_pending_proxy_resolution_; |
| 411 }; | 408 }; |
| 412 | 409 |
| 413 // This collects the network connectivity stats for UDP and TCP for small | 410 // This collects the network connectivity stats for UDP protocol for small |
| 414 // percentage of users who are participating in the experiment. All users must | 411 // percentage of users who are participating in the experiment. All users must |
| 415 // have enabled "UMA upload". This method gets called only if UMA upload to the | 412 // have enabled "UMA upload". This method gets called only if UMA upload to the |
| 416 // server has succeeded. | 413 // server has succeeded. |
| 417 void CollectNetworkStats(const std::string& network_stats_server_url, | 414 void CollectNetworkStats(const std::string& network_stats_server_url, |
| 418 IOThread* io_thread); | 415 IOThread* io_thread); |
| 419 | 416 |
| 420 // This starts a test randomly selected among "TCP test with small packet size", | 417 // This starts a test randomly selected among "6 packets correlation test for |
| 421 // "TCP test with large packet size", "UDP test with small packet size", "UDP | 418 // 1200 bytes packet", "21 packets bursty test with 1200 bytes packet", |
| 422 // test with large packet size" and "UDP multi packet loss" tests to collect the | 419 // "21 packets bursty test with 500 bytes packet", and "21 packets bursty test |
| 423 // network connectivity stats. | 420 // with 100 bytes packet" tests. |
| 424 void StartNetworkStatsTest(net::HostResolver* host_resolver, | 421 void StartNetworkStatsTest(net::HostResolver* host_resolver, |
| 425 const net::HostPortPair& server_address, | 422 const net::HostPortPair& server_address, |
| 426 NetworkStats::HistogramPortSelector histogram_port, | 423 NetworkStats::HistogramPortSelector histogram_port, |
| 427 bool has_proxy_server); | 424 bool has_proxy_server); |
| 428 | 425 |
| 429 } // namespace chrome_browser_net | 426 } // namespace chrome_browser_net |
| 430 | 427 |
| 431 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | 428 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| OLD | NEW |