OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| 6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/time.h" |
| 15 #include "chrome/browser/io_thread.h" |
| 16 #include "net/base/address_list.h" |
| 17 #include "net/base/completion_callback.h" |
| 18 #include "net/base/host_port_pair.h" |
| 19 #include "net/base/host_resolver.h" |
| 20 #include "net/base/io_buffer.h" |
| 21 #include "net/base/ip_endpoint.h" |
| 22 #include "net/base/test_data_stream.h" |
| 23 #include "net/socket/socket.h" |
| 24 |
| 25 namespace chrome_browser_net { |
| 26 |
| 27 // 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 // users (who are in the experiment) must have enabled "UMA upload". |
| 30 // |
| 31 // 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 // 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 // d) If connectivity failed, at what stage (Connect or Write or Read) did it |
| 36 // fail? |
| 37 |
| 38 class NetworkStats { |
| 39 public: |
| 40 enum Status { // Used in HISTOGRAM_ENUMERATION. |
| 41 SUCCESS, // Successfully received bytes from the server. |
| 42 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. |
| 43 RESOLVE_FAILED, // Host resolution failed. |
| 44 CONNECT_FAILED, // Connection to the server failed. |
| 45 WRITE_FAILED, // Sending an echo message to the server failed. |
| 46 READ_FAILED, // Reading the reply from the server failed. |
| 47 READ_VERIFY_FAILED, // Verification of data failed. |
| 48 STATUS_MAX, // Bounding value. |
| 49 }; |
| 50 |
| 51 protected: |
| 52 // Constructs an NetworkStats object that collects metrics for network |
| 53 // connectivity (either TCP or UDP). |
| 54 NetworkStats(); |
| 55 virtual ~NetworkStats(); |
| 56 |
| 57 // 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. |
| 59 // |finished_callback| is mainly useful for unittests. |
| 60 void Initialize(int bytes_to_send, |
| 61 net::CompletionCallback* finished_callback); |
| 62 |
| 63 // This method is called after socket connection is completed. It will send |
| 64 // |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 |
| 66 // from the |server|. Returns true if successful. |
| 67 bool DoStart(int result); |
| 68 |
| 69 // 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. |
| 71 virtual void Finish(Status status, int result) {} |
| 72 |
| 73 // This method is called from Finish() and calls |finished_callback_| callback |
| 74 // to indicate that the test has finished. |
| 75 void DoFinishCallback(int result); |
| 76 |
| 77 // Returns the number of bytes to be sent to the |server|. |
| 78 int load_size() const { return load_size_; } |
| 79 |
| 80 // Helper methods to get and set |socket_|. |
| 81 net::Socket* socket() { return socket_.get(); } |
| 82 void set_socket(net::Socket* socket); |
| 83 |
| 84 // Returns |start_time_| (used by histograms). |
| 85 base::TimeTicks start_time() const { return start_time_; } |
| 86 |
| 87 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. |
| 93 void OnReadComplete(int result); |
| 94 void OnWriteComplete(int result); |
| 95 |
| 96 // Reads data from server until an error occurs. |
| 97 void ReadData(); |
| 98 |
| 99 // Sends data to server until an error occurs. |
| 100 int SendData(); |
| 101 |
| 102 // The socket handle for this session. |
| 103 scoped_ptr<net::Socket> socket_; |
| 104 |
| 105 // The read buffer used to read data from the socket. |
| 106 scoped_refptr<net::IOBuffer> read_buffer_; |
| 107 |
| 108 // The write buffer used to write data to the socket. |
| 109 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
| 110 |
| 111 // Some counters for the session. |
| 112 int load_size_; |
| 113 int bytes_to_read_; |
| 114 int bytes_to_send_; |
| 115 |
| 116 // |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. |
| 118 net::TestDataStream stream_; |
| 119 |
| 120 // Callback to call when data is read from the server. |
| 121 net::CompletionCallbackImpl<NetworkStats> read_callback_; |
| 122 |
| 123 // Callback to call when data is sent to the server. |
| 124 net::CompletionCallbackImpl<NetworkStats> write_callback_; |
| 125 |
| 126 // 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 |
| 128 // round trip is finished). |
| 129 net::CompletionCallback* finished_callback_; |
| 130 |
| 131 // The time when the session was started. |
| 132 base::TimeTicks start_time_; |
| 133 }; |
| 134 |
| 135 class UDPStatsClient : public NetworkStats { |
| 136 public: |
| 137 // Constructs an UDPStatsClient object that collects metrics for UDP |
| 138 // connectivity. |
| 139 UDPStatsClient(); |
| 140 virtual ~UDPStatsClient(); |
| 141 |
| 142 // Starts the client, connecting to |server|. |
| 143 // Client will send |bytes_to_send| bytes to |server|. |
| 144 // When client has received all echoed bytes from the server, or |
| 145 // when an error occurs causing the client to stop, |Finish| will be |
| 146 // called with a net status code. |
| 147 // |Finish| will collect histogram stats. |
| 148 // Returns true if successful in starting the client. |
| 149 bool Start(const std::string& ip_str, |
| 150 int port, |
| 151 int bytes_to_send, |
| 152 net::CompletionCallback* callback); |
| 153 |
| 154 protected: |
| 155 // Allow tests to access our innards for testing purposes. |
| 156 friend class NetworkStatsTestUDP; |
| 157 |
| 158 // 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. |
| 160 virtual void Finish(Status status, int result); |
| 161 }; |
| 162 |
| 163 class TCPStatsClient : public NetworkStats { |
| 164 public: |
| 165 // Constructs a TCPStatsClient object that collects metrics for TCP |
| 166 // connectivity. |
| 167 TCPStatsClient(); |
| 168 virtual ~TCPStatsClient(); |
| 169 |
| 170 // Starts the client, connecting to |server|. |
| 171 // Client will send |bytes_to_send| bytes. |
| 172 // 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 |
| 174 // called with a net status code. |
| 175 // |Finish| will collect histogram stats. |
| 176 // Returns true if successful in starting the client. |
| 177 bool Start(net::HostResolver* host_resolver, |
| 178 const net::HostPortPair& server, |
| 179 int bytes_to_send, |
| 180 net::CompletionCallback* callback); |
| 181 |
| 182 protected: |
| 183 // Allow tests to access our innards for testing purposes. |
| 184 friend class NetworkStatsTestTCP; |
| 185 |
| 186 // 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. |
| 188 virtual void Finish(Status status, int result); |
| 189 |
| 190 private: |
| 191 // Callback that is called when host resolution is completed. |
| 192 void OnResolveComplete(int result); |
| 193 |
| 194 // Called after host is resolved. Creates TCPClientSocket and connects to the |
| 195 // server. |
| 196 bool DoConnect(int result); |
| 197 |
| 198 // Callback that is called when connect is completed and calls DoStart() to |
| 199 // start the echo protocl. |
| 200 void OnConnectComplete(int result); |
| 201 |
| 202 // Callback to call when host resolution is completed. |
| 203 net::CompletionCallbackImpl<TCPStatsClient> resolve_callback_; |
| 204 |
| 205 // Callback to call when connect is completed. |
| 206 net::CompletionCallbackImpl<TCPStatsClient> connect_callback_; |
| 207 |
| 208 // HostResolver fills out the |addresses_| after host resolution is completed. |
| 209 net::AddressList addresses_; |
| 210 }; |
| 211 |
| 212 // 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 |
| 214 // have enabled "UMA upload". This method gets called only if UMA upload to the |
| 215 // server has succeeded. |
| 216 void CollectNetworkStats(const std::string& network_stats_server_url, |
| 217 IOThread* io_thread); |
| 218 |
| 219 } // namespace chrome_browser_net |
| 220 |
| 221 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ |
OLD | NEW |