Chromium Code Reviews| 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 // Calls |finished_callback_| to indicate that the test has finished. | |
| 74 void DoFinishCallback(int result); | |
| 75 | |
| 76 // Returns the number of bytes to be sent to the |server|. | |
| 77 int load_size() const { return load_size_; } | |
| 78 | |
| 79 // Helper methods to get and set |socket_|. | |
| 80 net::Socket* socket() { return socket_; } | |
| 81 void set_socket(net::Socket* socket); | |
| 82 | |
| 83 // Returns |start_time_| (used by histograms). | |
| 84 base::TimeTicks start_time() const { return start_time_; } | |
| 85 | |
| 86 private: | |
| 87 // Verifies the data and calls Finish() if there is an error or if all bytes | |
| 88 // are read. Returns true if Finish() is called otherwise returns false. | |
| 89 bool ReadComplete(int result); | |
| 90 | |
| 91 // Callbacks when an internal IO is completed. | |
| 92 void OnReadComplete(int result); | |
| 93 void OnWriteComplete(int result); | |
| 94 | |
| 95 // Reads data from server until an error occurs. | |
| 96 void ReadData(); | |
| 97 | |
| 98 // Sends data to server until an error occurs. | |
| 99 int SendData(); | |
| 100 | |
| 101 // The socket handle for this session. | |
| 102 net::Socket* socket_; | |
|
willchan no longer on Chromium
2011/06/08 14:07:02
Why isn't this a scoped_ptr?
ramant (doing other things)
2011/06/08 17:47:17
Done.
| |
| 103 | |
| 104 // The read buffer used to read data from the socket. | |
| 105 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 106 | |
| 107 // The write buffer used to write data to the socket. | |
| 108 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
| 109 | |
| 110 // Some counters for the session. | |
| 111 int load_size_; | |
| 112 int bytes_to_read_; | |
| 113 int bytes_to_send_; | |
| 114 | |
| 115 // |stream_| is used to generate data to be sent to the server and it is also | |
| 116 // used to verify the data received from the server. | |
| 117 net::TestDataStream stream_; | |
| 118 | |
| 119 // Callback to call when data is read from the server. | |
| 120 net::CompletionCallbackImpl<NetworkStats> read_callback_; | |
| 121 | |
| 122 // Callback to call when data is sent to the server. | |
| 123 net::CompletionCallbackImpl<NetworkStats> write_callback_; | |
| 124 | |
| 125 // Callback to call when echo protocol is successefully finished or whenever | |
| 126 // there is an error (this allows unittests to wait until echo protocol's | |
| 127 // round trip is finished). | |
| 128 net::CompletionCallback* finished_callback_; | |
| 129 | |
| 130 // The time when the session was started. | |
| 131 base::TimeTicks start_time_; | |
| 132 }; | |
| 133 | |
| 134 class UDPStatsClient : public NetworkStats { | |
| 135 public: | |
| 136 // Constructs an UDPStatsClient object that collects metrics for UDP | |
| 137 // connectivity. | |
| 138 UDPStatsClient(); | |
| 139 virtual ~UDPStatsClient(); | |
| 140 | |
| 141 // Starts the client, connecting to |server|. | |
| 142 // Client will send |bytes_to_send| bytes to |server|. | |
| 143 // When client has received all echoed bytes from the server, or | |
| 144 // when an error occurs causing the client to stop, |Finish| will be | |
| 145 // called with a net status code. | |
| 146 // |Finish| will collect histogram stats. | |
| 147 // Returns true if successful in starting the client. | |
| 148 bool Start(const std::string& ip_str, | |
| 149 int port, | |
| 150 int bytes_to_send, | |
| 151 net::CompletionCallback* callback); | |
| 152 | |
| 153 protected: | |
| 154 // Allow tests to access our innards for testing purposes. | |
| 155 friend class NetworkStatsTestUDP; | |
| 156 | |
| 157 // Collects stats for UDP connectivity. This is called when all the data from | |
| 158 // server is read or when there is a failure during connect/read/write. | |
| 159 virtual void Finish(Status status, int result); | |
| 160 }; | |
| 161 | |
| 162 class TCPStatsClient : public NetworkStats { | |
| 163 public: | |
| 164 // Constructs a TCPStatsClient object that collects metrics for TCP | |
| 165 // connectivity. | |
| 166 TCPStatsClient(); | |
| 167 virtual ~TCPStatsClient(); | |
| 168 | |
| 169 // Starts the client, connecting to |server|. | |
| 170 // Client will send |bytes_to_send| bytes. | |
| 171 // When the client has received all echoed bytes from the server, or | |
| 172 // when an error occurs causing the client to stop, |Finish| will be | |
| 173 // called with a net status code. | |
| 174 // |Finish| will collect histogram stats. | |
| 175 // Returns true if successful in starting the client. | |
| 176 bool Start(net::HostResolver* host_resolver, | |
| 177 const net::HostPortPair& server, | |
| 178 int bytes_to_send, | |
| 179 net::CompletionCallback* callback); | |
| 180 | |
| 181 protected: | |
| 182 // Allow tests to access our innards for testing purposes. | |
| 183 friend class NetworkStatsTestTCP; | |
| 184 | |
| 185 // Collects stats for TCP connectivity. This is called when all the data from | |
| 186 // server is read or when there is a failure during connect/read/write. | |
| 187 virtual void Finish(Status status, int result); | |
| 188 | |
| 189 private: | |
| 190 // Callback that is called when host resolution is completed. | |
| 191 void OnResolveComplete(int result); | |
| 192 | |
| 193 // Called after host is resolved. Creates TCPClientSocket and connects to the | |
| 194 // server. | |
| 195 bool DoConnect(int result); | |
| 196 | |
| 197 // Callback that is called when connect is completed and calls DoStart() to | |
| 198 // start the echo protocl. | |
| 199 void OnConnectComplete(int result); | |
| 200 | |
| 201 // Callback to call when host resolution is completed. | |
| 202 net::CompletionCallbackImpl<TCPStatsClient> resolve_callback_; | |
| 203 | |
| 204 // Callback to call when connect is completed. | |
| 205 net::CompletionCallbackImpl<TCPStatsClient> connect_callback_; | |
| 206 | |
| 207 // HostResolver fills out the |addresses_| after host resolution is completed. | |
| 208 net::AddressList addresses_; | |
| 209 }; | |
| 210 | |
| 211 // This collects the network connectivity stats for UDP and TCP for small | |
| 212 // percentage of users who are participating in the experiment. All users must | |
| 213 // have enabled "UMA upload". This method gets called only if UMA upload to the | |
| 214 // server has succeeded. | |
| 215 void CollectNetworkStats(const std::string& network_stats_server_url, | |
| 216 IOThread* io_thread); | |
| 217 | |
| 218 } // namespace chrome_browser_net | |
| 219 | |
| 220 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | |
| OLD | NEW |