summaryrefslogtreecommitdiffstats
path: root/net/socket_stream/socket_stream_metrics.cc
blob: 7ef92bb24eebd862f1d955f3af86ea156af20054 (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
// Copyright (c) 2011 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 protocol_type = PROTOCOL_UNKNOWN;
  if (url.SchemeIs("ws"))
    protocol_type = PROTOCOL_WEBSOCKET;
  else if (url.SchemeIs("wss"))
    protocol_type = PROTOCOL_WEBSOCKET_SECURE;

  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ProtocolType",
                            protocol_type, NUM_PROTOCOL_TYPES);
}

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_);
  OnCountConnectionType(ALL_CONNECTIONS);
}

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::OnCountConnectionType(ConnectionType type) {
  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ConnectionType", type,
                            NUM_CONNECTION_TYPES);
}

void SocketStreamMetrics::OnCountWireProtocolType(WireProtocolType type) {
  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.WireProtocolType", type,
                            NUM_WIRE_PROTOCOL_TYPES);
}

}  // namespace net