Commit cc7ce0ef authored by Greg Thompson's avatar Greg Thompson Committed by Commit Bot

Fix temp dir cleanup in components_perftests.

ScopedTempDirs must outlive anything that holds file(s) open within
them; otherwise, they are unable to delete their backing directories.

BUG=546640


Change-Id: I38da1876e033b9408b5957596311e5dd82b04083
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1891419
Auto-Submit: Greg Thompson <grt@chromium.org>
Commit-Queue: Tommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711287}
parent f7fa1dd0
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
...@@ -144,13 +145,12 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -144,13 +145,12 @@ class ProtoDBPerfTest : public testing::Test {
} }
void TearDown() override { void TearDown() override {
base::RunLoop().RunUntilIdle();
main_loop_.reset();
ShutdownDBs(); ShutdownDBs();
} }
void ShutdownDBs() { void ShutdownDBs() {
dbs_.clear(); dbs_.clear();
base::RunLoop().RunUntilIdle();
PruneBlockCache(); PruneBlockCache();
uint64_t mem; uint64_t mem;
GetApproximateMemoryUsage(&mem); GetApproximateMemoryUsage(&mem);
...@@ -167,12 +167,17 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -167,12 +167,17 @@ class ProtoDBPerfTest : public testing::Test {
return task_runner_; return task_runner_;
} }
void InitDB(const std::string& name, const ScopedTempDir& temp_dir) { // Initializes a DB named |name| in a dedicated directory. The same directory
InitDB(name, temp_dir.GetPath()); // will be used for all instances created for the same |name| for the lifetime
} // of the test.
void InitDB(const std::string& name) {
void InitDB(const std::string& name, const base::FilePath& path) { if (!base::Contains(temp_dirs_, name)) {
auto db = std::make_unique<TestDatabase>(task_runner_, path); auto temp_dir = std::make_unique<ScopedTempDir>();
EXPECT_TRUE(temp_dir->CreateUniqueTempDir());
temp_dirs_[name] = std::move(temp_dir);
}
auto db = std::make_unique<TestDatabase>(task_runner_,
temp_dirs_[name]->GetPath());
EXPECT_TRUE(db->is_initialized()); EXPECT_TRUE(db->is_initialized());
dbs_[name] = std::move(db); dbs_[name] = std::move(db);
} }
...@@ -254,8 +259,7 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -254,8 +259,7 @@ class ProtoDBPerfTest : public testing::Test {
KeyEntryVectorMap entries = KeyEntryVectorMap entries =
GenerateTestEntries(prefixes, params.num_entries, params.data_size); GenerateTestEntries(prefixes, params.num_entries, params.data_size);
std::vector<std::unique_ptr<ScopedTempDir>> temp_dirs; InitDBs(params.single_db, prefixes);
InitDBs(params.single_db, prefixes, &temp_dirs);
int remaining = params.num_entries; int remaining = params.num_entries;
PerfStats stats; PerfStats stats;
...@@ -298,8 +302,7 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -298,8 +302,7 @@ class ProtoDBPerfTest : public testing::Test {
prefixes.emplace_back(base::StringPrintf("test%03d_", i)); prefixes.emplace_back(base::StringPrintf("test%03d_", i));
} }
std::vector<std::unique_ptr<ScopedTempDir>> temp_dirs; InitDBs(single_db, prefixes);
InitDBs(single_db, prefixes, &temp_dirs);
PerfStats stats; PerfStats stats;
for (int i = 0; i < static_cast<int>(test_params.size()); i++) { for (int i = 0; i < static_cast<int>(test_params.size()); i++) {
...@@ -336,18 +339,14 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -336,18 +339,14 @@ class ProtoDBPerfTest : public testing::Test {
std::string test_modifier, std::string test_modifier,
unsigned int* num_entries_loaded, unsigned int* num_entries_loaded,
bool fill_read_cache = true) { bool fill_read_cache = true) {
ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
std::vector<std::string> prefixes = GenerateDBNames(num_dbs); std::vector<std::string> prefixes = GenerateDBNames(num_dbs);
PrefillDatabase(kSingleDBName, prefixes, num_entries, data_size, temp_dir); PrefillDatabase(kSingleDBName, prefixes, num_entries, data_size);
uint64_t memory_use_before; uint64_t memory_use_before;
GetApproximateMemoryUsage(&memory_use_before); GetApproximateMemoryUsage(&memory_use_before);
ShutdownDBs(); ShutdownDBs();
ASSERT_TRUE(temp_dir.IsValid()); InitDB(kSingleDBName);
InitDB(kSingleDBName, temp_dir.GetPath());
TestDatabase* db; TestDatabase* db;
GetDatabase(kSingleDBName, &db); GetDatabase(kSingleDBName, &db);
...@@ -394,12 +393,9 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -394,12 +393,9 @@ class ProtoDBPerfTest : public testing::Test {
unsigned int* num_entries_loaded, unsigned int* num_entries_loaded,
bool fill_read_cache = true) { bool fill_read_cache = true) {
std::vector<std::string> prefixes = GenerateDBNames(num_dbs); std::vector<std::string> prefixes = GenerateDBNames(num_dbs);
ScopedTempDir temp_dirs[num_dbs];
for (unsigned int i = 0; i < num_dbs; i++) { for (unsigned int i = 0; i < num_dbs; i++) {
ASSERT_TRUE(temp_dirs[i].CreateUniqueTempDir());
std::vector<std::string> single_prefix = {prefixes[i]}; std::vector<std::string> single_prefix = {prefixes[i]};
PrefillDatabase(prefixes[i], single_prefix, num_entries, data_size, PrefillDatabase(prefixes[i], single_prefix, num_entries, data_size);
temp_dirs[i]);
} }
uint64_t memory_use_before; uint64_t memory_use_before;
GetApproximateMemoryUsage(&memory_use_before); GetApproximateMemoryUsage(&memory_use_before);
...@@ -411,8 +407,7 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -411,8 +407,7 @@ class ProtoDBPerfTest : public testing::Test {
for (unsigned int i = 0; i < num_dbs; i++) { for (unsigned int i = 0; i < num_dbs; i++) {
if (dbs_to_load.size() > 0 && dbs_to_load.find(i) == dbs_to_load.end()) if (dbs_to_load.size() > 0 && dbs_to_load.find(i) == dbs_to_load.end())
continue; continue;
InitDB(prefixes[i], temp_dirs[i].GetPath()); InitDB(prefixes[i]);
ASSERT_TRUE(temp_dirs[i].IsValid());
TestDatabase* db; TestDatabase* db;
GetDatabase(prefixes[i], &db); GetDatabase(prefixes[i], &db);
...@@ -442,21 +437,12 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -442,21 +437,12 @@ class ProtoDBPerfTest : public testing::Test {
ShutdownDBs(); ShutdownDBs();
} }
void InitDBs(bool single_db, void InitDBs(bool single_db, const std::vector<std::string>& prefixes) {
const std::vector<std::string>& prefixes,
std::vector<std::unique_ptr<ScopedTempDir>>* temp_dirs) {
temp_dirs->clear();
if (single_db) { if (single_db) {
auto temp_dir = std::make_unique<ScopedTempDir>(); InitDB(kSingleDBName);
ASSERT_TRUE(temp_dir->CreateUniqueTempDir());
InitDB(kSingleDBName, *(temp_dir.get()));
temp_dirs->push_back(std::move(temp_dir));
} else { } else {
for (auto& prefix : prefixes) { for (auto& prefix : prefixes) {
auto temp_dir = std::make_unique<ScopedTempDir>(); InitDB(prefix);
ASSERT_TRUE(temp_dir->CreateUniqueTempDir());
InitDB(prefix, *(temp_dir.get()));
temp_dirs->push_back(std::move(temp_dir));
} }
} }
} }
...@@ -464,9 +450,8 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -464,9 +450,8 @@ class ProtoDBPerfTest : public testing::Test {
PerfStats PrefillDatabase(const std::string& name, PerfStats PrefillDatabase(const std::string& name,
std::vector<std::string>& prefixes, std::vector<std::string>& prefixes,
int num_entries, int num_entries,
int data_size, int data_size) {
const ScopedTempDir& temp_dir) { InitDB(name);
InitDB(name, temp_dir);
auto entries = GenerateTestEntries(prefixes, num_entries, data_size); auto entries = GenerateTestEntries(prefixes, num_entries, data_size);
PerfStats stats; PerfStats stats;
...@@ -574,6 +559,7 @@ class ProtoDBPerfTest : public testing::Test { ...@@ -574,6 +559,7 @@ class ProtoDBPerfTest : public testing::Test {
return reporter; return reporter;
} }
std::map<std::string, std::unique_ptr<ScopedTempDir>> temp_dirs_;
std::map<std::string, std::unique_ptr<TestDatabase>> dbs_; std::map<std::string, std::unique_ptr<TestDatabase>> dbs_;
std::unique_ptr<MessageLoop> main_loop_; std::unique_ptr<MessageLoop> main_loop_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_; scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
......
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