| Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_experiments_stats_unittest.cc
|
| diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_experiments_stats_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_experiments_stats_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..255a6d6a5c4311b3535e6dbe667de215b8cdfc5a
|
| --- /dev/null
|
| +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_experiments_stats_unittest.cc
|
| @@ -0,0 +1,225 @@
|
| +// Copyright 2015 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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_experiments_stats.h"
|
| +
|
| +#include <map>
|
| +
|
| +#include "base/metrics/field_trial.h"
|
| +#include "base/prefs/pref_service.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "base/test/histogram_tester.h"
|
| +#include "base/time/time.h"
|
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
|
| +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
|
| +#include "components/variations/variations_associated_data.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +// Test value for how long a Data Reduction Proxy configuration should be valid.
|
| +const int64 kConfigExpirationSeconds = 60 * 60 * 24;
|
| +
|
| +} // namespace
|
| +
|
| +namespace data_reduction_proxy {
|
| +
|
| +class DataReductionProxyExperimentsStatsTest : public testing::Test {
|
| + public:
|
| + void SetPrefValue(const std::string& path, int64 value) {}
|
| +
|
| + protected:
|
| + void SetUp() override {
|
| + test_context_ = DataReductionProxyTestContext::Builder().Build();
|
| + }
|
| +
|
| + void SetConfigExperimentValues(
|
| + bool enabled,
|
| + const base::TimeDelta& expiration_offset_from_now,
|
| + int64 roundtrip_milliseconds_base,
|
| + double roundtrip_multiplier,
|
| + int64 roundtrip_milliseconds_increment,
|
| + int64 expiration_seconds,
|
| + bool always_stale) {
|
| + variations::testing::ClearAllVariationParams();
|
| + std::map<std::string, std::string> variation_params;
|
| + variation_params[experiments::kConfigRoundtripMillisecondsBaseParam] =
|
| + base::Int64ToString(roundtrip_milliseconds_base);
|
| + variation_params[experiments::kConfigRoundtripMultiplierParam] =
|
| + base::DoubleToString(roundtrip_multiplier);
|
| + variation_params[experiments::kConfigRoundtripMillisecondsIncrementParam] =
|
| + base::Int64ToString(roundtrip_milliseconds_increment);
|
| + variation_params[experiments::kConfigExpirationSecondsParam] =
|
| + base::Int64ToString(expiration_seconds);
|
| + variation_params[experiments::kConfigAlwaysStaleParam] =
|
| + always_stale ? "1" : "0:";
|
| + if (enabled) {
|
| + ASSERT_TRUE(variations::AssociateVariationParams(
|
| + experiments::kConfigFetchTrialGroup, "Enabled", variation_params));
|
| + }
|
| + pref_service()->SetInt64(
|
| + prefs::kSimulatedConfigExpireTime,
|
| + (base::Time::Now() + expiration_offset_from_now).ToInternalValue());
|
| + }
|
| +
|
| + PrefService* pref_service() { return test_context_->pref_service(); }
|
| +
|
| + private:
|
| + base::MessageLoopForIO message_loop_;
|
| + scoped_ptr<DataReductionProxyTestContext> test_context_;
|
| +};
|
| +
|
| +TEST_F(DataReductionProxyExperimentsStatsTest, CreateConfigParams) {
|
| + struct {
|
| + bool experiment_enabled;
|
| + base::TimeDelta expiration_offset_from_now;
|
| + int64 expiration_seconds;
|
| + bool always_stale;
|
| + } test_cases[] = {
|
| + {
|
| + false, base::TimeDelta::FromHours(2), kConfigExpirationSeconds, false,
|
| + },
|
| + {
|
| + true, base::TimeDelta::FromHours(2), kConfigExpirationSeconds, false,
|
| + },
|
| + {
|
| + true, base::TimeDelta::FromHours(2), kConfigExpirationSeconds, true,
|
| + },
|
| + {
|
| + true, base::TimeDelta::FromDays(-1), kConfigExpirationSeconds, false,
|
| + },
|
| + };
|
| + for (const auto& test_case : test_cases) {
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + base::FieldTrialList::CreateFieldTrial(
|
| + experiments::kConfigFetchTrialGroup,
|
| + test_case.experiment_enabled ? "Enabled" : "Disabled");
|
| + base::Time min_now = base::Time::Now();
|
| + SetConfigExperimentValues(
|
| + test_case.experiment_enabled, test_case.expiration_offset_from_now, 100,
|
| + 2.0, 100, test_case.expiration_seconds, test_case.always_stale);
|
| + scoped_ptr<DataReductionProxyExperimentsStats::ConfigRetrievalParams>
|
| + config_params =
|
| + DataReductionProxyExperimentsStats::ConfigRetrievalParams::Create(
|
| + pref_service());
|
| + base::Time max_now = base::Time::Now();
|
| + if (test_case.experiment_enabled) {
|
| + if (!test_case.always_stale &&
|
| + test_case.expiration_offset_from_now.InMilliseconds() > 0) {
|
| + EXPECT_GE(config_params->config_expiration(),
|
| + min_now + test_case.expiration_offset_from_now);
|
| + EXPECT_LE(config_params->config_expiration(),
|
| + max_now + test_case.expiration_offset_from_now);
|
| + } else {
|
| + EXPECT_GE(config_params->config_expiration(),
|
| + min_now + base::TimeDelta::FromSeconds(
|
| + test_case.expiration_seconds));
|
| + EXPECT_LE(config_params->config_expiration(),
|
| + max_now + base::TimeDelta::FromSeconds(
|
| + test_case.expiration_seconds));
|
| + }
|
| + EXPECT_EQ(config_params->refresh_interval(),
|
| + base::TimeDelta::FromSeconds(test_case.expiration_seconds) -
|
| + base::TimeDelta::FromSeconds(
|
| + experiments::kConfigFetchBufferSeconds));
|
| + } else {
|
| + EXPECT_EQ(nullptr, config_params.get());
|
| + }
|
| + }
|
| +}
|
| +
|
| +TEST_F(DataReductionProxyExperimentsStatsTest, CheckHistogramsNoExperiments) {
|
| + scoped_ptr<DataReductionProxyExperimentsStats> experiments_stats(
|
| + new DataReductionProxyExperimentsStats(
|
| + base::Bind(&DataReductionProxyExperimentsStatsTest::SetPrefValue,
|
| + base::Unretained(this))));
|
| + experiments_stats->InitializeOnUIThread(
|
| + scoped_ptr<DataReductionProxyExperimentsStats::ConfigRetrievalParams>());
|
| + base::HistogramTester histogram_tester;
|
| + experiments_stats->RecordBytes(base::Time::Now(), VIA_DATA_REDUCTION_PROXY,
|
| + 500, 1000);
|
| + histogram_tester.ExpectTotalCount(
|
| + "DataReductionProxy.ConfigFetchLostBytesOCL_0", 0);
|
| + histogram_tester.ExpectTotalCount(
|
| + "DataReductionProxy.ConfigFetchLostBytesCL_0", 0);
|
| + histogram_tester.ExpectTotalCount(
|
| + "DataReductionProxy.ConfigFetchLostBytesDiff_0", 0);
|
| +}
|
| +
|
| +TEST_F(DataReductionProxyExperimentsStatsTest, CheckConfigHistograms) {
|
| + struct {
|
| + DataReductionProxyRequestType request_type;
|
| + base::Time request_time;
|
| + int64 received_content_length;
|
| + int64 original_content_length;
|
| + int64 expected_received_content_length;
|
| + int64 expected_original_content_length;
|
| + int64 expected_diff;
|
| + } test_cases[] = {
|
| + {
|
| + VIA_DATA_REDUCTION_PROXY,
|
| + base::Time::Now() + base::TimeDelta::FromMinutes(5),
|
| + 500,
|
| + 1000,
|
| + -1,
|
| + -1,
|
| + -1,
|
| + },
|
| + {
|
| + VIA_DATA_REDUCTION_PROXY, base::Time::Now(), 500, 1000, 500, 1000, 500,
|
| + },
|
| + {
|
| + HTTPS, base::Time::Now(), 500, 1000, -1, -1, -1,
|
| + },
|
| + };
|
| +
|
| + base::FieldTrialList field_trial_list(nullptr);
|
| + base::FieldTrialList::CreateFieldTrial(experiments::kConfigFetchTrialGroup,
|
| + "Enabled");
|
| + SetConfigExperimentValues(true, base::TimeDelta(), 100, 2.0, 1000,
|
| + kConfigExpirationSeconds, true);
|
| + scoped_ptr<DataReductionProxyExperimentsStats::ConfigRetrievalParams>
|
| + config_params =
|
| + DataReductionProxyExperimentsStats::ConfigRetrievalParams::Create(
|
| + pref_service());
|
| + scoped_ptr<DataReductionProxyExperimentsStats> experiments_stats(
|
| + new DataReductionProxyExperimentsStats(
|
| + base::Bind(&DataReductionProxyExperimentsStatsTest::SetPrefValue,
|
| + base::Unretained(this))));
|
| + experiments_stats->InitializeOnUIThread(config_params.Pass());
|
| + for (const auto& test_case : test_cases) {
|
| + base::HistogramTester histogram_tester;
|
| + experiments_stats->RecordBytes(
|
| + test_case.request_time, test_case.request_type,
|
| + test_case.received_content_length, test_case.original_content_length);
|
| + if (test_case.expected_received_content_length == -1) {
|
| + histogram_tester.ExpectTotalCount(
|
| + "DataReductionProxy.ConfigFetchLostBytesCL_0", 0);
|
| + } else {
|
| + histogram_tester.ExpectUniqueSample(
|
| + "DataReductionProxy.ConfigFetchLostBytesCL_0",
|
| + test_case.expected_received_content_length, 1);
|
| + }
|
| +
|
| + if (test_case.expected_original_content_length == -1) {
|
| + histogram_tester.ExpectTotalCount(
|
| + "DataReductionProxy.ConfigFetchLostBytesOCL_0", 0);
|
| + } else {
|
| + histogram_tester.ExpectUniqueSample(
|
| + "DataReductionProxy.ConfigFetchLostBytesOCL_0",
|
| + test_case.expected_original_content_length, 1);
|
| + }
|
| +
|
| + if (test_case.expected_diff == -1) {
|
| + histogram_tester.ExpectTotalCount(
|
| + "DataReductionProxy.ConfigFetchLostBytesDiff_0", 0);
|
| + } else {
|
| + histogram_tester.ExpectUniqueSample(
|
| + "DataReductionProxy.ConfigFetchLostBytesDiff_0",
|
| + test_case.expected_diff, 1);
|
| + }
|
| + }
|
| +}
|
| +
|
| +} // namespace data_reduction_proxy
|
|
|