1 // Copyright (c) 2009 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 #include "net/socket_stream/socket_stream_metrics.h"
6
7 #include <string.h>
8
9 #include "base/metrics/histogram.h"
10 #include "base/time.h"
11 #include "googleurl/src/gurl.h"
12
13 namespace net {
14
SocketStreamMetrics(const GURL & url)15 SocketStreamMetrics::SocketStreamMetrics(const GURL& url)
16 : received_bytes_(0),
17 received_counts_(0),
18 sent_bytes_(0),
19 sent_counts_(0) {
20 ProtocolType proto_type = PROTOCOL_UNKNOWN;
21 if (url.SchemeIs("ws"))
22 proto_type = PROTOCOL_WEBSOCKET;
23 else if (url.SchemeIs("wss"))
24 proto_type = PROTOCOL_WEBSOCKET_SECURE;
25
26 CountProtocolType(proto_type);
27 }
28
~SocketStreamMetrics()29 SocketStreamMetrics::~SocketStreamMetrics() {}
30
OnWaitConnection()31 void SocketStreamMetrics::OnWaitConnection() {
32 wait_start_time_ = base::TimeTicks::Now();
33 }
34
OnStartConnection()35 void SocketStreamMetrics::OnStartConnection() {
36 connect_start_time_ = base::TimeTicks::Now();
37 if (!wait_start_time_.is_null())
38 UMA_HISTOGRAM_TIMES("Net.SocketStream.ConnectionLatency",
39 connect_start_time_ - wait_start_time_);
40 CountConnectionType(ALL_CONNECTIONS);
41 }
42
OnTunnelProxy()43 void SocketStreamMetrics::OnTunnelProxy() {
44 CountConnectionType(TUNNEL_CONNECTION);
45 }
46
OnSOCKSProxy()47 void SocketStreamMetrics::OnSOCKSProxy() {
48 CountConnectionType(SOCKS_CONNECTION);
49 }
50
OnSSLConnection()51 void SocketStreamMetrics::OnSSLConnection() {
52 CountConnectionType(SSL_CONNECTION);
53 }
54
OnConnected()55 void SocketStreamMetrics::OnConnected() {
56 connect_establish_time_ = base::TimeTicks::Now();
57 UMA_HISTOGRAM_TIMES("Net.SocketStream.ConnectionEstablish",
58 connect_establish_time_ - connect_start_time_);
59 }
60
OnRead(int len)61 void SocketStreamMetrics::OnRead(int len) {
62 received_bytes_ += len;
63 ++received_counts_;
64 }
65
OnWrite(int len)66 void SocketStreamMetrics::OnWrite(int len) {
67 sent_bytes_ += len;
68 ++sent_counts_;
69 }
70
OnClose()71 void SocketStreamMetrics::OnClose() {
72 base::TimeTicks closed_time = base::TimeTicks::Now();
73 if (!connect_establish_time_.is_null()) {
74 UMA_HISTOGRAM_LONG_TIMES("Net.SocketStream.Duration",
75 closed_time - connect_establish_time_);
76 UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedBytes",
77 received_bytes_);
78 UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedCounts",
79 received_counts_);
80 UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentBytes",
81 sent_bytes_);
82 UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentCounts",
83 sent_counts_);
84 }
85 }
86
CountProtocolType(ProtocolType protocol_type)87 void SocketStreamMetrics::CountProtocolType(ProtocolType protocol_type) {
88 UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ProtocolType",
89 protocol_type, NUM_PROTOCOL_TYPES);
90 }
91
CountConnectionType(ConnectionType connection_type)92 void SocketStreamMetrics::CountConnectionType(ConnectionType connection_type) {
93 UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ConnectionType",
94 connection_type, NUM_CONNECTION_TYPES);
95 }
96
97
98 } // namespace net
99