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><encoded_payload>". <payload_size> |
| 49 // specifies the number of bytes in the <encoded_payload>. <key> is used to |
| 50 // decode the <encoded_payload>. |
37 | 51 |
38 class NetworkStats { | 52 class NetworkStats { |
39 public: | 53 public: |
40 enum Status { // Used in HISTOGRAM_ENUMERATION. | 54 enum Status { // Used in HISTOGRAM_ENUMERATION. |
41 SUCCESS, // Successfully received bytes from the server. | 55 SUCCESS, // Successfully received bytes from the server. |
42 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. | 56 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. |
| 57 SOCKET_CREATE_FAILED, // Socket creation 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 |
| 92 // Verifies the data and calls Finish() if there is an error or if all bytes |
| 93 // are read. Returns true if Finish() is called otherwise returns false. |
| 94 virtual bool ReadComplete(int result); |
| 95 |
77 // Returns the number of bytes to be sent to the |server|. | 96 // Returns the number of bytes to be sent to the |server|. |
78 int load_size() const { return load_size_; } | 97 uint32 load_size() const { return load_size_; } |
79 | 98 |
80 // Helper methods to get and set |socket_|. | 99 // Helper methods to get and set |socket_|. |
81 net::Socket* socket() { return socket_.get(); } | 100 net::Socket* socket() { return socket_.get(); } |
82 void set_socket(net::Socket* socket); | 101 void set_socket(net::Socket* socket); |
83 | 102 |
84 // Returns |start_time_| (used by histograms). | 103 // Returns |start_time_| (used by histograms). |
85 base::TimeTicks start_time() const { return start_time_; } | 104 base::TimeTicks start_time() const { return start_time_; } |
86 | 105 |
87 private: | 106 private: |
88 // 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. | |
90 bool ReadComplete(int result); | |
91 | |
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 // We set a timeout for responses from the echo servers. |
| 118 void StartReadDataTimer(int milliseconds); |
| 119 void OnReadDataTimeout(); // Called when the ReadData Timer fires. |
| 120 |
| 121 // Fills the |io_buffer| with the "echo request" message. This gets the |
| 122 // <payload> from |stream_| and calculates the <checksum> of the <payload> and |
| 123 // returns the "echo request" that has <version>, <checksum>, <payload_size> |
| 124 // and <payload>. |
| 125 void GetEchoRequest(net::IOBuffer* io_buffer); |
| 126 |
| 127 // This method parses the "echo response" message in the |encoded_message_| to |
| 128 // verify that the <payload> is same as what we had sent in "echo request" |
| 129 // message. |
| 130 bool VerifyBytes(); |
| 131 |
102 // The socket handle for this session. | 132 // The socket handle for this session. |
103 scoped_ptr<net::Socket> socket_; | 133 scoped_ptr<net::Socket> socket_; |
104 | 134 |
105 // The read buffer used to read data from the socket. | 135 // The read buffer used to read data from the socket. |
106 scoped_refptr<net::IOBuffer> read_buffer_; | 136 scoped_refptr<net::IOBuffer> read_buffer_; |
107 | 137 |
108 // The write buffer used to write data to the socket. | 138 // The write buffer used to write data to the socket. |
109 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | 139 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
110 | 140 |
111 // Some counters for the session. | 141 // Some counters for the session. |
112 int load_size_; | 142 uint32 load_size_; |
113 int bytes_to_read_; | 143 int bytes_to_read_; |
114 int bytes_to_send_; | 144 int bytes_to_send_; |
115 | 145 |
| 146 // The encoded message read from the server. |
| 147 std::string encoded_message_; |
| 148 |
116 // |stream_| is used to generate data to be sent to the server and it is also | 149 // |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. | 150 // used to verify the data received from the server. |
118 net::TestDataStream stream_; | 151 net::TestDataStream stream_; |
119 | 152 |
120 // Callback to call when data is read from the server. | 153 // Callback to call when data is read from the server. |
121 net::CompletionCallbackImpl<NetworkStats> read_callback_; | 154 net::CompletionCallbackImpl<NetworkStats> read_callback_; |
122 | 155 |
123 // Callback to call when data is sent to the server. | 156 // Callback to call when data is sent to the server. |
124 net::CompletionCallbackImpl<NetworkStats> write_callback_; | 157 net::CompletionCallbackImpl<NetworkStats> write_callback_; |
125 | 158 |
126 // Callback to call when echo protocol is successefully finished or whenever | 159 // Callback to call when echo protocol is successefully finished or whenever |
127 // there is an error (this allows unittests to wait until echo protocol's | 160 // there is an error (this allows unittests to wait until echo protocol's |
128 // round trip is finished). | 161 // round trip is finished). |
129 net::CompletionCallback* finished_callback_; | 162 net::CompletionCallback* finished_callback_; |
130 | 163 |
131 // The time when the session was started. | 164 // The time when the session was started. |
132 base::TimeTicks start_time_; | 165 base::TimeTicks start_time_; |
| 166 |
| 167 // We use this factory to create timeout tasks for socket's ReadData. |
| 168 ScopedRunnableMethodFactory<NetworkStats> timers_factory_; |
133 }; | 169 }; |
134 | 170 |
135 class UDPStatsClient : public NetworkStats { | 171 class UDPStatsClient : public NetworkStats { |
136 public: | 172 public: |
137 // Constructs an UDPStatsClient object that collects metrics for UDP | 173 // Constructs an UDPStatsClient object that collects metrics for UDP |
138 // connectivity. | 174 // connectivity. |
139 UDPStatsClient(); | 175 UDPStatsClient(); |
140 virtual ~UDPStatsClient(); | 176 virtual ~UDPStatsClient(); |
141 | 177 |
142 // Starts the client, connecting to |server|. | 178 // Starts the client, connecting to |server|. |
143 // Client will send |bytes_to_send| bytes to |server|. | 179 // Client will send |bytes_to_send| bytes to |server|. |
144 // When client has received all echoed bytes from the server, or | 180 // When client has received all echoed bytes from the server, or |
145 // when an error occurs causing the client to stop, |Finish| will be | 181 // when an error occurs causing the client to stop, |Finish| will be |
146 // called with a net status code. | 182 // called with a net status code. |
147 // |Finish| will collect histogram stats. | 183 // |Finish| will collect histogram stats. |
148 // Returns true if successful in starting the client. | 184 // Returns true if successful in starting the client. |
149 bool Start(const std::string& ip_str, | 185 bool Start(const std::string& ip_str, |
150 int port, | 186 int port, |
151 int bytes_to_send, | 187 uint32 bytes_to_send, |
152 net::CompletionCallback* callback); | 188 net::CompletionCallback* callback); |
153 | 189 |
154 protected: | 190 protected: |
155 // Allow tests to access our innards for testing purposes. | 191 // Allow tests to access our innards for testing purposes. |
156 friend class NetworkStatsTestUDP; | 192 friend class NetworkStatsTestUDP; |
157 | 193 |
158 // Collects stats for UDP connectivity. This is called when all the data from | 194 // 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. | 195 // server is read or when there is a failure during connect/read/write. |
160 virtual void Finish(Status status, int result); | 196 virtual void Finish(Status status, int result); |
| 197 |
| 198 // This method calls NetworkStats::ReadComplete() to verify the data and calls |
| 199 // Finish() if there is an error or if read callback didn't return any data |
| 200 // (|result| is less than or equal to 0). |
| 201 virtual bool ReadComplete(int result); |
161 }; | 202 }; |
162 | 203 |
163 class TCPStatsClient : public NetworkStats { | 204 class TCPStatsClient : public NetworkStats { |
164 public: | 205 public: |
165 // Constructs a TCPStatsClient object that collects metrics for TCP | 206 // Constructs a TCPStatsClient object that collects metrics for TCP |
166 // connectivity. | 207 // connectivity. |
167 TCPStatsClient(); | 208 TCPStatsClient(); |
168 virtual ~TCPStatsClient(); | 209 virtual ~TCPStatsClient(); |
169 | 210 |
170 // Starts the client, connecting to |server|. | 211 // Starts the client, connecting to |server|. |
171 // Client will send |bytes_to_send| bytes. | 212 // Client will send |bytes_to_send| bytes. |
172 // When the client has received all echoed bytes from the server, or | 213 // 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 | 214 // when an error occurs causing the client to stop, |Finish| will be |
174 // called with a net status code. | 215 // called with a net status code. |
175 // |Finish| will collect histogram stats. | 216 // |Finish| will collect histogram stats. |
176 // Returns true if successful in starting the client. | 217 // Returns true if successful in starting the client. |
177 bool Start(net::HostResolver* host_resolver, | 218 bool Start(net::HostResolver* host_resolver, |
178 const net::HostPortPair& server, | 219 const net::HostPortPair& server, |
179 int bytes_to_send, | 220 uint32 bytes_to_send, |
180 net::CompletionCallback* callback); | 221 net::CompletionCallback* callback); |
181 | 222 |
182 protected: | 223 protected: |
183 // Allow tests to access our innards for testing purposes. | 224 // Allow tests to access our innards for testing purposes. |
184 friend class NetworkStatsTestTCP; | 225 friend class NetworkStatsTestTCP; |
185 | 226 |
186 // Collects stats for TCP connectivity. This is called when all the data from | 227 // 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. | 228 // server is read or when there is a failure during connect/read/write. |
188 virtual void Finish(Status status, int result); | 229 virtual void Finish(Status status, int result); |
189 | 230 |
| 231 // This method calls NetworkStats::ReadComplete() to verify the data and calls |
| 232 // Finish() if there is an error (|result| is less than 0). |
| 233 virtual bool ReadComplete(int result); |
| 234 |
190 private: | 235 private: |
191 // Callback that is called when host resolution is completed. | 236 // Callback that is called when host resolution is completed. |
192 void OnResolveComplete(int result); | 237 void OnResolveComplete(int result); |
193 | 238 |
194 // Called after host is resolved. Creates TCPClientSocket and connects to the | 239 // Called after host is resolved. Creates TCPClientSocket and connects to the |
195 // server. | 240 // server. |
196 bool DoConnect(int result); | 241 bool DoConnect(int result); |
197 | 242 |
198 // Callback that is called when connect is completed and calls DoStart() to | 243 // Callback that is called when connect is completed and calls DoStart() to |
199 // start the echo protocl. | 244 // start the echo protocl. |
(...skipping 12 matching lines...) Expand all Loading... |
212 // This collects the network connectivity stats for UDP and TCP for small | 257 // 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 | 258 // 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 | 259 // have enabled "UMA upload". This method gets called only if UMA upload to the |
215 // server has succeeded. | 260 // server has succeeded. |
216 void CollectNetworkStats(const std::string& network_stats_server_url, | 261 void CollectNetworkStats(const std::string& network_stats_server_url, |
217 IOThread* io_thread); | 262 IOThread* io_thread); |
218 | 263 |
219 } // namespace chrome_browser_net | 264 } // namespace chrome_browser_net |
220 | 265 |
221 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | 266 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ |
OLD | NEW |