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 // Constructs an NetworkStats object that collects metrics for network | |
49 // connectivity (either TCP or UDP). | |
50 NetworkStats(); | |
51 virtual ~NetworkStats(); | |
52 | |
53 // Returns the number of errors this server encountered. | |
54 int error_count() { return errors_; } | |
55 | |
56 protected: | |
57 static const int kMaxMessage = 1024; | |
58 | |
59 // Verifies the data and calls Finish() if there is an error or if all bytes | |
60 // are read. Returns true if Finish() is called otherwise returns false. | |
61 virtual bool ReadComplete(int result); | |
62 | |
63 // Callbacks when an internal IO is completed. | |
64 virtual void OnReadComplete(int result); | |
65 virtual void OnWriteComplete(int result); | |
66 | |
67 // Reads data from server until an error occurs. | |
68 virtual void ReadData(); | |
69 | |
70 // Sends data to server until an error occurs. | |
71 virtual int SendData(); | |
72 | |
73 // Collects network connectivity stats. This is called when all the data from | |
74 // server is read or when there is a failure during connect/read/write. | |
75 virtual void Finish(Status status, int result) {} | |
willchan no longer on Chromium
2011/06/04 19:26:51
I think this is the only method that needs to be v
ramant (doing other things)
2011/06/05 17:54:13
unittests provide finish_callback_. If we couldn't
| |
76 | |
77 // The socket handle for this session. | |
willchan no longer on Chromium
2011/06/04 19:26:51
All these member variables should be private: http
ramant (doing other things)
2011/06/05 17:54:13
Done.
| |
78 scoped_ptr<net::Socket> socket_; | |
79 | |
80 // The read buffer used to read data from the socket. | |
81 scoped_refptr<net::IOBuffer> read_buffer_; | |
82 | |
83 // The write buffer used to write data to the socket. | |
84 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
85 | |
86 // Some counters for the session. | |
87 int errors_; | |
88 int load_size_; | |
89 int bytes_to_read_; | |
90 int bytes_to_send_; | |
91 | |
92 // |stream_| is used to generate data to be sent to the server and it is also | |
93 // used to verify the data received from the server. | |
94 net::TestDataStream stream_; | |
95 | |
96 // Callback to call when data is read from the server. | |
97 net::CompletionCallbackImpl<NetworkStats> read_callback_; | |
98 | |
99 // Callback to call when data is sent to the server. | |
100 net::CompletionCallbackImpl<NetworkStats> write_callback_; | |
101 | |
102 // Callback to call when echo protocol is successefully finished or whenever | |
103 // there is an error (this allows unittests to wait until echo protocol's | |
104 // round trip is finished). | |
105 net::CompletionCallback* finished_callback_; | |
106 | |
107 // The time when the session was started. | |
108 base::TimeTicks start_time_; | |
109 }; | |
110 | |
111 class UDPStatsClient : public base::RefCounted<UDPStatsClient>, NetworkStats { | |
112 public: | |
113 // Constructs an UDPStatsClient object that collects metrics for UDP | |
114 // connectivity. | |
115 UDPStatsClient(); | |
116 virtual ~UDPStatsClient(); | |
117 | |
118 // Starts the client, connecting to |server|. | |
119 // Client will send |bytes_to_send| bytes to |server|. | |
120 // When client has received all echoed bytes from the server, or | |
121 // when an error occurs causing the client to stop, |Finish| will be | |
122 // called with a net status code. | |
123 // |Finish| will collect histogram stats. | |
124 // Returns true if successful in starting the client. | |
125 bool Start(const std::string& ip_str, | |
126 int port, | |
127 int bytes_to_send, | |
128 net::CompletionCallback* callback); | |
129 | |
130 protected: | |
131 // Allow tests to access our innards for testing purposes. | |
132 friend class NetworkStatsTestUDP; | |
133 | |
134 friend class base::RefCounted<UDPStatsClient>; | |
135 | |
136 // Collects stats for UDP connectivity. This is called when all the data from | |
137 // server is read or when there is a failure during connect/read/write. | |
138 virtual void Finish(Status status, int result); | |
139 }; | |
140 | |
141 class TCPStatsClient : public base::RefCounted<TCPStatsClient>, NetworkStats { | |
142 public: | |
143 // Constructs a TCPStatsClient object that collects metrics for TCP | |
144 // connectivity. | |
145 TCPStatsClient(); | |
146 virtual ~TCPStatsClient(); | |
147 | |
148 // Starts the client, connecting to |server|. | |
149 // Client will send |bytes_to_send| bytes. | |
150 // When the client has received all echoed bytes from the server, or | |
151 // when an error occurs causing the client to stop, |Finish| will be | |
152 // called with a net status code. | |
153 // |Finish| will collect histogram stats. | |
154 // Returns true if successful in starting the client. | |
155 bool Start(const net::HostPortPair& server, | |
156 int bytes_to_send, | |
157 net::CompletionCallback* callback); | |
158 | |
159 protected: | |
160 // Allow tests to access our innards for testing purposes. | |
161 friend class NetworkStatsTestTCP; | |
162 | |
163 friend class base::RefCounted<TCPStatsClient>; | |
164 | |
165 // Callback that is called when connect is completed. | |
166 void OnConnectComplete(int result); | |
167 | |
168 // Collects stats for TCP connectivity. This is called when all the data from | |
169 // server is read or when there is a failure during connect/read/write. | |
170 virtual void Finish(Status status, int result); | |
171 | |
172 // Callback to call when connect is completed. | |
173 net::CompletionCallbackImpl<TCPStatsClient> connect_callback_; | |
174 }; | |
175 | |
176 // This collects the network connectivity stats for UDP and TCP for small | |
177 // percentage of users who are participating in the experiment. All users must | |
178 // have enabled "UMA upload". | |
179 void CollectNetworkStats(const std::string& network_stats_server_url); | |
180 | |
181 } // namespace chrome_browser_net | |
182 | |
183 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | |
OLD | NEW |