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. 4 5 #include "net/socket_stream/socket_stream_metrics.h" 6 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" 15 16 using base::Histogram; 17 using base::HistogramBase; 18 using base::HistogramSamples; 19 using base::StatisticsRecorder; 20 21 namespace net { 22 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(); 32 } 33 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/")); 40 41 histogram = 42 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType"); 43 ASSERT_TRUE(histogram != NULL); 44 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); 45 46 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); 47 if (original.get()) { 48 samples->Subtract(*original); // Cancel the original values. 49 } 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)); 54 } 55 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(); 63 } 64 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); 74 75 76 histogram = 77 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType"); 78 ASSERT_TRUE(histogram != NULL); 79 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); 80 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 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(); 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 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; 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