diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-15 08:04:40 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-15 08:04:40 +0000 |
commit | 26a645902ab659f2365e4cbd4a739e2ba8a9ce94 (patch) | |
tree | 23aee7d731bd9ca5ccfb5df57ec77cb61021bacd | |
parent | 5e6fef06b01974607105482ed637727e7686bab5 (diff) | |
download | chromium_src-26a645902ab659f2365e4cbd4a739e2ba8a9ce94.zip chromium_src-26a645902ab659f2365e4cbd4a739e2ba8a9ce94.tar.gz chromium_src-26a645902ab659f2365e4cbd4a739e2ba8a9ce94.tar.bz2 |
Add tests for SocketStreamMetrics.
Tests for SocketStreamMetrics were missing, which is not great.
Note that the tests became a bit more complex than I originally
thought as the global histograms are shared by other tests in
other files.
TEST=net_unittests
BUG=none
Review URL: http://codereview.chromium.org/491045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34536 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/base/run_all_unittests.cc | 3 | ||||
-rw-r--r-- | net/net.gyp | 1 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_metrics.h | 3 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_metrics_unittest.cc | 181 |
4 files changed, 187 insertions, 1 deletions
diff --git a/net/base/run_all_unittests.cc b/net/base/run_all_unittests.cc index d26b1e1..9786f6e 100644 --- a/net/base/run_all_unittests.cc +++ b/net/base/run_all_unittests.cc @@ -27,9 +27,12 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "base/histogram.h" #include "net/base/net_test_suite.h" int main(int argc, char** argv) { + // Record histograms, so we can get histograms data in tests. + StatisticsRecorder recorder; NetTestSuite test_suite(argc, argv); // TODO(phajdan.jr): Enforce test isolation, http://crbug.com/12710. return test_suite.Run(); diff --git a/net/net.gyp b/net/net.gyp index 4c3fe53..d95ffd7 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -664,6 +664,7 @@ 'socket/tcp_client_socket_pool_unittest.cc', 'socket/tcp_client_socket_unittest.cc', 'socket/tcp_pinger_unittest.cc', + 'socket_stream/socket_stream_metrics_unittest.cc', 'socket_stream/socket_stream_unittest.cc', 'url_request/request_tracker_unittest.cc', 'url_request/url_request_unittest.cc', diff --git a/net/socket_stream/socket_stream_metrics.h b/net/socket_stream/socket_stream_metrics.h index 67b1e33a0..44cbc7b 100644 --- a/net/socket_stream/socket_stream_metrics.h +++ b/net/socket_stream/socket_stream_metrics.h @@ -32,7 +32,6 @@ class SocketStreamMetrics { void OnWrite(int len); void OnClose(); - private: enum ProtocolType { PROTOCOL_UNKNOWN, PROTOCOL_WEBSOCKET, @@ -47,6 +46,8 @@ class SocketStreamMetrics { SSL_CONNECTION, NUM_CONNECTION_TYPES, }; + + private: void CountProtocolType(ProtocolType protocol_type); void CountConnectionType(ConnectionType connection_type); diff --git a/net/socket_stream/socket_stream_metrics_unittest.cc b/net/socket_stream/socket_stream_metrics_unittest.cc new file mode 100644 index 0000000..e29ee3f --- /dev/null +++ b/net/socket_stream/socket_stream_metrics_unittest.cc @@ -0,0 +1,181 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/basictypes.h" +#include "base/histogram.h" +#include "googleurl/src/gurl.h" +#include "net/socket_stream/socket_stream_metrics.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +namespace net { + +TEST(SocketStreamMetricsTest, Initialize) { + if (!StatisticsRecorder::WasStarted()) { + // Create the recorder if not yet started, as SocketStreamMetrics + // relys on the StatisticsRecorder to be present. This is useful when + // tests are run with --gtest_filter='SocketStreamMetricsTest*'. + static StatisticsRecorder *recorder = NULL; + recorder = new StatisticsRecorder; + } +} + +TEST(SocketStreamMetricsTest, ProtocolType) { + scoped_refptr<Histogram> histogram; + + // First we'll preserve the original values. We need to do this + // as histograms can get affected by other tests. In particular, + // SocketStreamTest and WebSocketTest can affect the histograms. + Histogram::SampleSet original; + if (StatisticsRecorder::FindHistogram( + "Net.SocketStream.ProtocolType", &histogram)) { + histogram->SnapshotSample(&original); + } + + SocketStreamMetrics unknown(GURL("unknown://www.example.com/")); + SocketStreamMetrics ws1(GURL("ws://www.example.com/")); + SocketStreamMetrics ws2(GURL("ws://www.example.com/")); + SocketStreamMetrics wss1(GURL("wss://www.example.com/")); + SocketStreamMetrics wss2(GURL("wss://www.example.com/")); + SocketStreamMetrics wss3(GURL("wss://www.example.com/")); + + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.ProtocolType", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + + Histogram::SampleSet sample; + histogram->SnapshotSample(&sample); + original.Resize(*histogram); // Ensure |original| size is same as |sample|. + sample.Subtract(original); // Cancel the original values. + EXPECT_EQ(1, sample.counts(SocketStreamMetrics::PROTOCOL_UNKNOWN)); + EXPECT_EQ(2, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET)); + EXPECT_EQ(3, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE)); +} + +TEST(SocketStreamMetricsTest, ConnectionType) { + scoped_refptr<Histogram> histogram; + + // First we'll preserve the original values. + Histogram::SampleSet original; + if (StatisticsRecorder::FindHistogram( + "Net.SocketStream.ConnectionType", &histogram)) { + histogram->SnapshotSample(&original); + } + + SocketStreamMetrics metrics(GURL("ws://www.example.com/")); + for (int i = 0; i < 1; ++i) + metrics.OnStartConnection(); + for (int i = 0; i < 2; ++i) + metrics.OnTunnelProxy(); + for (int i = 0; i < 3; ++i) + metrics.OnSOCKSProxy(); + for (int i = 0; i < 4; ++i) + metrics.OnSSLConnection(); + + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.ConnectionType", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + + Histogram::SampleSet sample; + histogram->SnapshotSample(&sample); + original.Resize(*histogram); + sample.Subtract(original); + EXPECT_EQ(1, sample.counts(SocketStreamMetrics::ALL_CONNECTIONS)); + EXPECT_EQ(2, sample.counts(SocketStreamMetrics::TUNNEL_CONNECTION)); + EXPECT_EQ(3, sample.counts(SocketStreamMetrics::SOCKS_CONNECTION)); + EXPECT_EQ(4, sample.counts(SocketStreamMetrics::SSL_CONNECTION)); +} + +TEST(SocketStreamMetricsTest, OtherNumbers) { + scoped_refptr<Histogram> histogram; + + // First we'll preserve the original values. + int64 original_received_bytes = 0; + int64 original_received_counts = 0; + int64 original_sent_bytes = 0; + int64 original_sent_counts = 0; + + Histogram::SampleSet original; + if (StatisticsRecorder::FindHistogram( + "Net.SocketStream.ReceivedBytes", &histogram)) { + histogram->SnapshotSample(&original); + original_received_bytes = original.sum(); + } + if (StatisticsRecorder::FindHistogram( + "Net.SocketStream.ReceivedCounts", &histogram)) { + histogram->SnapshotSample(&original); + original_received_counts = original.sum(); + } + if (StatisticsRecorder::FindHistogram( + "Net.SocketStream.SentBytes", &histogram)) { + histogram->SnapshotSample(&original); + original_sent_bytes = original.sum(); + } + if (StatisticsRecorder::FindHistogram( + "Net.SocketStream.SentCounts", &histogram)) { + histogram->SnapshotSample(&original); + original_sent_counts = original.sum(); + } + + SocketStreamMetrics metrics(GURL("ws://www.example.com/")); + metrics.OnWaitConnection(); + metrics.OnStartConnection(); + metrics.OnConnected(); + metrics.OnRead(1); + metrics.OnRead(10); + metrics.OnWrite(2); + metrics.OnWrite(20); + metrics.OnWrite(200); + metrics.OnClose(); + + Histogram::SampleSet sample; + + // ConnectionLatency. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.ConnectionLatency", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + // We don't check the contents of the histogram as it's time sensitive. + + // ConnectionEstablish. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.ConnectionEstablish", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + // We don't check the contents of the histogram as it's time sensitive. + + // Duration. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.Duration", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + // We don't check the contents of the histogram as it's time sensitive. + + // ReceivedBytes. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.ReceivedBytes", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + histogram->SnapshotSample(&sample); + EXPECT_EQ(11, sample.sum() - original_received_bytes); // 11 bytes read. + + // ReceivedCounts. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.ReceivedCounts", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + histogram->SnapshotSample(&sample); + EXPECT_EQ(2, sample.sum() - original_received_counts); // 2 read requests. + + // SentBytes. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.SentBytes", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + histogram->SnapshotSample(&sample); + EXPECT_EQ(222, sample.sum() - original_sent_bytes); // 222 bytes sent. + + // SentCounts. + ASSERT_TRUE(StatisticsRecorder::FindHistogram( + "Net.SocketStream.SentCounts", &histogram)); + EXPECT_EQ(kUmaTargetedHistogramFlag, histogram->flags()); + histogram->SnapshotSample(&sample); + EXPECT_EQ(3, sample.sum() - original_sent_counts); // 3 write requests. +} + +} // namespace net |