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
13namespace net {
14
15SocketStreamMetrics::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
29SocketStreamMetrics::~SocketStreamMetrics() {}
30
31void SocketStreamMetrics::OnWaitConnection() {
32  wait_start_time_ = base::TimeTicks::Now();
33}
34
35void 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
43void SocketStreamMetrics::OnTunnelProxy() {
44  CountConnectionType(TUNNEL_CONNECTION);
45}
46
47void SocketStreamMetrics::OnSOCKSProxy() {
48  CountConnectionType(SOCKS_CONNECTION);
49}
50
51void SocketStreamMetrics::OnSSLConnection() {
52  CountConnectionType(SSL_CONNECTION);
53}
54
55void 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
61void SocketStreamMetrics::OnRead(int len) {
62  received_bytes_ += len;
63  ++received_counts_;
64}
65
66void SocketStreamMetrics::OnWrite(int len) {
67  sent_bytes_ += len;
68  ++sent_counts_;
69}
70
71void 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
87void SocketStreamMetrics::CountProtocolType(ProtocolType protocol_type) {
88  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ProtocolType",
89       protocol_type, NUM_PROTOCOL_TYPES);
90}
91
92void 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