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"
16 using base::Histogram
;
17 using base::HistogramBase
;
18 using base::HistogramSamples
;
19 using base::StatisticsRecorder
;
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");
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/"));
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());
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
));
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");
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
);
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());
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");
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
);
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");
132 original
= histogram
->SnapshotSamples();
133 original_received_bytes
= original
->sum();
136 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts");
138 original
= histogram
->SnapshotSamples();
139 original_received_counts
= original
->sum();
142 StatisticsRecorder::FindHistogram("Net.SocketStream.SentBytes");
144 original
= histogram
->SnapshotSamples();
145 original_sent_bytes
= original
->sum();
148 StatisticsRecorder::FindHistogram("Net.SocketStream.SentCounts");
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();
162 metrics
.OnWrite(200);
165 scoped_ptr
<HistogramSamples
> samples
;
167 // ConnectionLatency.
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.
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.
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.
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.
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.
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.
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.