Skip to content

Commit 7a92537

Browse files
author
Lei Jin
committed
db_bench: add IteratorCreationWhileWriting mode and allow prefix_seek
Summary: as title Test Plan: ran it Reviewers: igor, haobo, yhchiang Reviewed By: igor CC: leveldb Differential Revision: https://reviews.facebook.net/D17655
1 parent ca4fa20 commit 7a92537

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

db/db_bench.cc

+60-42
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ DEFINE_string(benchmarks,
4747
"overwrite,"
4848
"readrandom,"
4949
"readrandom,"
50+
"newiterator,"
51+
"newiteratorwhilewriting,"
5052
"readseq,"
5153
"readreverse,"
5254
"compact,"
@@ -1172,6 +1174,9 @@ class Benchmark {
11721174
method = &Benchmark::ReadRandom;
11731175
} else if (name == Slice("newiterator")) {
11741176
method = &Benchmark::IteratorCreation;
1177+
} else if (name == Slice("newiteratorwhilewriting")) {
1178+
num_threads++; // Add extra thread for writing
1179+
method = &Benchmark::IteratorCreationWhileWriting;
11751180
} else if (name == Slice("seekrandom")) {
11761181
method = &Benchmark::SeekRandom;
11771182
} else if (name == Slice("readrandomsmall")) {
@@ -1864,13 +1869,22 @@ class Benchmark {
18641869
void IteratorCreation(ThreadState* thread) {
18651870
Duration duration(FLAGS_duration, reads_);
18661871
ReadOptions options(FLAGS_verify_checksum, true);
1872+
options.prefix_seek = (FLAGS_prefix_size > 0);
18671873
while (!duration.Done(1)) {
18681874
Iterator* iter = db_->NewIterator(options);
18691875
delete iter;
18701876
thread->stats.FinishedSingleOp(db_);
18711877
}
18721878
}
18731879

1880+
void IteratorCreationWhileWriting(ThreadState* thread) {
1881+
if (thread->tid > 0) {
1882+
IteratorCreation(thread);
1883+
} else {
1884+
BGWriter(thread);
1885+
}
1886+
}
1887+
18741888
void SeekRandom(ThreadState* thread) {
18751889
int64_t read = 0;
18761890
int64_t found = 0;
@@ -1934,53 +1948,57 @@ class Benchmark {
19341948
if (thread->tid > 0) {
19351949
ReadRandom(thread);
19361950
} else {
1937-
// Special thread that keeps writing until other threads are done.
1938-
RandomGenerator gen;
1939-
double last = FLAGS_env->NowMicros();
1940-
int writes_per_second_by_10 = 0;
1941-
int num_writes = 0;
1942-
1943-
// --writes_per_second rate limit is enforced per 100 milliseconds
1944-
// intervals to avoid a burst of writes at the start of each second.
1945-
1946-
if (FLAGS_writes_per_second > 0)
1947-
writes_per_second_by_10 = FLAGS_writes_per_second / 10;
1948-
1949-
// Don't merge stats from this thread with the readers.
1950-
thread->stats.SetExcludeFromMerge();
1951-
1952-
Slice key = AllocateKey();
1953-
std::unique_ptr<const char[]> key_guard(key.data());
1954-
1955-
while (true) {
1956-
{
1957-
MutexLock l(&thread->shared->mu);
1958-
if (thread->shared->num_done + 1 >= thread->shared->num_initialized) {
1959-
// Other threads have finished
1960-
break;
1961-
}
1962-
}
1951+
BGWriter(thread);
1952+
}
1953+
}
19631954

1964-
GenerateKeyFromInt(thread->rand.Next() % FLAGS_num, FLAGS_num, &key);
1965-
Status s = db_->Put(write_options_, key, gen.Generate(value_size_));
1966-
if (!s.ok()) {
1967-
fprintf(stderr, "put error: %s\n", s.ToString().c_str());
1968-
exit(1);
1955+
void BGWriter(ThreadState* thread) {
1956+
// Special thread that keeps writing until other threads are done.
1957+
RandomGenerator gen;
1958+
double last = FLAGS_env->NowMicros();
1959+
int writes_per_second_by_10 = 0;
1960+
int num_writes = 0;
1961+
1962+
// --writes_per_second rate limit is enforced per 100 milliseconds
1963+
// intervals to avoid a burst of writes at the start of each second.
1964+
1965+
if (FLAGS_writes_per_second > 0)
1966+
writes_per_second_by_10 = FLAGS_writes_per_second / 10;
1967+
1968+
// Don't merge stats from this thread with the readers.
1969+
thread->stats.SetExcludeFromMerge();
1970+
1971+
Slice key = AllocateKey();
1972+
std::unique_ptr<const char[]> key_guard(key.data());
1973+
1974+
while (true) {
1975+
{
1976+
MutexLock l(&thread->shared->mu);
1977+
if (thread->shared->num_done + 1 >= thread->shared->num_initialized) {
1978+
// Other threads have finished
1979+
break;
19691980
}
1970-
thread->stats.FinishedSingleOp(db_);
1981+
}
1982+
1983+
GenerateKeyFromInt(thread->rand.Next() % FLAGS_num, FLAGS_num, &key);
1984+
Status s = db_->Put(write_options_, key, gen.Generate(value_size_));
1985+
if (!s.ok()) {
1986+
fprintf(stderr, "put error: %s\n", s.ToString().c_str());
1987+
exit(1);
1988+
}
1989+
thread->stats.FinishedSingleOp(db_);
19711990

1972-
++num_writes;
1973-
if (writes_per_second_by_10 && num_writes >= writes_per_second_by_10) {
1974-
double now = FLAGS_env->NowMicros();
1975-
double usecs_since_last = now - last;
1991+
++num_writes;
1992+
if (writes_per_second_by_10 && num_writes >= writes_per_second_by_10) {
1993+
double now = FLAGS_env->NowMicros();
1994+
double usecs_since_last = now - last;
19761995

1977-
num_writes = 0;
1978-
last = now;
1996+
num_writes = 0;
1997+
last = now;
19791998

1980-
if (usecs_since_last < 100000.0) {
1981-
FLAGS_env->SleepForMicroseconds(100000.0 - usecs_since_last);
1982-
last = FLAGS_env->NowMicros();
1983-
}
1999+
if (usecs_since_last < 100000.0) {
2000+
FLAGS_env->SleepForMicroseconds(100000.0 - usecs_since_last);
2001+
last = FLAGS_env->NowMicros();
19842002
}
19852003
}
19862004
}

0 commit comments

Comments
 (0)