2013-10-31 21:38:54 +01:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
#include <gflags/gflags.h>
|
|
|
|
|
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/table.h"
|
2013-11-16 07:23:12 +01:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2013-10-31 21:38:54 +01:00
|
|
|
#include "db/db_impl.h"
|
2013-11-16 07:23:12 +01:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "port/atomic_pointer.h"
|
2013-10-31 21:38:54 +01:00
|
|
|
#include "table/block_based_table_factory.h"
|
|
|
|
#include "util/histogram.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
// Make a key that i determines the first 4 characters and j determines the
|
|
|
|
// last 4 characters.
|
2013-11-16 07:23:12 +01:00
|
|
|
static std::string MakeKey(int i, int j, bool through_db) {
|
2013-10-31 21:38:54 +01:00
|
|
|
char buf[100];
|
2013-11-16 07:23:12 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%04d__key___%04d", i, j);
|
|
|
|
if (through_db) {
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
// If we directly query table, which operates on internal keys
|
|
|
|
// instead of user keys, we need to add 8 bytes of internal
|
|
|
|
// information (row type etc) to user key to make an internal
|
|
|
|
// key.
|
|
|
|
InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
|
|
|
|
return key.Encode().ToString();
|
2013-10-31 21:38:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool DummySaveValue(void* arg, const Slice& ikey, const Slice& v,
|
|
|
|
bool didIO) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A very simple benchmark that.
|
|
|
|
// Create a table with roughly numKey1 * numKey2 keys,
|
|
|
|
// where there are numKey1 prefixes of the key, each has numKey2 number of
|
|
|
|
// distinguished key, differing in the suffix part.
|
|
|
|
// If if_query_empty_keys = false, query the existing keys numKey1 * numKey2
|
|
|
|
// times randomly.
|
|
|
|
// If if_query_empty_keys = true, query numKey1 * numKey2 random empty keys.
|
|
|
|
// Print out the total time.
|
2013-11-16 07:23:12 +01:00
|
|
|
// If through_db=true, a full DB will be created and queries will be against
|
|
|
|
// it. Otherwise, operations will be directly through table level.
|
2013-10-31 21:38:54 +01:00
|
|
|
//
|
|
|
|
// If for_terator=true, instead of just query one key each time, it queries
|
|
|
|
// a range sharing the same prefix.
|
|
|
|
void TableReaderBenchmark(Options& opts, EnvOptions& env_options,
|
2013-11-16 07:23:12 +01:00
|
|
|
ReadOptions& read_options, int num_keys1,
|
|
|
|
int num_keys2, int num_iter, int prefix_len,
|
|
|
|
bool if_query_empty_keys, bool for_iterator,
|
|
|
|
bool through_db) {
|
|
|
|
Slice prefix = Slice();
|
|
|
|
|
2013-10-31 21:38:54 +01:00
|
|
|
std::string file_name = test::TmpDir()
|
|
|
|
+ "/rocksdb_table_reader_benchmark";
|
2013-11-16 07:23:12 +01:00
|
|
|
std::string dbname = test::TmpDir() + "/rocksdb_table_reader_bench_db";
|
2013-10-31 21:38:54 +01:00
|
|
|
ReadOptions ro;
|
2013-11-16 07:23:12 +01:00
|
|
|
WriteOptions wo;
|
2013-10-31 21:38:54 +01:00
|
|
|
unique_ptr<WritableFile> file;
|
|
|
|
Env* env = Env::Default();
|
2013-11-16 07:23:12 +01:00
|
|
|
TableBuilder* tb = nullptr;
|
|
|
|
DB* db = nullptr;
|
|
|
|
Status s;
|
|
|
|
if (!through_db) {
|
|
|
|
env->NewWritableFile(file_name, &file, env_options);
|
|
|
|
tb = opts.table_factory->GetTableBuilder(opts, file.get(),
|
|
|
|
CompressionType::kNoCompression);
|
|
|
|
} else {
|
|
|
|
s = DB::Open(opts, dbname, &db);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
ASSERT_TRUE(db != nullptr);
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
// Populate slightly more than 1M keys
|
|
|
|
for (int i = 0; i < num_keys1; i++) {
|
|
|
|
for (int j = 0; j < num_keys2; j++) {
|
2013-11-16 07:23:12 +01:00
|
|
|
std::string key = MakeKey(i * 2, j, through_db);
|
|
|
|
if (!through_db) {
|
|
|
|
tb->Add(key, key);
|
|
|
|
} else {
|
|
|
|
db->Put(wo, key, key);
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-16 07:23:12 +01:00
|
|
|
if (!through_db) {
|
|
|
|
tb->Finish();
|
|
|
|
file->Close();
|
|
|
|
} else {
|
|
|
|
db->Flush(FlushOptions());
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
|
|
|
|
unique_ptr<TableReader> table_reader;
|
|
|
|
unique_ptr<RandomAccessFile> raf;
|
2013-11-16 07:23:12 +01:00
|
|
|
if (!through_db) {
|
|
|
|
Status s = env->NewRandomAccessFile(file_name, &raf, env_options);
|
|
|
|
uint64_t file_size;
|
|
|
|
env->GetFileSize(file_name, &file_size);
|
|
|
|
s = opts.table_factory->GetTableReader(opts, env_options, std::move(raf),
|
|
|
|
file_size, &table_reader);
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
|
|
|
|
Random rnd(301);
|
2013-11-16 07:23:12 +01:00
|
|
|
std::string result;
|
2013-10-31 21:38:54 +01:00
|
|
|
HistogramImpl hist;
|
|
|
|
|
|
|
|
void* arg = nullptr;
|
|
|
|
for (int it = 0; it < num_iter; it++) {
|
|
|
|
for (int i = 0; i < num_keys1; i++) {
|
|
|
|
for (int j = 0; j < num_keys2; j++) {
|
|
|
|
int r1 = rnd.Uniform(num_keys1) * 2;
|
|
|
|
int r2 = rnd.Uniform(num_keys2);
|
2013-11-16 07:23:12 +01:00
|
|
|
if (if_query_empty_keys) {
|
|
|
|
r1++;
|
|
|
|
r2 = num_keys2 * 2 - r2;
|
|
|
|
}
|
|
|
|
|
2013-10-31 21:38:54 +01:00
|
|
|
if (!for_iterator) {
|
|
|
|
// Query one existing key;
|
2013-11-16 07:23:12 +01:00
|
|
|
std::string key = MakeKey(r1, r2, through_db);
|
2013-10-31 21:38:54 +01:00
|
|
|
uint64_t start_micros = env->NowMicros();
|
2013-11-16 07:23:12 +01:00
|
|
|
port::MemoryBarrier();
|
|
|
|
if (!through_db) {
|
|
|
|
s = table_reader->Get(ro, key, arg, DummySaveValue, nullptr);
|
|
|
|
} else {
|
|
|
|
s = db->Get(ro, key, &result);
|
|
|
|
}
|
|
|
|
port::MemoryBarrier();
|
2013-10-31 21:38:54 +01:00
|
|
|
hist.Add(env->NowMicros() - start_micros);
|
|
|
|
} else {
|
2013-11-16 07:23:12 +01:00
|
|
|
int r2_len;
|
|
|
|
if (if_query_empty_keys) {
|
|
|
|
r2_len = 0;
|
|
|
|
} else {
|
|
|
|
r2_len = rnd.Uniform(num_keys2) + 1;
|
|
|
|
if (r2_len + r2 > num_keys2) {
|
|
|
|
r2_len = num_keys2 - r2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string start_key = MakeKey(r1, r2, through_db);
|
|
|
|
std::string end_key = MakeKey(r1, r2 + r2_len, through_db);
|
|
|
|
if (prefix_len < 16) {
|
|
|
|
prefix = Slice(start_key.data(), prefix_len);
|
|
|
|
read_options.prefix = &prefix;
|
2013-10-31 21:38:54 +01:00
|
|
|
}
|
2013-10-31 23:26:06 +01:00
|
|
|
uint64_t total_time = 0;
|
2013-10-31 21:38:54 +01:00
|
|
|
uint64_t start_micros = env->NowMicros();
|
2013-11-16 07:23:12 +01:00
|
|
|
port::MemoryBarrier();
|
|
|
|
Iterator* iter;
|
|
|
|
if (!through_db) {
|
|
|
|
iter = table_reader->NewIterator(read_options);
|
|
|
|
} else {
|
|
|
|
iter = db->NewIterator(read_options);
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
int count = 0;
|
|
|
|
for(iter->Seek(start_key); iter->Valid(); iter->Next()) {
|
2013-11-16 07:23:12 +01:00
|
|
|
if (if_query_empty_keys) {
|
|
|
|
break;
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
// verify key;
|
2013-11-16 07:23:12 +01:00
|
|
|
port::MemoryBarrier();
|
2013-10-31 23:26:06 +01:00
|
|
|
total_time += env->NowMicros() - start_micros;
|
2013-11-16 07:23:12 +01:00
|
|
|
assert(Slice(MakeKey(r1, r2 + count, through_db)) == iter->key());
|
2013-10-31 21:38:54 +01:00
|
|
|
start_micros = env->NowMicros();
|
|
|
|
if (++count >= r2_len) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count != r2_len) {
|
|
|
|
fprintf(
|
|
|
|
stderr, "Iterator cannot iterate expected number of entries. "
|
|
|
|
"Expected %d but got %d\n", r2_len, count);
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
delete iter;
|
2013-11-16 07:23:12 +01:00
|
|
|
port::MemoryBarrier();
|
2013-10-31 23:26:06 +01:00
|
|
|
total_time += env->NowMicros() - start_micros;
|
|
|
|
hist.Add(total_time);
|
2013-10-31 21:38:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"==================================================="
|
|
|
|
"====================================================\n"
|
|
|
|
"InMemoryTableSimpleBenchmark: %20s num_key1: %5d "
|
|
|
|
"num_key2: %5d %10s\n"
|
|
|
|
"==================================================="
|
|
|
|
"===================================================="
|
2013-10-31 23:26:06 +01:00
|
|
|
"\nHistogram (unit: microseconds): \n%s",
|
2013-11-16 07:23:12 +01:00
|
|
|
opts.table_factory->Name(), num_keys1, num_keys2,
|
2013-10-31 21:38:54 +01:00
|
|
|
for_iterator? "iterator" : (if_query_empty_keys ? "empty" : "non_empty"),
|
|
|
|
hist.ToString().c_str());
|
2013-11-16 07:23:12 +01:00
|
|
|
if (!through_db) {
|
|
|
|
env->DeleteFile(file_name);
|
|
|
|
} else {
|
|
|
|
delete db;
|
|
|
|
db = nullptr;
|
|
|
|
DestroyDB(dbname, opts);
|
|
|
|
}
|
2013-10-31 21:38:54 +01:00
|
|
|
}
|
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
DEFINE_bool(query_empty, false, "query non-existing keys instead of existing "
|
|
|
|
"ones.");
|
|
|
|
DEFINE_int32(num_keys1, 4096, "number of distinguish prefix of keys");
|
|
|
|
DEFINE_int32(num_keys2, 512, "number of distinguish keys for each prefix");
|
|
|
|
DEFINE_int32(iter, 3, "query non-existing keys instead of existing ones");
|
2013-11-16 07:23:12 +01:00
|
|
|
DEFINE_int32(prefix_len, 16, "Prefix length used for iterators and indexes");
|
2013-10-31 21:38:54 +01:00
|
|
|
DEFINE_bool(iterator, false, "For test iterator");
|
2013-11-16 07:23:12 +01:00
|
|
|
DEFINE_bool(through_db, false, "If enable, a DB instance will be created and "
|
|
|
|
"the query will be against DB. Otherwise, will be directly against "
|
|
|
|
"a table reader.");
|
2013-10-31 21:38:54 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
google::SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv[0]) +
|
|
|
|
" [OPTIONS]...");
|
|
|
|
google::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
|
|
2013-11-16 07:23:12 +01:00
|
|
|
rocksdb::TableFactory* tf = new rocksdb::BlockBasedTableFactory();
|
2013-10-31 21:38:54 +01:00
|
|
|
rocksdb::Options options;
|
2013-11-16 07:23:12 +01:00
|
|
|
if (FLAGS_prefix_len < 16) {
|
|
|
|
options.prefix_extractor = rocksdb::NewFixedPrefixTransform(
|
|
|
|
FLAGS_prefix_len);
|
|
|
|
}
|
|
|
|
options.SetUpDefaultFlushBlockPolicyFactory();
|
2013-10-31 21:38:54 +01:00
|
|
|
rocksdb::ReadOptions ro;
|
|
|
|
rocksdb::EnvOptions env_options;
|
2013-11-16 07:23:12 +01:00
|
|
|
options.create_if_missing = true;
|
|
|
|
options.table_factory =
|
|
|
|
std::shared_ptr<rocksdb::TableFactory>(tf);
|
|
|
|
TableReaderBenchmark(options, env_options, ro, FLAGS_num_keys1,
|
|
|
|
FLAGS_num_keys2, FLAGS_iter, FLAGS_prefix_len,
|
|
|
|
FLAGS_query_empty, FLAGS_iterator, FLAGS_through_db);
|
2013-10-31 21:38:54 +01:00
|
|
|
delete tf;
|
|
|
|
return 0;
|
|
|
|
}
|