15254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu// Use of this source code is governed by a BSD-style license that can be
35254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu// found in the LICENSE file.
45254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
55254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "net/socket_stream/socket_stream_metrics.h"
65254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
75254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "base/basictypes.h"
85254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "base/memory/scoped_ptr.h"
95254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "base/metrics/histogram.h"
105254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "base/metrics/histogram_samples.h"
115254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "base/metrics/statistics_recorder.h"
125254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "testing/gtest/include/gtest/gtest.h"
135254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "testing/platform_test.h"
145254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu#include "url/gurl.h"
155254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
165254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieuusing base::Histogram;
175254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieuusing base::HistogramBase;
185254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieuusing base::HistogramSamples;
195254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieuusing base::StatisticsRecorder;
205254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
215254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieunamespace net {
225254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
235254161b269829b74e7a9379b1bdfa27de72d7ccRichard TrieuTEST(SocketStreamMetricsTest, ProtocolType) {
245254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  // First we'll preserve the original values. We need to do this
255254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  // as histograms can get affected by other tests. In particular,
265254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  // SocketStreamTest and WebSocketTest can affect the histograms.
275254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  scoped_ptr<HistogramSamples> original;
28651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  HistogramBase* histogram =
29651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType");
30651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (histogram) {
31651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    original = histogram->SnapshotSamples();
32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
33651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
34651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SocketStreamMetrics unknown(GURL("unknown://www.example.com/"));
35651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SocketStreamMetrics ws1(GURL("ws://www.example.com/"));
365254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  SocketStreamMetrics ws2(GURL("ws://www.example.com/"));
37651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SocketStreamMetrics wss1(GURL("wss://www.example.com/"));
38651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SocketStreamMetrics wss2(GURL("wss://www.example.com/"));
39651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  SocketStreamMetrics wss3(GURL("wss://www.example.com/"));
40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
41651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  histogram =
42651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType");
43651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ASSERT_TRUE(histogram != NULL);
44651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
455254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
465254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
475254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  if (original.get()) {
485254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu    samples->Subtract(*original);  // Cancel the original values.
495254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  }
505254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::PROTOCOL_UNKNOWN));
515254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET));
525254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  EXPECT_EQ(3,
535254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu            samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE));
545254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu}
555254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
565254161b269829b74e7a9379b1bdfa27de72d7ccRichard TrieuTEST(SocketStreamMetricsTest, ConnectionType) {
575254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  // First we'll preserve the original values.
585254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  scoped_ptr<HistogramSamples> original;
595254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  HistogramBase* histogram =
605254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu      StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType");
615254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  if (histogram) {
625254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu    original = histogram->SnapshotSamples();
635254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  }
645254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
655254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
665254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  for (int i = 0; i < 1; ++i)
675254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu    metrics.OnStartConnection();
685254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  for (int i = 0; i < 2; ++i)
695254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu    metrics.OnCountConnectionType(SocketStreamMetrics::TUNNEL_CONNECTION);
705254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  for (int i = 0; i < 3; ++i)
715254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu    metrics.OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION);
725254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  for (int i = 0; i < 4; ++i)
735254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu    metrics.OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION);
745254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
755254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
765254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  histogram =
775254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu      StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType");
785254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  ASSERT_TRUE(histogram != NULL);
795254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu  EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
805254161b269829b74e7a9379b1bdfa27de72d7ccRichard Trieu
81  scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
82  if (original.get()) {
83    samples->Subtract(*original);  // Cancel the original values.
84  }
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));
89}
90
91TEST(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();
98  }
99
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);
106
107  histogram =
108      StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType");
109  ASSERT_TRUE(histogram != NULL);
110  EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags());
111
112  scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
113  if (original.get()) {
114    samples->Subtract(*original);  // Cancel the original values.
115  }
116  EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET));
117  EXPECT_EQ(7, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_SPDY));
118}
119
120TEST(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;
126
127  scoped_ptr<HistogramSamples> original;
128
129  HistogramBase* histogram =
130      StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes");
131  if (histogram) {
132    original = histogram->SnapshotSamples();
133    original_received_bytes = original->sum();
134  }
135  histogram =
136      StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts");
137  if (histogram) {
138    original = histogram->SnapshotSamples();
139    original_received_counts = original->sum();
140  }
141  histogram =
142      StatisticsRecorder::FindHistogram("Net.SocketStream.SentBytes");
143  if (histogram) {
144    original = histogram->SnapshotSamples();
145    original_sent_bytes = original->sum();
146  }
147  histogram =
148      StatisticsRecorder::FindHistogram("Net.SocketStream.SentCounts");
149  if (histogram) {
150    original = histogram->SnapshotSamples();
151    original_sent_counts = original->sum();
152  }
153
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();
164
165  scoped_ptr<HistogramSamples> samples;
166
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.
173
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.
180
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.
187
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.
195
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.
203
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.
211
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.
219}
220
221}  // namespace net
222