24
24
// overwrite -- overwrite N values in random key order in async mode
25
25
// fillsync -- write N/100 values in random key order in sync mode
26
26
// fill100K -- write N/1000 100K values in random order in async mode
27
- // readseq -- read N values sequentially
28
- // readreverse -- read N values in reverse order
29
- // readrandom -- read N values in random order
27
+ // readseq -- read N times sequentially
28
+ // readreverse -- read N times in reverse order
29
+ // readrandom -- read N times in random order
30
+ // readhot -- read N times in random order from 1% section of DB
30
31
// crc32c -- repeated crc32c of 4K of data
31
32
// Meta operations:
32
33
// compact -- Compact the entire DB
@@ -54,6 +55,9 @@ static const char* FLAGS_benchmarks =
54
55
// Number of key/values to place in database
55
56
static int FLAGS_num = 1000000 ;
56
57
58
+ // Number of read operations to do. If negative, do FLAGS_num reads.
59
+ static int FLAGS_reads = -1 ;
60
+
57
61
// Size of each value
58
62
static int FLAGS_value_size = 100 ;
59
63
@@ -72,6 +76,14 @@ static int FLAGS_write_buffer_size = 0;
72
76
// Negative means use default settings.
73
77
static int FLAGS_cache_size = -1 ;
74
78
79
+ // Maximum number of files to keep open at the same time (use default if == 0)
80
+ static int FLAGS_open_files = 0 ;
81
+
82
+ // If true, do not destroy the existing database. If you set this
83
+ // flag and also specify a benchmark that wants a fresh database, that
84
+ // benchmark will fail.
85
+ static bool FLAGS_use_existing_db = false ;
86
+
75
87
namespace leveldb {
76
88
77
89
// Helper for quickly generating random data.
@@ -126,6 +138,7 @@ class Benchmark {
126
138
Cache* cache_;
127
139
DB* db_;
128
140
int num_;
141
+ int reads_;
129
142
int heap_counter_;
130
143
double start_;
131
144
double last_op_finish_;
@@ -298,6 +311,7 @@ class Benchmark {
298
311
: cache_(FLAGS_cache_size >= 0 ? NewLRUCache(FLAGS_cache_size) : NULL ),
299
312
db_ (NULL ),
300
313
num_(FLAGS_num),
314
+ reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
301
315
heap_counter_(0 ),
302
316
bytes_(0 ),
303
317
rand_(301 ) {
@@ -308,7 +322,9 @@ class Benchmark {
308
322
Env::Default ()->DeleteFile (" /tmp/dbbench/" + files[i]);
309
323
}
310
324
}
311
- DestroyDB (" /tmp/dbbench" , Options ());
325
+ if (!FLAGS_use_existing_db) {
326
+ DestroyDB (" /tmp/dbbench" , Options ());
327
+ }
312
328
}
313
329
314
330
~Benchmark () {
@@ -355,11 +371,13 @@ class Benchmark {
355
371
ReadReverse ();
356
372
} else if (name == Slice (" readrandom" )) {
357
373
ReadRandom ();
374
+ } else if (name == Slice (" readhot" )) {
375
+ ReadHot ();
358
376
} else if (name == Slice (" readrandomsmall" )) {
359
- int n = num_ ;
360
- num_ /= 1000 ;
377
+ int n = reads_ ;
378
+ reads_ /= 1000 ;
361
379
ReadRandom ();
362
- num_ = n;
380
+ reads_ = n;
363
381
} else if (name == Slice (" compact" )) {
364
382
Compact ();
365
383
} else if (name == Slice (" crc32c" )) {
@@ -449,7 +467,7 @@ class Benchmark {
449
467
void Open () {
450
468
assert (db_ == NULL );
451
469
Options options;
452
- options.create_if_missing = true ;
470
+ options.create_if_missing = !FLAGS_use_existing_db ;
453
471
options.block_cache = cache_;
454
472
options.write_buffer_size = FLAGS_write_buffer_size;
455
473
Status s = DB::Open (options, " /tmp/dbbench" , &db_);
@@ -462,6 +480,10 @@ class Benchmark {
462
480
void Write (const WriteOptions& options, Order order, DBState state,
463
481
int num_entries, int value_size, int entries_per_batch) {
464
482
if (state == FRESH) {
483
+ if (FLAGS_use_existing_db) {
484
+ message_ = " skipping (--use_existing_db is true)" ;
485
+ return ;
486
+ }
465
487
delete db_;
466
488
db_ = NULL ;
467
489
DestroyDB (" /tmp/dbbench" , Options ());
@@ -499,7 +521,7 @@ class Benchmark {
499
521
void ReadSequential () {
500
522
Iterator* iter = db_->NewIterator (ReadOptions ());
501
523
int i = 0 ;
502
- for (iter->SeekToFirst (); i < num_ && iter->Valid (); iter->Next ()) {
524
+ for (iter->SeekToFirst (); i < reads_ && iter->Valid (); iter->Next ()) {
503
525
bytes_ += iter->key ().size () + iter->value ().size ();
504
526
FinishedSingleOp ();
505
527
++i;
@@ -510,7 +532,7 @@ class Benchmark {
510
532
void ReadReverse () {
511
533
Iterator* iter = db_->NewIterator (ReadOptions ());
512
534
int i = 0 ;
513
- for (iter->SeekToLast (); i < num_ && iter->Valid (); iter->Prev ()) {
535
+ for (iter->SeekToLast (); i < reads_ && iter->Valid (); iter->Prev ()) {
514
536
bytes_ += iter->key ().size () + iter->value ().size ();
515
537
FinishedSingleOp ();
516
538
++i;
@@ -521,7 +543,7 @@ class Benchmark {
521
543
void ReadRandom () {
522
544
ReadOptions options;
523
545
std::string value;
524
- for (int i = 0 ; i < num_ ; i++) {
546
+ for (int i = 0 ; i < reads_ ; i++) {
525
547
char key[100 ];
526
548
const int k = rand_.Next () % FLAGS_num;
527
549
snprintf (key, sizeof (key), " %016d" , k);
@@ -530,6 +552,19 @@ class Benchmark {
530
552
}
531
553
}
532
554
555
+ void ReadHot () {
556
+ ReadOptions options;
557
+ std::string value;
558
+ const int range = (FLAGS_num + 99 ) / 100 ;
559
+ for (int i = 0 ; i < reads_; i++) {
560
+ char key[100 ];
561
+ const int k = rand_.Next () % range;
562
+ snprintf (key, sizeof (key), " %016d" , k);
563
+ db_->Get (options, key, &value);
564
+ FinishedSingleOp ();
565
+ }
566
+ }
567
+
533
568
void Compact () {
534
569
DBImpl* dbi = reinterpret_cast <DBImpl*>(db_);
535
570
dbi->TEST_CompactMemTable ();
@@ -582,6 +617,8 @@ class Benchmark {
582
617
583
618
int main (int argc, char ** argv) {
584
619
FLAGS_write_buffer_size = leveldb::Options ().write_buffer_size ;
620
+ FLAGS_open_files = leveldb::Options ().max_open_files ;
621
+
585
622
for (int i = 1 ; i < argc; i++) {
586
623
double d;
587
624
int n;
@@ -593,14 +630,21 @@ int main(int argc, char** argv) {
593
630
} else if (sscanf (argv[i], " --histogram=%d%c" , &n, &junk) == 1 &&
594
631
(n == 0 || n == 1 )) {
595
632
FLAGS_histogram = n;
633
+ } else if (sscanf (argv[i], " --use_existing_db=%d%c" , &n, &junk) == 1 &&
634
+ (n == 0 || n == 1 )) {
635
+ FLAGS_use_existing_db = n;
596
636
} else if (sscanf (argv[i], " --num=%d%c" , &n, &junk) == 1 ) {
597
637
FLAGS_num = n;
638
+ } else if (sscanf (argv[i], " --reads=%d%c" , &n, &junk) == 1 ) {
639
+ FLAGS_reads = n;
598
640
} else if (sscanf (argv[i], " --value_size=%d%c" , &n, &junk) == 1 ) {
599
641
FLAGS_value_size = n;
600
642
} else if (sscanf (argv[i], " --write_buffer_size=%d%c" , &n, &junk) == 1 ) {
601
643
FLAGS_write_buffer_size = n;
602
644
} else if (sscanf (argv[i], " --cache_size=%d%c" , &n, &junk) == 1 ) {
603
645
FLAGS_cache_size = n;
646
+ } else if (sscanf (argv[i], " --open_files=%d%c" , &n, &junk) == 1 ) {
647
+ FLAGS_open_files = n;
604
648
} else {
605
649
fprintf (stderr, " Invalid flag '%s'\n " , argv[i]);
606
650
exit (1 );
0 commit comments