Commit 9020aee1 authored by twifkak's avatar twifkak Committed by Commit bot

Create a synthetic field trial for precache.

For clients in a Precache trial group who've started a precache in the last 15
days, registers them in a matching group in the PrecacheSynthetic15D trial. For
those who've started a precache in the last 1 day, registers in them in the
PrecacheSynthetic1D trial also.

BUG=663987

Review-Url: https://codereview.chromium.org/2596093002
Cr-Commit-Position: refs/heads/master@{#443352}
parent 7903b42a
......@@ -49,6 +49,10 @@ namespace options {
class BrowserOptionsHandler;
}
namespace precache {
void RegisterPrecacheSyntheticFieldTrial(base::Time);
}
namespace prerender {
bool IsOmniboxEnabled(Profile* profile);
}
......@@ -107,6 +111,7 @@ class ChromeMetricsServiceAccessor : public metrics::MetricsServiceAccessor {
bool,
const OnMetricsReportingCallbackType&);
friend class options::BrowserOptionsHandler;
friend void precache::RegisterPrecacheSyntheticFieldTrial(base::Time);
friend bool prerender::IsOmniboxEnabled(Profile* profile);
friend class settings::MetricsReportingHandler;
friend class speech::ChromeSpeechRecognitionManagerDelegate;
......
......@@ -4,8 +4,13 @@
#include "chrome/browser/precache/precache_util.h"
#include <string>
#include <vector>
#include "base/metrics/field_trial.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/precache/precache_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
......@@ -21,6 +26,9 @@ class HttpResponseInfo;
namespace {
const char kPrecacheSynthetic15D[] = "PrecacheSynthetic15D";
const char kPrecacheSynthetic1D[] = "PrecacheSynthetic1D";
void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url,
const GURL& referrer,
base::TimeDelta latency,
......@@ -42,7 +50,8 @@ void UpdatePrecacheMetricsAndStateOnUIThread(const GURL& url,
return;
precache_manager->UpdatePrecacheMetricsAndState(
url, referrer, latency, fetch_time, info, size, is_user_traffic);
url, referrer, latency, fetch_time, info, size, is_user_traffic,
base::Bind(&precache::RegisterPrecacheSyntheticFieldTrial));
}
} // namespace
......@@ -70,4 +79,25 @@ void UpdatePrecacheMetricsAndState(const net::URLRequest* request,
data_use_measurement::IsUserRequest(*request), profile_id));
}
// |last_precache_time| is the last time precache task was run.
void RegisterPrecacheSyntheticFieldTrial(base::Time last_precache_time) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::vector<uint32_t> groups;
base::TimeDelta time_ago = base::Time::Now() - last_precache_time;
// Look up the current group name (e.g. Control or Enabled).
std::string group_name =
base::FieldTrialList::FindFullName(kPrecacheFieldTrialName);
// group_name should only be empty if the Precache trial does not exist.
if (!group_name.empty()) {
// Register matching synthetic trials for 15-day and 1-day candidates.
if (time_ago <= base::TimeDelta::FromDays(15))
ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(
kPrecacheSynthetic15D, group_name);
if (time_ago <= base::TimeDelta::FromDays(1))
ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(
kPrecacheSynthetic1D, group_name);
}
}
} // namespace precache
......@@ -356,22 +356,35 @@ void PrecacheManager::UpdatePrecacheMetricsAndState(
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size,
bool is_user_traffic) {
bool is_user_traffic,
const base::Callback<void(base::Time)>& register_synthetic_trial) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RecordStatsForFetch(url, referrer, latency, fetch_time, info, size);
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::DB, FROM_HERE,
base::Bind(&PrecacheDatabase::GetLastPrecacheTimestamp,
base::Unretained(precache_database_.get())),
base::Bind(&PrecacheManager::RecordStatsForFetch, AsWeakPtr(), url,
referrer, latency, fetch_time, info, size,
register_synthetic_trial));
if (is_user_traffic && IsPrecaching())
CancelPrecaching();
}
void PrecacheManager::RecordStatsForFetch(const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size) {
void PrecacheManager::RecordStatsForFetch(
const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size,
const base::Callback<void(base::Time)>& register_synthetic_trial,
base::Time last_precache_time) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
register_synthetic_trial.Run(last_precache_time);
if (size == 0 || url.is_empty() || !url.SchemeIsHTTPOrHTTPS()) {
// Ignore empty responses, empty URLs, or URLs that aren't HTTP or HTTPS.
return;
......
......@@ -57,8 +57,9 @@ namespace precache {
class PrecacheDatabase;
class PrecacheUnfinishedWork;
// Visible for test.
extern const char kPrecacheFieldTrialName[];
// Visible for test.
extern const char kMinCacheSizeParam[];
size_t NumTopHosts();
......@@ -119,13 +120,15 @@ class PrecacheManager : public KeyedService,
// Update precache about an URL being fetched. Metrics related to precache are
// updated and any ongoing precache will be cancelled if this is an user
// initiated request. Should be called on UI thread.
void UpdatePrecacheMetricsAndState(const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size,
bool is_user_traffic);
void UpdatePrecacheMetricsAndState(
const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size,
bool is_user_traffic,
const base::Callback<void(base::Time)>& register_synthetic_trial);
private:
friend class PrecacheManagerTest;
......@@ -153,6 +156,11 @@ class PrecacheManager : public KeyedService,
// From PrecacheFetcher::PrecacheDelegate.
void OnDone() override;
// Registers the precache synthetic field trial for users whom the precache
// task was run recently. |last_precache_time| is the last time precache task
// was run.
void RegisterSyntheticFieldTrial(const base::Time last_precache_time);
// Callback when fetching unfinished work from storage is done.
void OnGetUnfinishedWorkDone(
std::unique_ptr<PrecacheUnfinishedWork> unfinished_work);
......@@ -179,12 +187,15 @@ class PrecacheManager : public KeyedService,
AllowedType PrecachingAllowed() const;
// Update precache-related metrics in response to a URL being fetched.
void RecordStatsForFetch(const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size);
void RecordStatsForFetch(
const GURL& url,
const GURL& referrer,
const base::TimeDelta& latency,
const base::Time& fetch_time,
const net::HttpResponseInfo& info,
int64_t size,
const base::Callback<void(base::Time)>& register_synthetic_trial,
base::Time last_precache_time);
// Update precache-related metrics in response to a URL being fetched. Called
// by RecordStatsForFetch() by way of an asynchronous HistoryService callback.
......
......@@ -67,11 +67,13 @@ class PrecacheDatabase {
base::Time GetLastPrecacheTimestamp();
// Report precache-related metrics in response to a URL being fetched, where
// the fetch was motivated by precaching.
// the fetch was motivated by precaching. This is called from the network
// delegate, via precache_util.
void RecordURLPrefetchMetrics(const net::HttpResponseInfo& info,
const base::TimeDelta& latency);
// Records the precache of an url |url| for top host |referrer_host|.
// Records the precache of an url |url| for top host |referrer_host|. This is
// called from PrecacheFetcher.
void RecordURLPrefetch(const GURL& url,
const std::string& referrer_host,
const base::Time& fetch_time,
......@@ -81,6 +83,7 @@ class PrecacheDatabase {
// Report precache-related metrics in response to a URL being fetched, where
// the fetch was not motivated by precaching. |is_connection_cellular|
// indicates whether the current network connection is a cellular network.
// This is called from the network delegate, via precache_util.
void RecordURLNonPrefetch(const GURL& url,
const base::TimeDelta& latency,
const base::Time& fetch_time,
......@@ -124,6 +127,7 @@ class PrecacheDatabase {
private:
friend class PrecacheDatabaseTest;
friend class PrecacheFetcherTest;
friend class PrecacheManagerTest;
bool IsDatabaseAccessible() const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment