This source file includes following definitions.
- TestURL
- NewTable
- Add
- Reset
- CheckVisited
- FillTable
- SetUp
- TearDown
- TEST_F
- TEST_F
#include <algorithm>
#include <string>
#include <vector>
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/memory/shared_memory.h"
#include "base/strings/stringprintf.h"
#include "base/test/perf_log.h"
#include "base/test/perf_time_logger.h"
#include "base/test/test_file_util.h"
#include "base/timer/elapsed_timer.h"
#include "components/visitedlink/browser/visitedlink_master.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using base::TimeDelta;
namespace visitedlink {
namespace {
const int add_count = 10000;
const int load_test_add_count = 250000;
const char added_prefix[] = "http://www.google.com/stuff/something/foo?session=85025602345625&id=1345142319023&seq=";
const char unadded_prefix[] = "http://www.google.org/stuff/something/foo?session=39586739476365&id=2347624314402&seq=";
GURL TestURL(const char* prefix, int i) {
return GURL(base::StringPrintf("%s%d", prefix, i));
}
class DummyVisitedLinkEventListener : public VisitedLinkMaster::Listener {
public:
DummyVisitedLinkEventListener() {}
virtual void NewTable(base::SharedMemory* table) OVERRIDE {}
virtual void Add(VisitedLinkCommon::Fingerprint) OVERRIDE {}
virtual void Reset() OVERRIDE {}
};
void CheckVisited(VisitedLinkMaster& master, const char* prefix,
int begin, int end) {
for (int i = begin; i < end; i++)
master.IsVisited(TestURL(prefix, i));
}
void FillTable(VisitedLinkMaster& master, const char* prefix,
int begin, int end) {
for (int i = begin; i < end; i++)
master.AddURL(TestURL(prefix, i));
}
class VisitedLink : public testing::Test {
protected:
base::FilePath db_path_;
virtual void SetUp() {
ASSERT_TRUE(base::CreateTemporaryFile(&db_path_));
}
virtual void TearDown() {
base::DeleteFile(db_path_, false);
}
};
}
TEST_F(VisitedLink, TestAddAndQuery) {
VisitedLinkMaster master(new DummyVisitedLinkEventListener(),
NULL, true, true, db_path_, 0);
ASSERT_TRUE(master.Init());
base::PerfTimeLogger timer("Visited_link_add_and_query");
CheckVisited(master, added_prefix, 0, add_count);
const int half_size = add_count / 2;
FillTable(master, added_prefix, 0, half_size);
CheckVisited(master, added_prefix, 0, add_count);
FillTable(master, added_prefix, half_size, add_count);
CheckVisited(master, added_prefix, 0, add_count);
CheckVisited(master, unadded_prefix, 0, add_count);
}
TEST_F(VisitedLink, TestLoad) {
{
base::PerfTimeLogger table_initialization_timer("Table_initialization");
VisitedLinkMaster master(new DummyVisitedLinkEventListener(),
NULL, true, true, db_path_, 0);
base::PerfTimeLogger initTimer("Empty_visited_link_init");
bool success = master.Init();
initTimer.Done();
ASSERT_TRUE(success);
FillTable(master, added_prefix, 0, load_test_add_count);
base::PerfTimeLogger flushTimer("Visited_link_database_flush");
master.RewriteFile();
flushTimer.Done();
table_initialization_timer.Done();
}
const int load_count = 5;
std::vector<double> cold_load_times;
std::vector<double> hot_load_times;
for (int i = 0; i < load_count; i++) {
base::EvictFileFromSystemCache(db_path_);
{
base::ElapsedTimer cold_timer;
VisitedLinkMaster master(new DummyVisitedLinkEventListener(),
NULL,
true,
true,
db_path_,
0);
bool success = master.Init();
TimeDelta elapsed = cold_timer.Elapsed();
ASSERT_TRUE(success);
cold_load_times.push_back(elapsed.InMillisecondsF());
}
{
base::ElapsedTimer hot_timer;
VisitedLinkMaster master(new DummyVisitedLinkEventListener(),
NULL,
true,
true,
db_path_,
0);
bool success = master.Init();
TimeDelta elapsed = hot_timer.Elapsed();
ASSERT_TRUE(success);
hot_load_times.push_back(elapsed.InMillisecondsF());
}
}
cold_load_times.erase(std::max_element(cold_load_times.begin(),
cold_load_times.end()));
hot_load_times.erase(std::max_element(hot_load_times.begin(),
hot_load_times.end()));
double cold_sum = 0, hot_sum = 0;
for (int i = 0; i < static_cast<int>(cold_load_times.size()); i++) {
cold_sum += cold_load_times[i];
hot_sum += hot_load_times[i];
}
base::LogPerfResult(
"Visited_link_cold_load_time", cold_sum / cold_load_times.size(), "ms");
base::LogPerfResult(
"Visited_link_hot_load_time", hot_sum / hot_load_times.size(), "ms");
}
}