| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket_stream/socket_stream_metrics.h" | 5 #include "net/socket_stream/socket_stream_metrics.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/histogram_samples.h" | 10 #include "base/metrics/histogram_samples.h" |
| 11 #include "base/metrics/statistics_recorder.h" | 11 #include "base/metrics/statistics_recorder.h" |
| 12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 15 | 15 |
| 16 using base::Histogram; | 16 using base::Histogram; |
| 17 using base::HistogramBase; |
| 17 using base::HistogramSamples; | 18 using base::HistogramSamples; |
| 18 using base::StatisticsRecorder; | 19 using base::StatisticsRecorder; |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 22 TEST(SocketStreamMetricsTest, ProtocolType) { | 23 TEST(SocketStreamMetricsTest, ProtocolType) { |
| 23 // First we'll preserve the original values. We need to do this | 24 // First we'll preserve the original values. We need to do this |
| 24 // as histograms can get affected by other tests. In particular, | 25 // as histograms can get affected by other tests. In particular, |
| 25 // SocketStreamTest and WebSocketTest can affect the histograms. | 26 // SocketStreamTest and WebSocketTest can affect the histograms. |
| 26 scoped_ptr<HistogramSamples> original; | 27 scoped_ptr<HistogramSamples> original; |
| 27 Histogram* histogram = | 28 HistogramBase* histogram = |
| 28 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType"); | 29 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType"); |
| 29 if (histogram) { | 30 if (histogram) { |
| 30 original = histogram->SnapshotSamples(); | 31 original = histogram->SnapshotSamples(); |
| 31 } | 32 } |
| 32 | 33 |
| 33 SocketStreamMetrics unknown(GURL("unknown://www.example.com/")); | 34 SocketStreamMetrics unknown(GURL("unknown://www.example.com/")); |
| 34 SocketStreamMetrics ws1(GURL("ws://www.example.com/")); | 35 SocketStreamMetrics ws1(GURL("ws://www.example.com/")); |
| 35 SocketStreamMetrics ws2(GURL("ws://www.example.com/")); | 36 SocketStreamMetrics ws2(GURL("ws://www.example.com/")); |
| 36 SocketStreamMetrics wss1(GURL("wss://www.example.com/")); | 37 SocketStreamMetrics wss1(GURL("wss://www.example.com/")); |
| 37 SocketStreamMetrics wss2(GURL("wss://www.example.com/")); | 38 SocketStreamMetrics wss2(GURL("wss://www.example.com/")); |
| 38 SocketStreamMetrics wss3(GURL("wss://www.example.com/")); | 39 SocketStreamMetrics wss3(GURL("wss://www.example.com/")); |
| 39 | 40 |
| 40 histogram = | 41 histogram = |
| 41 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType"); | 42 StatisticsRecorder::FindHistogram("Net.SocketStream.ProtocolType"); |
| 42 ASSERT_TRUE(histogram != NULL); | 43 ASSERT_TRUE(histogram != NULL); |
| 43 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 44 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 44 | 45 |
| 45 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | 46 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| 46 if (original.get()) { | 47 if (original.get()) { |
| 47 samples->Subtract(*original); // Cancel the original values. | 48 samples->Subtract(*original); // Cancel the original values. |
| 48 } | 49 } |
| 49 EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::PROTOCOL_UNKNOWN)); | 50 EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::PROTOCOL_UNKNOWN)); |
| 50 EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET)); | 51 EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET)); |
| 51 EXPECT_EQ(3, | 52 EXPECT_EQ(3, |
| 52 samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE)); | 53 samples->GetCount(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE)); |
| 53 } | 54 } |
| 54 | 55 |
| 55 TEST(SocketStreamMetricsTest, ConnectionType) { | 56 TEST(SocketStreamMetricsTest, ConnectionType) { |
| 56 // First we'll preserve the original values. | 57 // First we'll preserve the original values. |
| 57 scoped_ptr<HistogramSamples> original; | 58 scoped_ptr<HistogramSamples> original; |
| 58 Histogram* histogram = | 59 HistogramBase* histogram = |
| 59 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType"); | 60 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType"); |
| 60 if (histogram) { | 61 if (histogram) { |
| 61 original = histogram->SnapshotSamples(); | 62 original = histogram->SnapshotSamples(); |
| 62 } | 63 } |
| 63 | 64 |
| 64 SocketStreamMetrics metrics(GURL("ws://www.example.com/")); | 65 SocketStreamMetrics metrics(GURL("ws://www.example.com/")); |
| 65 for (int i = 0; i < 1; ++i) | 66 for (int i = 0; i < 1; ++i) |
| 66 metrics.OnStartConnection(); | 67 metrics.OnStartConnection(); |
| 67 for (int i = 0; i < 2; ++i) | 68 for (int i = 0; i < 2; ++i) |
| 68 metrics.OnCountConnectionType(SocketStreamMetrics::TUNNEL_CONNECTION); | 69 metrics.OnCountConnectionType(SocketStreamMetrics::TUNNEL_CONNECTION); |
| 69 for (int i = 0; i < 3; ++i) | 70 for (int i = 0; i < 3; ++i) |
| 70 metrics.OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION); | 71 metrics.OnCountConnectionType(SocketStreamMetrics::SOCKS_CONNECTION); |
| 71 for (int i = 0; i < 4; ++i) | 72 for (int i = 0; i < 4; ++i) |
| 72 metrics.OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION); | 73 metrics.OnCountConnectionType(SocketStreamMetrics::SSL_CONNECTION); |
| 73 | 74 |
| 74 | 75 |
| 75 histogram = | 76 histogram = |
| 76 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType"); | 77 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionType"); |
| 77 ASSERT_TRUE(histogram != NULL); | 78 ASSERT_TRUE(histogram != NULL); |
| 78 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 79 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 79 | 80 |
| 80 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | 81 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| 81 if (original.get()) { | 82 if (original.get()) { |
| 82 samples->Subtract(*original); // Cancel the original values. | 83 samples->Subtract(*original); // Cancel the original values. |
| 83 } | 84 } |
| 84 EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::ALL_CONNECTIONS)); | 85 EXPECT_EQ(1, samples->GetCount(SocketStreamMetrics::ALL_CONNECTIONS)); |
| 85 EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::TUNNEL_CONNECTION)); | 86 EXPECT_EQ(2, samples->GetCount(SocketStreamMetrics::TUNNEL_CONNECTION)); |
| 86 EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::SOCKS_CONNECTION)); | 87 EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::SOCKS_CONNECTION)); |
| 87 EXPECT_EQ(4, samples->GetCount(SocketStreamMetrics::SSL_CONNECTION)); | 88 EXPECT_EQ(4, samples->GetCount(SocketStreamMetrics::SSL_CONNECTION)); |
| 88 } | 89 } |
| 89 | 90 |
| 90 TEST(SocketStreamMetricsTest, WireProtocolType) { | 91 TEST(SocketStreamMetricsTest, WireProtocolType) { |
| 91 // First we'll preserve the original values. | 92 // First we'll preserve the original values. |
| 92 scoped_ptr<HistogramSamples> original; | 93 scoped_ptr<HistogramSamples> original; |
| 93 Histogram* histogram = | 94 HistogramBase* histogram = |
| 94 StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType"); | 95 StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType"); |
| 95 if (histogram) { | 96 if (histogram) { |
| 96 original = histogram->SnapshotSamples(); | 97 original = histogram->SnapshotSamples(); |
| 97 } | 98 } |
| 98 | 99 |
| 99 SocketStreamMetrics metrics(GURL("ws://www.example.com/")); | 100 SocketStreamMetrics metrics(GURL("ws://www.example.com/")); |
| 100 for (int i = 0; i < 3; ++i) | 101 for (int i = 0; i < 3; ++i) |
| 101 metrics.OnCountWireProtocolType( | 102 metrics.OnCountWireProtocolType( |
| 102 SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET); | 103 SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET); |
| 103 for (int i = 0; i < 7; ++i) | 104 for (int i = 0; i < 7; ++i) |
| 104 metrics.OnCountWireProtocolType(SocketStreamMetrics::WIRE_PROTOCOL_SPDY); | 105 metrics.OnCountWireProtocolType(SocketStreamMetrics::WIRE_PROTOCOL_SPDY); |
| 105 | 106 |
| 106 histogram = | 107 histogram = |
| 107 StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType"); | 108 StatisticsRecorder::FindHistogram("Net.SocketStream.WireProtocolType"); |
| 108 ASSERT_TRUE(histogram != NULL); | 109 ASSERT_TRUE(histogram != NULL); |
| 109 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 110 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 110 | 111 |
| 111 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); | 112 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples()); |
| 112 if (original.get()) { | 113 if (original.get()) { |
| 113 samples->Subtract(*original); // Cancel the original values. | 114 samples->Subtract(*original); // Cancel the original values. |
| 114 } | 115 } |
| 115 EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET)); | 116 EXPECT_EQ(3, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_WEBSOCKET)); |
| 116 EXPECT_EQ(7, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_SPDY)); | 117 EXPECT_EQ(7, samples->GetCount(SocketStreamMetrics::WIRE_PROTOCOL_SPDY)); |
| 117 } | 118 } |
| 118 | 119 |
| 119 TEST(SocketStreamMetricsTest, OtherNumbers) { | 120 TEST(SocketStreamMetricsTest, OtherNumbers) { |
| 120 // First we'll preserve the original values. | 121 // First we'll preserve the original values. |
| 121 int64 original_received_bytes = 0; | 122 int64 original_received_bytes = 0; |
| 122 int64 original_received_counts = 0; | 123 int64 original_received_counts = 0; |
| 123 int64 original_sent_bytes = 0; | 124 int64 original_sent_bytes = 0; |
| 124 int64 original_sent_counts = 0; | 125 int64 original_sent_counts = 0; |
| 125 | 126 |
| 126 scoped_ptr<HistogramSamples> original; | 127 scoped_ptr<HistogramSamples> original; |
| 127 | 128 |
| 128 Histogram* histogram = | 129 HistogramBase* histogram = |
| 129 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes"); | 130 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes"); |
| 130 if (histogram) { | 131 if (histogram) { |
| 131 original = histogram->SnapshotSamples(); | 132 original = histogram->SnapshotSamples(); |
| 132 original_received_bytes = original->sum(); | 133 original_received_bytes = original->sum(); |
| 133 } | 134 } |
| 134 histogram = | 135 histogram = |
| 135 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts"); | 136 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts"); |
| 136 if (histogram) { | 137 if (histogram) { |
| 137 original = histogram->SnapshotSamples(); | 138 original = histogram->SnapshotSamples(); |
| 138 original_received_counts = original->sum(); | 139 original_received_counts = original->sum(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 160 metrics.OnWrite(20); | 161 metrics.OnWrite(20); |
| 161 metrics.OnWrite(200); | 162 metrics.OnWrite(200); |
| 162 metrics.OnClose(); | 163 metrics.OnClose(); |
| 163 | 164 |
| 164 scoped_ptr<HistogramSamples> samples; | 165 scoped_ptr<HistogramSamples> samples; |
| 165 | 166 |
| 166 // ConnectionLatency. | 167 // ConnectionLatency. |
| 167 histogram = | 168 histogram = |
| 168 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionLatency"); | 169 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionLatency"); |
| 169 ASSERT_TRUE(histogram != NULL); | 170 ASSERT_TRUE(histogram != NULL); |
| 170 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 171 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 171 // We don't check the contents of the histogram as it's time sensitive. | 172 // We don't check the contents of the histogram as it's time sensitive. |
| 172 | 173 |
| 173 // ConnectionEstablish. | 174 // ConnectionEstablish. |
| 174 histogram = | 175 histogram = |
| 175 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionEstablish"); | 176 StatisticsRecorder::FindHistogram("Net.SocketStream.ConnectionEstablish"); |
| 176 ASSERT_TRUE(histogram != NULL); | 177 ASSERT_TRUE(histogram != NULL); |
| 177 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 178 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 178 // We don't check the contents of the histogram as it's time sensitive. | 179 // We don't check the contents of the histogram as it's time sensitive. |
| 179 | 180 |
| 180 // Duration. | 181 // Duration. |
| 181 histogram = | 182 histogram = |
| 182 StatisticsRecorder::FindHistogram("Net.SocketStream.Duration"); | 183 StatisticsRecorder::FindHistogram("Net.SocketStream.Duration"); |
| 183 ASSERT_TRUE(histogram != NULL); | 184 ASSERT_TRUE(histogram != NULL); |
| 184 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 185 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 185 // We don't check the contents of the histogram as it's time sensitive. | 186 // We don't check the contents of the histogram as it's time sensitive. |
| 186 | 187 |
| 187 // ReceivedBytes. | 188 // ReceivedBytes. |
| 188 histogram = | 189 histogram = |
| 189 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes"); | 190 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedBytes"); |
| 190 ASSERT_TRUE(histogram != NULL); | 191 ASSERT_TRUE(histogram != NULL); |
| 191 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 192 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 192 samples = histogram->SnapshotSamples(); | 193 samples = histogram->SnapshotSamples(); |
| 193 EXPECT_EQ(11, samples->sum() - original_received_bytes); // 11 bytes read. | 194 EXPECT_EQ(11, samples->sum() - original_received_bytes); // 11 bytes read. |
| 194 | 195 |
| 195 // ReceivedCounts. | 196 // ReceivedCounts. |
| 196 histogram = | 197 histogram = |
| 197 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts"); | 198 StatisticsRecorder::FindHistogram("Net.SocketStream.ReceivedCounts"); |
| 198 ASSERT_TRUE(histogram != NULL); | 199 ASSERT_TRUE(histogram != NULL); |
| 199 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 200 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 200 samples = histogram->SnapshotSamples(); | 201 samples = histogram->SnapshotSamples(); |
| 201 EXPECT_EQ(2, samples->sum() - original_received_counts); // 2 read requests. | 202 EXPECT_EQ(2, samples->sum() - original_received_counts); // 2 read requests. |
| 202 | 203 |
| 203 // SentBytes. | 204 // SentBytes. |
| 204 histogram = | 205 histogram = |
| 205 StatisticsRecorder::FindHistogram("Net.SocketStream.SentBytes"); | 206 StatisticsRecorder::FindHistogram("Net.SocketStream.SentBytes"); |
| 206 ASSERT_TRUE(histogram != NULL); | 207 ASSERT_TRUE(histogram != NULL); |
| 207 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 208 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 208 samples = histogram->SnapshotSamples(); | 209 samples = histogram->SnapshotSamples(); |
| 209 EXPECT_EQ(222, samples->sum() - original_sent_bytes); // 222 bytes sent. | 210 EXPECT_EQ(222, samples->sum() - original_sent_bytes); // 222 bytes sent. |
| 210 | 211 |
| 211 // SentCounts. | 212 // SentCounts. |
| 212 histogram = | 213 histogram = |
| 213 StatisticsRecorder::FindHistogram("Net.SocketStream.SentCounts"); | 214 StatisticsRecorder::FindHistogram("Net.SocketStream.SentCounts"); |
| 214 ASSERT_TRUE(histogram != NULL); | 215 ASSERT_TRUE(histogram != NULL); |
| 215 EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags()); | 216 EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, histogram->flags()); |
| 216 samples = histogram->SnapshotSamples(); | 217 samples = histogram->SnapshotSamples(); |
| 217 EXPECT_EQ(3, samples->sum() - original_sent_counts); // 3 write requests. | 218 EXPECT_EQ(3, samples->sum() - original_sent_counts); // 3 write requests. |
| 218 } | 219 } |
| 219 | 220 |
| 220 } // namespace net | 221 } // namespace net |
| OLD | NEW |