Home | History | Annotate | Download | only in socket_stream
      1 // Copyright (c) 2011 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 "base/basictypes.h"
      6 #include "base/metrics/histogram.h"
      7 #include "googleurl/src/gurl.h"
      8 #include "net/socket_stream/socket_stream_metrics.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "testing/platform_test.h"
     11 
     12 using base::Histogram;
     13 using base::StatisticsRecorder;
     14 
     15 namespace net {
     16 
     17 TEST(SocketStreamMetricsTest, Initialize) {
     18   if (!StatisticsRecorder::IsActive()) {
     19     // Create the recorder if not yet started, as SocketStreamMetrics
     20     // relys on the StatisticsRecorder to be present. This is useful when
     21     // tests are run with --gtest_filter='SocketStreamMetricsTest*'.
     22     static StatisticsRecorder *recorder = NULL;
     23     recorder = new StatisticsRecorder;
     24   }
     25 }
     26 
     27 TEST(SocketStreamMetricsTest, ProtocolType) {
     28   Histogram* histogram;
     29 
     30   // First we'll preserve the original values. We need to do this
     31   // as histograms can get affected by other tests. In particular,
     32   // SocketStreamTest and WebSocketTest can affect the histograms.
     33   Histogram::SampleSet original;
     34   if (StatisticsRecorder::FindHistogram(
     35           "Net.SocketStream.ProtocolType", &histogram)) {
     36     histogram->SnapshotSample(&original);
     37   }
     38 
     39   SocketStreamMetrics unknown(GURL("unknown://www.example.com/"));
     40   SocketStreamMetrics ws1(GURL("ws://www.example.com/"));
     41   SocketStreamMetrics ws2(GURL("ws://www.example.com/"));
     42   SocketStreamMetrics wss1(GURL("wss://www.example.com/"));
     43   SocketStreamMetrics wss2(GURL("wss://www.example.com/"));
     44   SocketStreamMetrics wss3(GURL("wss://www.example.com/"));
     45 
     46   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
     47       "Net.SocketStream.ProtocolType", &histogram));
     48   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
     49 
     50   Histogram::SampleSet sample;
     51   histogram->SnapshotSample(&sample);
     52   original.Resize(*histogram);  // Ensure |original| size is same as |sample|.
     53   sample.Subtract(original); // Cancel the original values.
     54   EXPECT_EQ(1, sample.counts(SocketStreamMetrics::PROTOCOL_UNKNOWN));
     55   EXPECT_EQ(2, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET));
     56   EXPECT_EQ(3, sample.counts(SocketStreamMetrics::PROTOCOL_WEBSOCKET_SECURE));
     57 }
     58 
     59 TEST(SocketStreamMetricsTest, ConnectionType) {
     60   Histogram* histogram;
     61 
     62   // First we'll preserve the original values.
     63   Histogram::SampleSet original;
     64   if (StatisticsRecorder::FindHistogram(
     65           "Net.SocketStream.ConnectionType", &histogram)) {
     66     histogram->SnapshotSample(&original);
     67   }
     68 
     69   SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
     70   for (int i = 0; i < 1; ++i)
     71     metrics.OnStartConnection();
     72   for (int i = 0; i < 2; ++i)
     73     metrics.OnTunnelProxy();
     74   for (int i = 0; i < 3; ++i)
     75     metrics.OnSOCKSProxy();
     76   for (int i = 0; i < 4; ++i)
     77     metrics.OnSSLConnection();
     78 
     79   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
     80       "Net.SocketStream.ConnectionType", &histogram));
     81   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
     82 
     83   Histogram::SampleSet sample;
     84   histogram->SnapshotSample(&sample);
     85   original.Resize(*histogram);
     86   sample.Subtract(original);
     87   EXPECT_EQ(1, sample.counts(SocketStreamMetrics::ALL_CONNECTIONS));
     88   EXPECT_EQ(2, sample.counts(SocketStreamMetrics::TUNNEL_CONNECTION));
     89   EXPECT_EQ(3, sample.counts(SocketStreamMetrics::SOCKS_CONNECTION));
     90   EXPECT_EQ(4, sample.counts(SocketStreamMetrics::SSL_CONNECTION));
     91 }
     92 
     93 TEST(SocketStreamMetricsTest, OtherNumbers) {
     94   Histogram* histogram;
     95 
     96   // First we'll preserve the original values.
     97   int64 original_received_bytes = 0;
     98   int64 original_received_counts = 0;
     99   int64 original_sent_bytes = 0;
    100   int64 original_sent_counts = 0;
    101 
    102   Histogram::SampleSet original;
    103   if (StatisticsRecorder::FindHistogram(
    104           "Net.SocketStream.ReceivedBytes", &histogram)) {
    105     histogram->SnapshotSample(&original);
    106     original_received_bytes = original.sum();
    107   }
    108   if (StatisticsRecorder::FindHistogram(
    109           "Net.SocketStream.ReceivedCounts", &histogram)) {
    110     histogram->SnapshotSample(&original);
    111     original_received_counts = original.sum();
    112   }
    113   if (StatisticsRecorder::FindHistogram(
    114           "Net.SocketStream.SentBytes", &histogram)) {
    115     histogram->SnapshotSample(&original);
    116     original_sent_bytes = original.sum();
    117   }
    118   if (StatisticsRecorder::FindHistogram(
    119           "Net.SocketStream.SentCounts", &histogram)) {
    120     histogram->SnapshotSample(&original);
    121     original_sent_counts = original.sum();
    122   }
    123 
    124   SocketStreamMetrics metrics(GURL("ws://www.example.com/"));
    125   metrics.OnWaitConnection();
    126   metrics.OnStartConnection();
    127   metrics.OnConnected();
    128   metrics.OnRead(1);
    129   metrics.OnRead(10);
    130   metrics.OnWrite(2);
    131   metrics.OnWrite(20);
    132   metrics.OnWrite(200);
    133   metrics.OnClose();
    134 
    135   Histogram::SampleSet sample;
    136 
    137   // ConnectionLatency.
    138   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    139       "Net.SocketStream.ConnectionLatency", &histogram));
    140   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    141   // We don't check the contents of the histogram as it's time sensitive.
    142 
    143   // ConnectionEstablish.
    144   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    145       "Net.SocketStream.ConnectionEstablish", &histogram));
    146   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    147   // We don't check the contents of the histogram as it's time sensitive.
    148 
    149   // Duration.
    150   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    151       "Net.SocketStream.Duration", &histogram));
    152   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    153   // We don't check the contents of the histogram as it's time sensitive.
    154 
    155   // ReceivedBytes.
    156   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    157       "Net.SocketStream.ReceivedBytes", &histogram));
    158   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    159   histogram->SnapshotSample(&sample);
    160   EXPECT_EQ(11, sample.sum() - original_received_bytes);  // 11 bytes read.
    161 
    162   // ReceivedCounts.
    163   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    164       "Net.SocketStream.ReceivedCounts", &histogram));
    165   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    166   histogram->SnapshotSample(&sample);
    167   EXPECT_EQ(2, sample.sum() - original_received_counts);  // 2 read requests.
    168 
    169   // SentBytes.
    170   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    171       "Net.SocketStream.SentBytes", &histogram));
    172   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    173   histogram->SnapshotSample(&sample);
    174   EXPECT_EQ(222, sample.sum() - original_sent_bytes);  // 222 bytes sent.
    175 
    176   // SentCounts.
    177   ASSERT_TRUE(StatisticsRecorder::FindHistogram(
    178       "Net.SocketStream.SentCounts", &histogram));
    179   EXPECT_EQ(Histogram::kUmaTargetedHistogramFlag, histogram->flags());
    180   histogram->SnapshotSample(&sample);
    181   EXPECT_EQ(3, sample.sum() - original_sent_counts);  // 3 write requests.
    182 }
    183 
    184 }  // namespace net
    185