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