Changeset View
Changeset View
Standalone View
Standalone View
src/leveldb/db/db_bench.cc
Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | |||||
// Print histogram of operation timings | // Print histogram of operation timings | ||||
static bool FLAGS_histogram = false; | static bool FLAGS_histogram = false; | ||||
// Number of bytes to buffer in memtable before compacting | // Number of bytes to buffer in memtable before compacting | ||||
// (initialized to default value by "main") | // (initialized to default value by "main") | ||||
static int FLAGS_write_buffer_size = 0; | static int FLAGS_write_buffer_size = 0; | ||||
// Number of bytes written to each file. | |||||
// (initialized to default value by "main") | |||||
static int FLAGS_max_file_size = 0; | |||||
// Approximate size of user data packed per block (before compression. | |||||
// (initialized to default value by "main") | |||||
static int FLAGS_block_size = 0; | |||||
// Number of bytes to use as a cache of uncompressed data. | // Number of bytes to use as a cache of uncompressed data. | ||||
// Negative means use default settings. | // Negative means use default settings. | ||||
static int FLAGS_cache_size = -1; | static int FLAGS_cache_size = -1; | ||||
// Maximum number of files to keep open at the same time (use default if == 0) | // Maximum number of files to keep open at the same time (use default if == 0) | ||||
static int FLAGS_open_files = 0; | static int FLAGS_open_files = 0; | ||||
// Bloom filter bits per key. | // Bloom filter bits per key. | ||||
Show All 9 Lines | |||||
static bool FLAGS_reuse_logs = false; | static bool FLAGS_reuse_logs = false; | ||||
// Use the db with the following name. | // Use the db with the following name. | ||||
static const char* FLAGS_db = NULL; | static const char* FLAGS_db = NULL; | ||||
namespace leveldb { | namespace leveldb { | ||||
namespace { | namespace { | ||||
leveldb::Env* g_env = NULL; | |||||
// Helper for quickly generating random data. | // Helper for quickly generating random data. | ||||
class RandomGenerator { | class RandomGenerator { | ||||
private: | private: | ||||
std::string data_; | std::string data_; | ||||
int pos_; | int pos_; | ||||
public: | public: | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | public: | ||||
void Start() { | void Start() { | ||||
next_report_ = 100; | next_report_ = 100; | ||||
last_op_finish_ = start_; | last_op_finish_ = start_; | ||||
hist_.Clear(); | hist_.Clear(); | ||||
done_ = 0; | done_ = 0; | ||||
bytes_ = 0; | bytes_ = 0; | ||||
seconds_ = 0; | seconds_ = 0; | ||||
start_ = Env::Default()->NowMicros(); | start_ = g_env->NowMicros(); | ||||
finish_ = start_; | finish_ = start_; | ||||
message_.clear(); | message_.clear(); | ||||
} | } | ||||
void Merge(const Stats& other) { | void Merge(const Stats& other) { | ||||
hist_.Merge(other.hist_); | hist_.Merge(other.hist_); | ||||
done_ += other.done_; | done_ += other.done_; | ||||
bytes_ += other.bytes_; | bytes_ += other.bytes_; | ||||
seconds_ += other.seconds_; | seconds_ += other.seconds_; | ||||
if (other.start_ < start_) start_ = other.start_; | if (other.start_ < start_) start_ = other.start_; | ||||
if (other.finish_ > finish_) finish_ = other.finish_; | if (other.finish_ > finish_) finish_ = other.finish_; | ||||
// Just keep the messages from one thread | // Just keep the messages from one thread | ||||
if (message_.empty()) message_ = other.message_; | if (message_.empty()) message_ = other.message_; | ||||
} | } | ||||
void Stop() { | void Stop() { | ||||
finish_ = Env::Default()->NowMicros(); | finish_ = g_env->NowMicros(); | ||||
seconds_ = (finish_ - start_) * 1e-6; | seconds_ = (finish_ - start_) * 1e-6; | ||||
} | } | ||||
void AddMessage(Slice msg) { | void AddMessage(Slice msg) { | ||||
AppendWithSpace(&message_, msg); | AppendWithSpace(&message_, msg); | ||||
} | } | ||||
void FinishedSingleOp() { | void FinishedSingleOp() { | ||||
if (FLAGS_histogram) { | if (FLAGS_histogram) { | ||||
double now = Env::Default()->NowMicros(); | double now = g_env->NowMicros(); | ||||
double micros = now - last_op_finish_; | double micros = now - last_op_finish_; | ||||
hist_.Add(micros); | hist_.Add(micros); | ||||
if (micros > 20000) { | if (micros > 20000) { | ||||
fprintf(stderr, "long op: %.1f micros%30s\r", micros, ""); | fprintf(stderr, "long op: %.1f micros%30s\r", micros, ""); | ||||
fflush(stderr); | fflush(stderr); | ||||
} | } | ||||
last_op_finish_ = now; | last_op_finish_ = now; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 173 Lines • ▼ Show 20 Lines | filter_policy_(FLAGS_bloom_bits >= 0 | ||||
: NULL), | : NULL), | ||||
db_(NULL), | db_(NULL), | ||||
num_(FLAGS_num), | num_(FLAGS_num), | ||||
value_size_(FLAGS_value_size), | value_size_(FLAGS_value_size), | ||||
entries_per_batch_(1), | entries_per_batch_(1), | ||||
reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads), | reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads), | ||||
heap_counter_(0) { | heap_counter_(0) { | ||||
std::vector<std::string> files; | std::vector<std::string> files; | ||||
Env::Default()->GetChildren(FLAGS_db, &files); | g_env->GetChildren(FLAGS_db, &files); | ||||
for (size_t i = 0; i < files.size(); i++) { | for (size_t i = 0; i < files.size(); i++) { | ||||
if (Slice(files[i]).starts_with("heap-")) { | if (Slice(files[i]).starts_with("heap-")) { | ||||
Env::Default()->DeleteFile(std::string(FLAGS_db) + "/" + files[i]); | g_env->DeleteFile(std::string(FLAGS_db) + "/" + files[i]); | ||||
} | } | ||||
} | } | ||||
if (!FLAGS_use_existing_db) { | if (!FLAGS_use_existing_db) { | ||||
DestroyDB(FLAGS_db, Options()); | DestroyDB(FLAGS_db, Options()); | ||||
} | } | ||||
} | } | ||||
~Benchmark() { | ~Benchmark() { | ||||
▲ Show 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | void RunBenchmark(int n, Slice name, | ||||
ThreadArg* arg = new ThreadArg[n]; | ThreadArg* arg = new ThreadArg[n]; | ||||
for (int i = 0; i < n; i++) { | for (int i = 0; i < n; i++) { | ||||
arg[i].bm = this; | arg[i].bm = this; | ||||
arg[i].method = method; | arg[i].method = method; | ||||
arg[i].shared = &shared; | arg[i].shared = &shared; | ||||
arg[i].thread = new ThreadState(i); | arg[i].thread = new ThreadState(i); | ||||
arg[i].thread->shared = &shared; | arg[i].thread->shared = &shared; | ||||
Env::Default()->StartThread(ThreadBody, &arg[i]); | g_env->StartThread(ThreadBody, &arg[i]); | ||||
} | } | ||||
shared.mu.Lock(); | shared.mu.Lock(); | ||||
while (shared.num_initialized < n) { | while (shared.num_initialized < n) { | ||||
shared.cv.Wait(); | shared.cv.Wait(); | ||||
} | } | ||||
shared.start = true; | shared.start = true; | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | void SnappyUncompress(ThreadState* thread) { | ||||
} else { | } else { | ||||
thread->stats.AddBytes(bytes); | thread->stats.AddBytes(bytes); | ||||
} | } | ||||
} | } | ||||
void Open() { | void Open() { | ||||
assert(db_ == NULL); | assert(db_ == NULL); | ||||
Options options; | Options options; | ||||
options.env = g_env; | |||||
options.create_if_missing = !FLAGS_use_existing_db; | options.create_if_missing = !FLAGS_use_existing_db; | ||||
options.block_cache = cache_; | options.block_cache = cache_; | ||||
options.write_buffer_size = FLAGS_write_buffer_size; | options.write_buffer_size = FLAGS_write_buffer_size; | ||||
options.max_file_size = FLAGS_max_file_size; | |||||
options.block_size = FLAGS_block_size; | |||||
options.max_open_files = FLAGS_open_files; | options.max_open_files = FLAGS_open_files; | ||||
options.filter_policy = filter_policy_; | options.filter_policy = filter_policy_; | ||||
options.reuse_logs = FLAGS_reuse_logs; | options.reuse_logs = FLAGS_reuse_logs; | ||||
Status s = DB::Open(options, FLAGS_db, &db_); | Status s = DB::Open(options, FLAGS_db, &db_); | ||||
if (!s.ok()) { | if (!s.ok()) { | ||||
fprintf(stderr, "open error: %s\n", s.ToString().c_str()); | fprintf(stderr, "open error: %s\n", s.ToString().c_str()); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 206 Lines • ▼ Show 20 Lines | private: | ||||
static void WriteToFile(void* arg, const char* buf, int n) { | static void WriteToFile(void* arg, const char* buf, int n) { | ||||
reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n)); | reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n)); | ||||
} | } | ||||
void HeapProfile() { | void HeapProfile() { | ||||
char fname[100]; | char fname[100]; | ||||
snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db, ++heap_counter_); | snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db, ++heap_counter_); | ||||
WritableFile* file; | WritableFile* file; | ||||
Status s = Env::Default()->NewWritableFile(fname, &file); | Status s = g_env->NewWritableFile(fname, &file); | ||||
if (!s.ok()) { | if (!s.ok()) { | ||||
fprintf(stderr, "%s\n", s.ToString().c_str()); | fprintf(stderr, "%s\n", s.ToString().c_str()); | ||||
return; | return; | ||||
} | } | ||||
bool ok = port::GetHeapProfile(WriteToFile, file); | bool ok = port::GetHeapProfile(WriteToFile, file); | ||||
delete file; | delete file; | ||||
if (!ok) { | if (!ok) { | ||||
fprintf(stderr, "heap profiling not supported\n"); | fprintf(stderr, "heap profiling not supported\n"); | ||||
Env::Default()->DeleteFile(fname); | g_env->DeleteFile(fname); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
} // namespace leveldb | } // namespace leveldb | ||||
int main(int argc, char** argv) { | int main(int argc, char** argv) { | ||||
FLAGS_write_buffer_size = leveldb::Options().write_buffer_size; | FLAGS_write_buffer_size = leveldb::Options().write_buffer_size; | ||||
FLAGS_max_file_size = leveldb::Options().max_file_size; | |||||
FLAGS_block_size = leveldb::Options().block_size; | |||||
FLAGS_open_files = leveldb::Options().max_open_files; | FLAGS_open_files = leveldb::Options().max_open_files; | ||||
std::string default_db_path; | std::string default_db_path; | ||||
for (int i = 1; i < argc; i++) { | for (int i = 1; i < argc; i++) { | ||||
double d; | double d; | ||||
int n; | int n; | ||||
char junk; | char junk; | ||||
if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) { | if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) { | ||||
Show All 14 Lines | for (int i = 1; i < argc; i++) { | ||||
} else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) { | ||||
FLAGS_reads = n; | FLAGS_reads = n; | ||||
} else if (sscanf(argv[i], "--threads=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--threads=%d%c", &n, &junk) == 1) { | ||||
FLAGS_threads = n; | FLAGS_threads = n; | ||||
} else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) { | ||||
FLAGS_value_size = n; | FLAGS_value_size = n; | ||||
} else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) { | ||||
FLAGS_write_buffer_size = n; | FLAGS_write_buffer_size = n; | ||||
} else if (sscanf(argv[i], "--max_file_size=%d%c", &n, &junk) == 1) { | |||||
FLAGS_max_file_size = n; | |||||
} else if (sscanf(argv[i], "--block_size=%d%c", &n, &junk) == 1) { | |||||
FLAGS_block_size = n; | |||||
} else if (sscanf(argv[i], "--cache_size=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--cache_size=%d%c", &n, &junk) == 1) { | ||||
FLAGS_cache_size = n; | FLAGS_cache_size = n; | ||||
} else if (sscanf(argv[i], "--bloom_bits=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--bloom_bits=%d%c", &n, &junk) == 1) { | ||||
FLAGS_bloom_bits = n; | FLAGS_bloom_bits = n; | ||||
} else if (sscanf(argv[i], "--open_files=%d%c", &n, &junk) == 1) { | } else if (sscanf(argv[i], "--open_files=%d%c", &n, &junk) == 1) { | ||||
FLAGS_open_files = n; | FLAGS_open_files = n; | ||||
} else if (strncmp(argv[i], "--db=", 5) == 0) { | } else if (strncmp(argv[i], "--db=", 5) == 0) { | ||||
FLAGS_db = argv[i] + 5; | FLAGS_db = argv[i] + 5; | ||||
} else { | } else { | ||||
fprintf(stderr, "Invalid flag '%s'\n", argv[i]); | fprintf(stderr, "Invalid flag '%s'\n", argv[i]); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
} | } | ||||
leveldb::g_env = leveldb::Env::Default(); | |||||
// Choose a location for the test database if none given with --db=<path> | // Choose a location for the test database if none given with --db=<path> | ||||
if (FLAGS_db == NULL) { | if (FLAGS_db == NULL) { | ||||
leveldb::Env::Default()->GetTestDirectory(&default_db_path); | leveldb::g_env->GetTestDirectory(&default_db_path); | ||||
default_db_path += "/dbbench"; | default_db_path += "/dbbench"; | ||||
FLAGS_db = default_db_path.c_str(); | FLAGS_db = default_db_path.c_str(); | ||||
} | } | ||||
leveldb::Benchmark benchmark; | leveldb::Benchmark benchmark; | ||||
benchmark.Run(); | benchmark.Run(); | ||||
return 0; | return 0; | ||||
} | } |