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/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 UMA_HISTOGRAM_LONG_TIMES("Net.SocketStream.Duration",
74 closed_time - connect_establish_time_);
75 UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedBytes",
76 received_bytes_);
77 UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedCounts",
78 received_counts_);
79 UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentBytes",
80 sent_bytes_);
81 UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentCounts",
82 sent_counts_);
83 }
84
CountProtocolType(ProtocolType protocol_type)85 void SocketStreamMetrics::CountProtocolType(ProtocolType protocol_type) {
86 UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ProtocolType",
87 protocol_type, NUM_PROTOCOL_TYPES);
88 }
89
CountConnectionType(ConnectionType connection_type)90 void SocketStreamMetrics::CountConnectionType(ConnectionType connection_type) {
91 UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ConnectionType",
92 connection_type, NUM_CONNECTION_TYPES);
93 }
94
95
96 } // namespace net
97