Chromium Code Reviews| Index: chrome/app/client_util.cc |
| diff --git a/chrome/app/client_util.cc b/chrome/app/client_util.cc |
| index 0d1c52cb3a433cdd80f85c38e5d36d5856891d6c..392c7616a3e90a439f4e8e0249b6de776d81df05 100644 |
| --- a/chrome/app/client_util.cc |
| +++ b/chrome/app/client_util.cc |
| @@ -11,6 +11,8 @@ |
| #include "base/file_version_info.h" |
| #include "base/logging.h" |
| #include "base/memory/scoped_ptr.h" |
| +#include "base/rand_util.h" // For PreRead experiment. |
| +#include "base/sha1.h" // For PreRead experiment. |
| #include "base/strings/string16.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| @@ -37,6 +39,116 @@ typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); |
| typedef void (*RelaunchChromeBrowserWithNewCommandLineIfNeededFunc)(); |
| +// Returns true if the build date for this module precedes the expiry date |
| +// for the pre-read experiment. |
| +bool PreReadExperimentIsActive() { |
| + static const int kPreReadExpiryYear = 2014; |
| + static const int kPreReadExpiryMonth = 7; |
| + static const int kPreReadExpiryDay = 1; |
| + static const char kBuildTimeStr[] = __DATE__ " " __TIME__; |
|
Alexei Svitkine (slow)
2013/09/03 14:59:16
Nit: static keyword isn't needed for any of these
Roger McFarlane (Chromium)
2013/09/03 18:38:43
Done.
|
| + |
| + // Get the timestamp of the build. |
| + base::Time build_time; |
| + bool result = base::Time::FromString(kBuildTimeStr, &build_time); |
| + DCHECK(result); |
| + |
| + // Get the timestamp at which the experiment expires. |
| + base::Time::Exploded exploded = {0}; |
| + exploded.year = kPreReadExpiryYear; |
| + exploded.month = kPreReadExpiryMonth; |
| + exploded.day_of_month = kPreReadExpiryDay; |
| + base::Time expiration_time = base::Time::FromLocalExploded(exploded); |
| + |
| + // Return true if the build time predates the expiration time.. |
| + return build_time < expiration_time; |
| +} |
| + |
| +// Get random unit values, i.e., in the range (0, 1), denoting a die-toss for |
| +// being in an experiment population and experimental group thereof. |
| +void GetPreReadPopulationAndGroup(double* population, double* group) { |
| + DCHECK(population != NULL); |
| + DCHECK(group != NULL); |
| + |
| + // By default we use the metrics id for the user as stable pseudo-random |
| + // input to a hash. |
| + base::string16 metrics_id; |
| + GoogleUpdateSettings::GetMetricsId(&metrics_id); |
| + |
| + // If this user has not metrics id, we fall back to a purely random value |
| + // per browser session. |
| + const size_t kLength = 16; |
| + std::string random_value(metrics_id.empty() ? base::RandBytesAsString(kLength) |
| + : base::WideToUTF8(metrics_id)); |
| + |
| + // To interpret the value as a random number we hash it and read the first 8 |
| + // bytes of the hash as a unit-interval representing a die-toss for being in |
| + // the experiment population and the second 8 bytes as a die-toss for being |
| + // in various experiment groups. |
| + unsigned char sha1_hash[base::kSHA1Length]; |
| + base::SHA1HashBytes( |
| + reinterpret_cast<const unsigned char*>(random_value.c_str()), |
| + random_value.size() * sizeof(random_value[0]), |
| + sha1_hash); |
| + COMPILE_ASSERT(2 * sizeof(uint64) < sizeof(sha1_hash), need_more_data); |
| + const uint64* random_bits = reinterpret_cast<uint64*>(&sha1_hash[0]); |
| + |
| + // Convert the bits into unit-intervals and return. |
| + *population = base::BitsToOpenEndedUnitInterval(random_bits[0]); |
| + *group = base::BitsToOpenEndedUnitInterval(random_bits[1]); |
| +} |
| + |
| +// Gets the amount of pre-read to use as well as the experiment group in which |
| +// the user falls. |
| +size_t InitPreReadPercentage() { |
| + // By default use the old behaviour: read 100%. |
| + static const uint8 kDefaultPercentage = 100; |
| + static const char kDefaultFormatStr[] = "%d%%-Default"; |
| + static const char kControlFormatStr[] = "%d%%-Control"; |
| + static const char kGroupFormatStr[] = "%d%%"; |
| + |
| + COMPILE_ASSERT(kDefaultPercentage <= 100, default_percentage_too_large); |
| + COMPILE_ASSERT(kDefaultPercentage % 5 == 0, default_percentage_not_mult_5); |
| + |
| + // Roll the dice to determine if this user is in the experiment and if so, |
| + // in which experimental group. |
| + double population = 0.0; |
| + double group = 0.0; |
| + GetPreReadPopulationAndGroup(&population, &group); |
| + |
| + // We limit experiment populations to 1% of the Stable and 10% of each of |
| + // the other channels. |
| + const string16 channel(GoogleUpdateSettings::GetChromeChannel( |
| + GoogleUpdateSettings::IsSystemInstall())); |
| + double threshold = (channel == installer::kChromeChannelStable) ? 0.01 : 0.10; |
| + |
| + // If the experiment has expired use the default pre-read level. Otherwise, |
| + // those not in the experiment population also use the default pre-read level. |
| + size_t value = kDefaultPercentage; |
| + const char* format_str = kDefaultFormatStr; |
| + if (PreReadExperimentIsActive() && (population <= threshold)) { |
| + // We divide the experiment population into groups pre-reading at 5 percent |
| + // increments. This is 21 groups of 5 -- to include the range [100, 105) -- |
| + // rounded down to the nearest 5. |
| + value = static_cast<size_t>(group * 21.0) * 5; |
| + DCHECK_LE(value, 100u); |
| + DCHECK_EQ(0u, value % 5); |
| + format_str = |
| + (value == kDefaultPercentage) ? kControlFormatStr : kGroupFormatStr; |
| + } |
| + |
| + // Generate the group name corresponding to this percentage value. |
| + std::string group_name; |
| + base::SStringPrintf(&group_name, format_str, value); |
| + |
| + // Persiste the group name to the environment so that it can be used for |
| + // reporting. |
| + scoped_ptr<base::Environment> env(base::Environment::Create()); |
| + env->SetVar(chrome::kPreReadEnvironmentVariable, group_name); |
| + |
| + // Return the percentage value to be used. |
| + return value; |
| +} |
| + |
| // Expects that |dir| has a trailing backslash. |dir| is modified so it |
| // contains the full path that was tried. Caller must check for the return |
| // value not being null to determine if this path contains a valid dll. |
| @@ -58,8 +170,8 @@ HMODULE LoadChromeWithDirectory(string16* dir) { |
| // performance loss. |
| if (!cmd_line.HasSwitch(switches::kProcessType)) { |
| const size_t kStepSize = 1024 * 1024; |
| - uint8 percent = base::win::GetVersion() > base::win::VERSION_XP ? 25 : 0; |
| - ImagePreReader::PartialPreReadImage(dir->c_str(), percent, kStepSize); |
| + size_t percentage = InitPreReadPercentage(); |
| + ImagePreReader::PartialPreReadImage(dir->c_str(), percentage, kStepSize); |
| } |
| #endif |