1// Copyright (c) 2011 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/time.h"
11#include "url/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 protocol_type = PROTOCOL_UNKNOWN;
21  if (url.SchemeIs("ws"))
22    protocol_type = PROTOCOL_WEBSOCKET;
23  else if (url.SchemeIs("wss"))
24    protocol_type = PROTOCOL_WEBSOCKET_SECURE;
25
26  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ProtocolType",
27                            protocol_type, NUM_PROTOCOL_TYPES);
28}
29
30SocketStreamMetrics::~SocketStreamMetrics() {}
31
32void SocketStreamMetrics::OnWaitConnection() {
33  wait_start_time_ = base::TimeTicks::Now();
34}
35
36void SocketStreamMetrics::OnStartConnection() {
37  connect_start_time_ = base::TimeTicks::Now();
38  if (!wait_start_time_.is_null())
39    UMA_HISTOGRAM_TIMES("Net.SocketStream.ConnectionLatency",
40                        connect_start_time_ - wait_start_time_);
41  OnCountConnectionType(ALL_CONNECTIONS);
42}
43
44void SocketStreamMetrics::OnConnected() {
45  connect_establish_time_ = base::TimeTicks::Now();
46  UMA_HISTOGRAM_TIMES("Net.SocketStream.ConnectionEstablish",
47                      connect_establish_time_ - connect_start_time_);
48}
49
50void SocketStreamMetrics::OnRead(int len) {
51  received_bytes_ += len;
52  ++received_counts_;
53}
54
55void SocketStreamMetrics::OnWrite(int len) {
56  sent_bytes_ += len;
57  ++sent_counts_;
58}
59
60void SocketStreamMetrics::OnClose() {
61  base::TimeTicks closed_time = base::TimeTicks::Now();
62  if (!connect_establish_time_.is_null()) {
63    UMA_HISTOGRAM_LONG_TIMES("Net.SocketStream.Duration",
64                             closed_time - connect_establish_time_);
65    UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedBytes",
66                         received_bytes_);
67    UMA_HISTOGRAM_COUNTS("Net.SocketStream.ReceivedCounts",
68                         received_counts_);
69    UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentBytes",
70                         sent_bytes_);
71    UMA_HISTOGRAM_COUNTS("Net.SocketStream.SentCounts",
72                         sent_counts_);
73  }
74}
75
76void SocketStreamMetrics::OnCountConnectionType(ConnectionType type) {
77  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.ConnectionType", type,
78                            NUM_CONNECTION_TYPES);
79}
80
81void SocketStreamMetrics::OnCountWireProtocolType(WireProtocolType type) {
82  UMA_HISTOGRAM_ENUMERATION("Net.SocketStream.WireProtocolType", type,
83                            NUM_WIRE_PROTOCOL_TYPES);
84}
85
86}  // namespace net
87