Created
November 5, 2014 16:13
-
-
Save siddontang/4008663f5776bc6ffc58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <string> | |
#include "rocksdb/db.h" | |
#include "rocksdb/slice.h" | |
#include "rocksdb/options.h" | |
#include <unistd.h> | |
#include <sys/time.h> | |
using namespace rocksdb; | |
std::string kDBPath = "./vartest"; | |
int64_t NowMicros() { | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
return (uint64_t)(tv.tv_sec) * 1000000 + tv.tv_usec; | |
} | |
int main() { | |
DB* db; | |
Options options; | |
// Optimize RocksDB. This is the easiest way to get RocksDB to perform well | |
options.IncreaseParallelism(); | |
options.OptimizeLevelStyleCompaction(); | |
// create the DB if it's not already present | |
options.create_if_missing = true; | |
options.compression = kNoCompression; | |
// open DB | |
Status s = DB::Open(options, kDBPath, &db); | |
if(!s.ok()) { | |
printf("%s\n", s.ToString().c_str()); | |
} | |
assert(s.ok()); | |
char *buff = (char*)malloc(4096); | |
memset(buff, 1, 4096); | |
int num = 500000; | |
char **keys; | |
keys = (char **) malloc (sizeof(char*) * num); | |
for (int i = 0; i < num; i++) { | |
keys[i] = (char *)malloc(sizeof(char) * 4); | |
memcpy(keys[i], &i, sizeof(i)); | |
} | |
// Put key-value | |
uint64_t tot = 0; | |
uint64_t st, ed; | |
for (int i = 0; i < num; i++) { | |
s = db->Put(WriteOptions(), Slice(keys[i], 4), Slice(buff, 4096)); | |
} | |
std::string value; | |
for (int i = 0; i < num; i++) { | |
st = NowMicros(); | |
s = db->Get(ReadOptions(), Slice(keys[i], 4), &value); | |
ed = NowMicros(); | |
tot += (ed - st); | |
// usleep(10); | |
// printf("total time %lld\n", tot); | |
value.clear(); | |
} | |
printf("total time %lld\n", tot); | |
value.clear(); | |
tot = 0; | |
for (int i = 0; i < num; i++) { | |
st = NowMicros(); | |
s = db->Get(ReadOptions(), Slice(keys[i], 4), &value); | |
ed = NowMicros(); | |
tot += (ed - st); | |
usleep(10); | |
// printf("total time %lld\n", tot); | |
value.clear(); | |
} | |
printf("total time %lld\n", tot); | |
value.clear(); | |
tot = 0; | |
for (int i = 0; i < num; i++) { | |
st = NowMicros(); | |
s = db->Get(ReadOptions(), Slice(keys[i], 4), &value); | |
ed = NowMicros(); | |
tot += (ed - st); | |
// usleep(10); | |
// printf("total time %lld\n", tot); | |
value.clear(); | |
} | |
printf("total time %lld\n", tot); | |
value.clear(); | |
tot = 0; | |
for (int i = 0; i < num; i++) { | |
st = NowMicros(); | |
s = db->Get(ReadOptions(), Slice(keys[i], 4), &value); | |
ed = NowMicros(); | |
tot += (ed - st); | |
usleep(10); | |
// printf("total time %lld\n", tot); | |
value.clear(); | |
} | |
printf("total time %lld\n", tot); | |
value.clear(); | |
tot = 0; | |
for (int i = 0; i < num; i++) { | |
st = NowMicros(); | |
s = db->Get(ReadOptions(), Slice(keys[i], 4), &value); | |
ed = NowMicros(); | |
tot += (ed - st); | |
// usleep(10); | |
// printf("total time %lld\n", tot); | |
value.clear(); | |
} | |
printf("total time %lld\n", tot); | |
// assert(s.ok()); | |
// // get value | |
// s = db->Get(ReadOptions(), "key", &value); | |
// assert(s.ok()); | |
// assert(value == "value"); | |
delete db; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment