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 "net/base/completion_callback.h" | |
16 #include "net/base/host_port_pair.h" | |
17 #include "net/base/io_buffer.h" | |
18 #include "net/base/ip_endpoint.h" | |
19 #include "net/base/test_data_stream.h" | |
20 #include "net/socket/socket.h" | |
21 | |
22 namespace chrome_browser_net { | |
23 | |
24 // This class is used for live experiment of network connectivity (either TCP or | |
25 // UDP) metrics. A small percentage of users participate in this experiment. All | |
26 // users (who are in the experiment) must have enabled "UMA upload". | |
27 // | |
28 // This class collects the following stats from users who have opted in. | |
29 // a) What percentage of users can get a message end-to-end to a UDP server? | |
30 // b) What percentage of users can get a message end-to-end to a TCP server? | |
31 // c) What is the latency for UDP and TCP. | |
32 // d) If connectivity failed, at what stage (Connect or Write or Read) did it | |
33 // fail? | |
34 | |
35 class NetworkStats { | |
36 public: | |
37 enum Status { // Used in HISTOGRAM_ENUMERATION. | |
38 SUCCESS, // Successfully received bytes from the server. | |
39 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. | |
40 RESOLVE_FAILED, // Host resolution failed. | |
41 CONNECT_FAILED, // Connection to the server failed. | |
42 WRITE_FAILED, // Sending an echo message to the server failed. | |
43 READ_FAILED, // Reading the reply from the server failed. | |
44 READ_VERIFY_FAILED, // Verification of data failed. | |
45 STATUS_MAX, // Bounding value. | |
46 }; | |
47 | |
48 protected: | |
49 // Constructs an NetworkStats object that collects metrics for network | |
50 // connectivity (either TCP or UDP). | |
51 NetworkStats(); | |
52 virtual ~NetworkStats(); | |
53 | |
54 // Initializes |finished_callback_| and the number of bytes to send to the | |
55 // server. |finished_callback| is called when we are done with the test. | |
56 // |finished_callback| is mainly useful for unittests. | |
57 void Initialize(int bytes_to_send, | |
58 net::CompletionCallback* finished_callback); | |
59 | |
60 // This method is called after socket connection is completed. It will send | |
61 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully | |
62 // sending data to the |server|, it calls ReadData() to read/verify the data | |
63 // from the |server|. Returns true if successful. | |
64 bool ConnectComplete(int result); | |
65 | |
66 // Collects network connectivity stats. This is called when all the data from | |
67 // server is read or when there is a failure during connect/read/write. | |
68 virtual void Finish(Status status, int result) {} | |
69 | |
70 // Calls |finished_callback_| to indicate that the test has finished. | |
71 void DoFinishCallback(int result); | |
72 | |
73 // Returns the number of bytes to be sent to the |server|. | |
74 int load_size() { return load_size_; } | |
75 | |
76 // Helper methods to get and set |socket_|. | |
77 const scoped_ptr<net::Socket>& socket() { return socket_; } | |
willchan no longer on Chromium
2011/06/06 09:25:49
I think you should just return socket_.get();, unl
ramant (doing other things)
2011/06/07 20:54:00
Done.
| |
78 void set_socket(net::Socket* socket); | |
79 | |
80 // Returns |start_time_| (used by histograms). | |
81 base::TimeTicks start_time() const { return start_time_; } | |
82 | |
83 private: | |
84 static const int kMaxMessage = 2048; | |
willchan no longer on Chromium
2011/06/06 09:25:49
If this is only used privately, this it's an imple
ramant (doing other things)
2011/06/07 20:54:00
Done.
| |
85 | |
86 // Verifies the data and calls Finish() if there is an error or if all bytes | |
87 // are read. Returns true if Finish() is called otherwise returns false. | |
88 bool ReadComplete(int result); | |
89 | |
90 // Callbacks when an internal IO is completed. | |
91 void OnReadComplete(int result); | |
92 void OnWriteComplete(int result); | |
93 | |
94 // Reads data from server until an error occurs. | |
95 void ReadData(); | |
96 | |
97 // Sends data to server until an error occurs. | |
98 int SendData(); | |
99 | |
100 // The socket handle for this session. | |
101 scoped_ptr<net::Socket> socket_; | |
102 | |
103 // The read buffer used to read data from the socket. | |
104 scoped_refptr<net::IOBuffer> read_buffer_; | |
105 | |
106 // The write buffer used to write data to the socket. | |
107 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
108 | |
109 // Some counters for the session. | |
110 int load_size_; | |
111 int bytes_to_read_; | |
112 int bytes_to_send_; | |
113 | |
114 // |stream_| is used to generate data to be sent to the server and it is also | |
115 // used to verify the data received from the server. | |
116 net::TestDataStream stream_; | |
117 | |
118 // Callback to call when data is read from the server. | |
119 net::CompletionCallbackImpl<NetworkStats> read_callback_; | |
120 | |
121 // Callback to call when data is sent to the server. | |
122 net::CompletionCallbackImpl<NetworkStats> write_callback_; | |
123 | |
124 // Callback to call when echo protocol is successefully finished or whenever | |
125 // there is an error (this allows unittests to wait until echo protocol's | |
126 // round trip is finished). | |
127 net::CompletionCallback* finished_callback_; | |
128 | |
129 // The time when the session was started. | |
130 base::TimeTicks start_time_; | |
131 }; | |
132 | |
133 class UDPStatsClient : public base::RefCounted<UDPStatsClient>, NetworkStats { | |
134 public: | |
135 // Constructs an UDPStatsClient object that collects metrics for UDP | |
136 // connectivity. | |
137 UDPStatsClient(); | |
138 virtual ~UDPStatsClient(); | |
139 | |
140 // Starts the client, connecting to |server|. | |
141 // Client will send |bytes_to_send| bytes to |server|. | |
142 // When client has received all echoed bytes from the server, or | |
143 // when an error occurs causing the client to stop, |Finish| will be | |
144 // called with a net status code. | |
145 // |Finish| will collect histogram stats. | |
146 // Returns true if successful in starting the client. | |
147 bool Start(const std::string& ip_str, | |
148 int port, | |
149 int bytes_to_send, | |
150 net::CompletionCallback* callback); | |
151 | |
152 protected: | |
153 // Allow tests to access our innards for testing purposes. | |
154 friend class NetworkStatsTestUDP; | |
155 | |
156 friend class base::RefCounted<UDPStatsClient>; | |
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 base::RefCounted<TCPStatsClient>, 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(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 friend class base::RefCounted<TCPStatsClient>; | |
186 | |
187 // Collects stats for TCP connectivity. This is called when all the data from | |
188 // server is read or when there is a failure during connect/read/write. | |
189 virtual void Finish(Status status, int result); | |
190 | |
191 private: | |
192 // Callback that is called when connect is completed. | |
193 void OnConnectComplete(int result); | |
194 | |
195 // Callback to call when connect is completed. | |
196 net::CompletionCallbackImpl<TCPStatsClient> connect_callback_; | |
197 }; | |
198 | |
199 // This collects the network connectivity stats for UDP and TCP for small | |
200 // percentage of users who are participating in the experiment. All users must | |
201 // have enabled "UMA upload". | |
202 void CollectNetworkStats(const std::string& network_stats_server_url); | |
203 | |
204 } // namespace chrome_browser_net | |
205 | |
206 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | |
OLD | NEW |