summaryrefslogtreecommitdiffstats
path: root/net/socket_stream/socket_stream_metrics_unittest.cc
blob: 23f227d8df8595b21c1498b7c2e3a14674922ea3 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// 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 "base/basictypes.h"
#include "base/metrics/histogram.h"
#include "googleurl/src/gurl.h"
#include "net/socket_stream/socket_stream_metrics.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using base::Histogram;
using base::StatisticsRecorder;

namespace net {

TEST(SocketStreamMetricsTest, Initialize) {
  if (!StatisticsRecorder::IsActive()) {
    // Create the recorder if not yet started, as SocketStreamMetrics
    // relys on the StatisticsRecorder to be present. This is useful when
    // tests are run with --gtest_filter='SocketStreamMetricsTest*'.
    static StatisticsRecorder *recorder = NULL;
    recorder = new StatisticsRecorder;
  }
}

TEST(SocketStreamMetricsTest, ProtocolType) {
  Histogram* histogram;

  // First we'll preserve the original values. We need to do this
  // as histograms can get affected by other tests. In particular,
  // SocketStreamTest and WebSocketTest can affect the histograms.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ProtocolType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics unknown(GURL("unknown://www.example.com/"));
  SocketStreamMetrics ws1(GURL("ws://www.example.com/"));
  SocketStreamMetrics ws2(GURL("ws://www.example.com/"));
  SocketStreamMetrics wss1(GURL("wss://www.example.com/"));
  SocketStreamMetrics wss2(GURL("wss://www.example.com/"));
  SocketStreamMetrics wss3(GURL("wss://www.example.com/"));

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ProtocolType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);  // Ensure |original| size is same as |sample|.
  sample.Subtract(original); // Cancel the original values.
  EXPECT_EQ(1, sample.counts(SocketStreamMetrics::PROTOCOL_UNKNOWN));
  EXPECT_EQ(2, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET));
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE));
}

TEST(SocketStreamMetricsTest, ConnectionType) {
  Histogram* histogram;

  // First we'll preserve the original values.
  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ConnectionType", &histogram)) {
    histogram->SnapshotSample(&original);
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  for (int i = 0; i < 1; ++i)
    metrics.OnStartConnection();
  for (int i = 0; i < 2; ++i)
    metrics.OnTunnelProxy();
  for (int i = 0; i < 3; ++i)
    metrics.OnSOCKSProxy();
  for (int i = 0; i < 4; ++i)
    metrics.OnSSLConnection();

  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionType", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());

  Histogram::SampleSet sample;
  histogram->SnapshotSample(&sample);
  original.Resize(*histogram);
  sample.Subtract(original);
  EXPECT_EQ(1, sample.counts(SocketStreamMetrics::ALL_CONNECTIONS));
  EXPECT_EQ(2, sample.counts(SocketStreamMetrics::TUNNEL_CONNECTION));
  EXPECT_EQ(3, sample.counts(SocketStreamMetrics::SOCKS_CONNECTION));
  EXPECT_EQ(4, sample.counts(SocketStreamMetrics::SSL_CONNECTION));
}

TEST(SocketStreamMetricsTest, OtherNumbers) {
  Histogram* histogram;

  // First we'll preserve the original values.
  int64 original_received_bytes = 0;
  int64 original_received_counts = 0;
  int64 original_sent_bytes = 0;
  int64 original_sent_counts = 0;

  Histogram::SampleSet original;
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ReceivedBytes", &histogram)) {
    histogram->SnapshotSample(&original);
    original_received_bytes = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.ReceivedCounts", &histogram)) {
    histogram->SnapshotSample(&original);
    original_received_counts = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.SentBytes", &histogram)) {
    histogram->SnapshotSample(&original);
    original_sent_bytes = original.sum();
  }
  if (StatisticsRecorder::FindHistogram(
          "Net.SocketStream.SentCounts", &histogram)) {
    histogram->SnapshotSample(&original);
    original_sent_counts = original.sum();
  }

  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
  metrics.OnWaitConnection();
  metrics.OnStartConnection();
  metrics.OnConnected();
  metrics.OnRead(1);
  metrics.OnRead(10);
  metrics.OnWrite(2);
  metrics.OnWrite(20);
  metrics.OnWrite(200);
  metrics.OnClose();

  Histogram::SampleSet sample;

  // ConnectionLatency.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionLatency", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // ConnectionEstablish.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ConnectionEstablish", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // Duration.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.Duration", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  // We don't check the contents of the histogram as it's time sensitive.

  // ReceivedBytes.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ReceivedBytes", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(11, sample.sum() - original_received_bytes);  // 11 bytes read.

  // ReceivedCounts.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.ReceivedCounts", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(2, sample.sum() - original_received_counts);  // 2 read requests.

  // SentBytes.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.SentBytes", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(222, sample.sum() - original_sent_bytes);  // 222 bytes sent.

  // SentCounts.
  ASSERT_TRUE(StatisticsRecorder::FindHistogram(
      "Net.SocketStream.SentCounts", &histogram));
  EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
  histogram->SnapshotSample(&sample);
  EXPECT_EQ(3, sample.sum() - original_sent_counts);  // 3 write requests.
}

}  // namespace net