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