summaryrefslogtreecommitdiffstats
path: root/net/socket_stream/socket_stream_metrics.cc
blob: 108e829d9040d03499abde4ab1f52e4f47fc1aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/socket_stream/socket_stream_metrics.h"

#include <string.h>

#include "base/metrics/histogram.h"
#include "base/time.h"
#include "googleurl/src/gurl.h"

namespace net {

SocketStreamMetrics::SocketStreamMetrics(const GURL& url)
    : received_bytes_(0),
      received_counts_(0),
      sent_bytes_(0),
      sent_counts_(0) {
  ProtocolType proto_type = PROTOCOL_UNKNOWN;
  if (url.SchemeIs("ws"))
    proto_type = PROTOCOL_WEBSOCKET;
  else if (url.SchemeIs("wss"))
    proto_type = PROTOCOL_WEBSOCKET_SECURE;

  CountProtocolType(proto_type);
}

SocketStreamMetrics::~SocketStreamMetrics() {}

void SocketStreamMetrics::OnWaitConnection() {
  wait_start_time_ = base::TimeTicks::Now();
}

void SocketStreamMetrics::OnStartConnection() {
  connect_start_time_ = base::TimeTicks::Now();
  if (!wait_start_time_.is_null())
    UMA_HISTOGRAM_TIMES("Net.SocketStream.ConnectionLatency",
                        connect_start_time_ - wait_start_time_);
  CountConnectionType(ALL_CONNECTIONS);
}

void SocketStreamMetrics::OnTunnelProxy() {
  CountConnectionType(TUNNEL_CONNECTION);
}

void SocketStreamMetrics::OnSOCKSProxy() {
  CountConnectionType(SOCKS_CONNECTION);
}

void SocketStreamMetrics::OnSSLConnection() {
  CountConnectionType(SSL_CONNECTION);
}

void SocketStreamMetrics::OnConnected() {
  connect_establish_time_ = base::TimeTicks::Now();
  UMA_HISTOGRAM_TIMES("Net.SocketStream.ConnectionEstablish",
                      connect_establish_time_ - connect_start_time_);
}

void SocketStreamMetrics::OnRead(int len) {
  received_bytes_ += len;
  ++received_counts_;
}

void SocketStreamMetrics::OnWrite(int len) {
  sent_bytes_ += len;
  ++sent_counts_;
}

void SocketStreamMetrics::OnClose() {
  base::TimeTicks closed_time = base::TimeTicks::Now();
  if (!connect_establish_time_.is_null()) {
    UMA_HISTOGRAM_LONG_TIMES("Net.SocketStream.Duration",
                             closed_time - connect_establish_time_);
    UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedBytes",
                         received_bytes_);
    UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedCounts",
                         received_counts_);
    UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentBytes",
                         sent_bytes_);
    UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentCounts",
                         sent_counts_);
  }
}

void SocketStreamMetrics::CountProtocolType(ProtocolType protocol_type) {
  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ProtocolType",
       protocol_type, NUM_PROTOCOL_TYPES);
}

void SocketStreamMetrics::CountConnectionType(ConnectionType connection_type) {
  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ConnectionType",
       connection_type, NUM_CONNECTION_TYPES);
}


}  // namespace net