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/scoped_ptr.h" | |
13 #include "base/time.h" | |
14 #include "net/base/completion_callback.h" | |
15 #include "net/base/host_port_pair.h" | |
16 #include "net/base/io_buffer.h" | |
17 #include "net/base/ip_endpoint.h" | |
18 #include "net/base/test_data_stream.h" | |
19 #include "net/socket/socket.h" | |
20 | |
21 namespace chrome_browser_net { | |
22 | |
23 // This class is used for live experiment of network connectivity (either TCP or | |
24 // UDP) metrics. A small percentage of users participate in this experiment. All | |
25 // users (who are in the experiment) must have enabled "UMA upload". | |
26 // | |
27 // This class collects the following stats from users who have opted in. | |
28 // a) What percentage of users can get a message end-to-end to a UDP server? | |
29 // b) What percentage of users can get a message end-to-end to a TCP server? | |
30 // c) What is the latency for UDP and TCP. | |
31 // d) If connectivity failed, at what stage (Connect or Write or Read) did it | |
32 // fail? | |
33 | |
34 class NetworkStats : public base::RefCounted<NetworkStats> { | |
35 public: | |
36 enum Status { // Used in HISTOGRAM_ENUMERATION. | |
37 SUCCESS, // Successfully received bytes from the server. | |
38 IP_STRING_PARSE_FAIL, // Parsing of IP string failed. | |
39 RESOLVE_FAIL, // Host resolution failed. | |
40 CONNECT_FAIL, // Connection to the server failed. | |
41 WRITE_FAIL, // Sending an echo message to the server failed. | |
42 READ_FAIL, // Reading the reply from the server failed. | |
43 READ_VERIFY_FAIL, // Verification of data failed. | |
44 STATUS_MAX, // Bounding value. | |
45 }; | |
46 | |
47 // Constructs an NetworkStats object that collects metrics for network | |
48 // connectivity (either TCP or UDP). | |
49 NetworkStats(); | |
50 virtual ~NetworkStats(); | |
51 | |
52 // Returns the number of errors this server encountered. | |
53 int error_count() { return errors_; } | |
54 | |
55 protected: | |
56 static const int kMaxMessage = 1024; | |
57 | |
58 // Callbacks when an internal IO is completed. | |
59 virtual void OnReadComplete(int result); | |
60 virtual void OnWriteComplete(int result); | |
61 | |
62 // Reads data from server until an error occurs. | |
63 virtual void ReadData(); | |
64 | |
65 // Sends data to server until an error occurs. | |
66 virtual void SendData(); | |
67 | |
68 // Collects network connectivity stats. This is called when all the data from | |
69 // server is read or when there is a failure during connect/read/write. | |
70 virtual void Finish(Status status, int result) {} | |
71 | |
72 // The socket handle for this session. | |
73 net::Socket* socket_; | |
74 | |
75 // The read buffer used to read data from the socket. | |
76 scoped_refptr<net::IOBuffer> read_buffer_; | |
77 | |
78 // The write buffer used to write data to the socket. | |
79 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
80 | |
81 // Some counters for the session. | |
82 int errors_; | |
83 int bytes_to_read_; | |
84 int bytes_to_send_; | |
85 | |
86 // Streams of generated data to be sent to the server. | |
87 net::TestDataStream sent_stream_; | |
88 | |
89 // Streams of data received from the server that can be independently verified | |
90 // as the correct stream of data. | |
91 net::TestDataStream received_stream_; | |
Mike Belshe
2011/05/27 21:27:00
BTW - I think you only need one stream of test dat
ramant (doing other things)
2011/05/31 21:25:36
Done.
| |
92 | |
93 // Callback to call when data is read from the server. | |
94 net::CompletionCallbackImpl<NetworkStats> read_callback_; | |
95 | |
96 // Callback to call when data is sent to the server. | |
97 net::CompletionCallbackImpl<NetworkStats> write_callback_; | |
98 | |
99 // Callback to call when echo protocol is successefully finished or whenever | |
100 // there is an error (this allows unittests to wait until echo protocol's | |
101 // round trip is finished). | |
102 net::CompletionCallback* finished_callback_; | |
103 | |
104 // The time when the session was started. | |
105 base::TimeTicks start_time_; | |
106 }; | |
107 | |
108 class UDPStatsClient : public NetworkStats { | |
109 public: | |
110 // Constructs an UDPStatsClient object that collects metrics for UDP | |
111 // connectivity. | |
112 UDPStatsClient(); | |
113 virtual ~UDPStatsClient(); | |
114 | |
115 // Starts the client, connecting to |server|. | |
116 // Client will send |bytes_to_send| bytes to |server|. | |
117 // When client has received all echoed bytes from the server, or | |
118 // when an error occurs causing the client to stop, |Finish| will be | |
119 // called with a net status code. | |
120 // |Finish| will collect histogram stats. | |
121 // Returns true if successful in starting the client. | |
122 bool Start(const std::string& ip_str, | |
123 int port, | |
124 int bytes_to_send, | |
125 net::CompletionCallback* callback); | |
126 | |
127 protected: | |
128 // Collects stats for UDP connectivity. This is called when all the data from | |
129 // server is read or when there is a failure during connect/read/write. | |
130 virtual void Finish(Status status, int result); | |
131 }; | |
132 | |
133 class TCPStatsClient : public NetworkStats { | |
134 public: | |
135 // Constructs a TCPStatsClient object that collects metrics for TCP | |
136 // connectivity. | |
137 TCPStatsClient(); | |
138 virtual ~TCPStatsClient(); | |
139 | |
140 // Starts the client, connecting to |server|. | |
141 // Client will send |bytes_to_send| bytes. | |
142 // When the 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 net::HostPortPair& server, | |
148 int bytes_to_send, | |
149 net::CompletionCallback* callback); | |
150 | |
151 protected: | |
152 // Callback that is called when connect is completed. | |
153 void OnConnectComplete(int result); | |
154 | |
155 // Collects stats for TCP connectivity. This is called when all the data from | |
156 // server is read or when there is a failure during connect/read/write. | |
157 virtual void Finish(Status status, int result); | |
158 | |
159 // Callback to call when connect is completed. | |
160 net::CompletionCallbackImpl<TCPStatsClient> connect_callback_; | |
161 }; | |
162 | |
163 // This collects the network connectivity stats for UDP and TCP for small | |
164 // percentage of users who are participating in the experiment. All users must | |
165 // have enabled "UMA upload". | |
166 void CollectNetworkStats(const std::string& network_stats_server_url); | |
167 | |
168 } // namespace chrome_browser_net | |
169 | |
170 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ | |
OLD | NEW |