Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / socket_stream / socket_stream_metrics_unittest.cc
blob219e692a4e89937fa0f46023ded0ee6efec083a7
1 // Copyright (c) 2012 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.
5 #include "net/socket_stream/socket_stream_metrics.h"
7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/histogram.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
14 #include "url/gurl.h"
16 using base::Histogram;
17 using base::HistogramBase;
18 using base::HistogramSamples;
19 using base::StatisticsRecorder;
21 namespace net {
23 TEST(SocketStreamMetricsTest, ProtocolType) {
24 // First we'll preserve the original values. We need to do this
25 // as histograms can get affected by other tests. In particular,
26 // SocketStreamTest and WebSocketTest can affect the histograms.
27 scoped_ptr<HistogramSamples> original;
28 HistogramBase* histogram =
29 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType");
30 if (histogram) {
31 original = histogram->SnapshotSamples();
34 SocketStreamMetrics unknown(GURL("unknown://www.example.com/"));
35 SocketStreamMetrics ws1(GURL("ws://www.example.com/"));
36 SocketStreamMetrics ws2(GURL("ws://www.example.com/"));
37 SocketStreamMetrics wss1(GURL("wss://www.example.com/"));
38 SocketStreamMetrics wss2(GURL("wss://www.example.com/"));
39 SocketStreamMetrics wss3(GURL("wss://www.example.com/"));
41 histogram =
42 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType");
43 ASSERT_TRUE(histogram != NULL);
44 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
46 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
47 if (original.get()) {
48 samples->Subtract(*original); // Cancel the original values.
50 EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::PROTOCOL_UNKNOWN));
51 EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET));
52 EXPECT_EQ(3,
53 samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE));
56 TEST(SocketStreamMetricsTest, ConnectionType) {
57 // First we'll preserve the original values.
58 scoped_ptr<HistogramSamples> original;
59 HistogramBase* histogram =
60 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType");
61 if (histogram) {
62 original = histogram->SnapshotSamples();
65 SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
66 for (int i = 0; i < 1; ++i)
67 metrics.OnStartConnection();
68 for (int i = 0; i < 2; ++i)
69 metrics.OnCountConnectionType(SocketStreamMetrics::TUNNEL_CONNECTION);
70 for (int i = 0; i < 3; ++i)
71 metrics.OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION);
72 for (int i = 0; i < 4; ++i)
73 metrics.OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION);
76 histogram =
77 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType");
78 ASSERT_TRUE(histogram != NULL);
79 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
81 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
82 if (original.get()) {
83 samples->Subtract(*original); // Cancel the original values.
85 EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::ALL_CONNECTIONS));
86 EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::TUNNEL_CONNECTION));
87 EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::SOCKS_CONNECTION));
88 EXPECT_EQ(4, samples->GetCount(SocketStreamMetrics::SSL_CONNECTION));
91 TEST(SocketStreamMetricsTest, WireProtocolType) {
92 // First we'll preserve the original values.
93 scoped_ptr<HistogramSamples> original;
94 HistogramBase* histogram =
95 StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType");
96 if (histogram) {
97 original = histogram->SnapshotSamples();
100 SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
101 for (int i = 0; i < 3; ++i)
102 metrics.OnCountWireProtocolType(
103 SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET);
104 for (int i = 0; i < 7; ++i)
105 metrics.OnCountWireProtocolType(SocketStreamMetrics::WIRE_PROTOCOL_SPDY);
107 histogram =
108 StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType");
109 ASSERT_TRUE(histogram != NULL);
110 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
112 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
113 if (original.get()) {
114 samples->Subtract(*original); // Cancel the original values.
116 EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET));
117 EXPECT_EQ(7, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_SPDY));
120 TEST(SocketStreamMetricsTest, OtherNumbers) {
121 // First we'll preserve the original values.
122 int64 original_received_bytes = 0;
123 int64 original_received_counts = 0;
124 int64 original_sent_bytes = 0;
125 int64 original_sent_counts = 0;
127 scoped_ptr<HistogramSamples> original;
129 HistogramBase* histogram =
130 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes");
131 if (histogram) {
132 original = histogram->SnapshotSamples();
133 original_received_bytes = original->sum();
135 histogram =
136 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts");
137 if (histogram) {
138 original = histogram->SnapshotSamples();
139 original_received_counts = original->sum();
141 histogram =
142 StatisticsRecorder::FindHistogram("Net.SocketStream.SentBytes");
143 if (histogram) {
144 original = histogram->SnapshotSamples();
145 original_sent_bytes = original->sum();
147 histogram =
148 StatisticsRecorder::FindHistogram("Net.SocketStream.SentCounts");
149 if (histogram) {
150 original = histogram->SnapshotSamples();
151 original_sent_counts = original->sum();
154 SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
155 metrics.OnWaitConnection();
156 metrics.OnStartConnection();
157 metrics.OnConnected();
158 metrics.OnRead(1);
159 metrics.OnRead(10);
160 metrics.OnWrite(2);
161 metrics.OnWrite(20);
162 metrics.OnWrite(200);
163 metrics.OnClose();
165 scoped_ptr<HistogramSamples> samples;
167 // ConnectionLatency.
168 histogram =
169 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionLatency");
170 ASSERT_TRUE(histogram != NULL);
171 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
172 // We don't check the contents of the histogram as it's time sensitive.
174 // ConnectionEstablish.
175 histogram =
176 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionEstablish");
177 ASSERT_TRUE(histogram != NULL);
178 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
179 // We don't check the contents of the histogram as it's time sensitive.
181 // Duration.
182 histogram =
183 StatisticsRecorder::FindHistogram("Net.SocketStream.Duration");
184 ASSERT_TRUE(histogram != NULL);
185 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
186 // We don't check the contents of the histogram as it's time sensitive.
188 // ReceivedBytes.
189 histogram =
190 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes");
191 ASSERT_TRUE(histogram != NULL);
192 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
193 samples = histogram->SnapshotSamples();
194 EXPECT_EQ(11, samples->sum() - original_received_bytes); // 11 bytes read.
196 // ReceivedCounts.
197 histogram =
198 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts");
199 ASSERT_TRUE(histogram != NULL);
200 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
201 samples = histogram->SnapshotSamples();
202 EXPECT_EQ(2, samples->sum() - original_received_counts); // 2 read requests.
204 // SentBytes.
205 histogram =
206 StatisticsRecorder::FindHistogram("Net.SocketStream.SentBytes");
207 ASSERT_TRUE(histogram != NULL);
208 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
209 samples = histogram->SnapshotSamples();
210 EXPECT_EQ(222, samples->sum() - original_sent_bytes); // 222 bytes sent.
212 // SentCounts.
213 histogram =
214 StatisticsRecorder::FindHistogram("Net.SocketStream.SentCounts");
215 ASSERT_TRUE(histogram != NULL);
216 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
217 samples = histogram->SnapshotSamples();
218 EXPECT_EQ(3, samples->sum() - original_sent_counts); // 3 write requests.
221 } // namespace net