1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5 #include <sqlite3.h>
6
7 #include <cstdio>
8 #include <cstdlib>
9
10 #include "util/histogram.h"
11 #include "util/random.h"
12 #include "util/testutil.h"
13
14 // Comma-separated list of operations to run in the specified order
15 // Actual benchmarks:
16 //
17 // fillseq -- write N values in sequential key order in async mode
18 // fillseqsync -- write N/100 values in sequential key order in sync mode
19 // fillseqbatch -- batch write N values in sequential key order in async mode
20 // fillrandom -- write N values in random key order in async mode
21 // fillrandsync -- write N/100 values in random key order in sync mode
22 // fillrandbatch -- batch write N values in sequential key order in async mode
23 // overwrite -- overwrite N values in random key order in async mode
24 // fillrand100K -- write N/1000 100K values in random order in async mode
25 // fillseq100K -- write N/1000 100K values in sequential order in async mode
26 // readseq -- read N times sequentially
27 // readrandom -- read N times in random order
28 // readrand100K -- read N/1000 100K values in sequential order in async mode
29 static const char* FLAGS_benchmarks =
30 "fillseq,"
31 "fillseqsync,"
32 "fillseqbatch,"
33 "fillrandom,"
34 "fillrandsync,"
35 "fillrandbatch,"
36 "overwrite,"
37 "overwritebatch,"
38 "readrandom,"
39 "readseq,"
40 "fillrand100K,"
41 "fillseq100K,"
42 "readseq,"
43 "readrand100K,";
44
45 // Number of key/values to place in database
46 static int FLAGS_num = 1000000;
47
48 // Number of read operations to do. If negative, do FLAGS_num reads.
49 static int FLAGS_reads = -1;
50
51 // Size of each value
52 static int FLAGS_value_size = 100;
53
54 // Print histogram of operation timings
55 static bool FLAGS_histogram = false;
56
57 // Arrange to generate values that shrink to this fraction of
58 // their original size after compression
59 static double FLAGS_compression_ratio = 0.5;
60
61 // Page size. Default 1 KB.
62 static int FLAGS_page_size = 1024;
63
64 // Number of pages.
65 // Default cache size = FLAGS_page_size * FLAGS_num_pages = 4 MB.
66 static int FLAGS_num_pages = 4096;
67
68 // If true, do not destroy the existing database. If you set this
69 // flag and also specify a benchmark that wants a fresh database, that
70 // benchmark will fail.
71 static bool FLAGS_use_existing_db = false;
72
73 // If true, the SQLite table has ROWIDs.
74 static bool FLAGS_use_rowids = false;
75
76 // If true, we allow batch writes to occur
77 static bool FLAGS_transaction = true;
78
79 // If true, we enable Write-Ahead Logging
80 static bool FLAGS_WAL_enabled = true;
81
82 // Use the db with the following name.
83 static const char* FLAGS_db = nullptr;
84
ExecErrorCheck(int status,char * err_msg)85 inline static void ExecErrorCheck(int status, char* err_msg) {
86 if (status != SQLITE_OK) {
87 std::fprintf(stderr, "SQL error: %s\n", err_msg);
88 sqlite3_free(err_msg);
89 std::exit(1);
90 }
91 }
92
StepErrorCheck(int status)93 inline static void StepErrorCheck(int status) {
94 if (status != SQLITE_DONE) {
95 std::fprintf(stderr, "SQL step error: status = %d\n", status);
96 std::exit(1);
97 }
98 }
99
ErrorCheck(int status)100 inline static void ErrorCheck(int status) {
101 if (status != SQLITE_OK) {
102 std::fprintf(stderr, "sqlite3 error: status = %d\n", status);
103 std::exit(1);
104 }
105 }
106
WalCheckpoint(sqlite3 * db_)107 inline static void WalCheckpoint(sqlite3* db_) {
108 // Flush all writes to disk
109 if (FLAGS_WAL_enabled) {
110 sqlite3_wal_checkpoint_v2(db_, nullptr, SQLITE_CHECKPOINT_FULL, nullptr,
111 nullptr);
112 }
113 }
114
115 namespace leveldb {
116
117 // Helper for quickly generating random data.
118 namespace {
119 class RandomGenerator {
120 private:
121 std::string data_;
122 int pos_;
123
124 public:
RandomGenerator()125 RandomGenerator() {
126 // We use a limited amount of data over and over again and ensure
127 // that it is larger than the compression window (32KB), and also
128 // large enough to serve all typical value sizes we want to write.
129 Random rnd(301);
130 std::string piece;
131 while (data_.size() < 1048576) {
132 // Add a short fragment that is as compressible as specified
133 // by FLAGS_compression_ratio.
134 test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);
135 data_.append(piece);
136 }
137 pos_ = 0;
138 }
139
Generate(int len)140 Slice Generate(int len) {
141 if (pos_ + len > data_.size()) {
142 pos_ = 0;
143 assert(len < data_.size());
144 }
145 pos_ += len;
146 return Slice(data_.data() + pos_ - len, len);
147 }
148 };
149
TrimSpace(Slice s)150 static Slice TrimSpace(Slice s) {
151 int start = 0;
152 while (start < s.size() && isspace(s[start])) {
153 start++;
154 }
155 int limit = s.size();
156 while (limit > start && isspace(s[limit - 1])) {
157 limit--;
158 }
159 return Slice(s.data() + start, limit - start);
160 }
161
162 } // namespace
163
164 class Benchmark {
165 private:
166 sqlite3* db_;
167 int db_num_;
168 int num_;
169 int reads_;
170 double start_;
171 double last_op_finish_;
172 int64_t bytes_;
173 std::string message_;
174 Histogram hist_;
175 RandomGenerator gen_;
176 Random rand_;
177
178 // State kept for progress messages
179 int done_;
180 int next_report_; // When to report next
181
PrintHeader()182 void PrintHeader() {
183 const int kKeySize = 16;
184 PrintEnvironment();
185 std::fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
186 std::fprintf(stdout, "Values: %d bytes each\n", FLAGS_value_size);
187 std::fprintf(stdout, "Entries: %d\n", num_);
188 std::fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
189 ((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_) /
190 1048576.0));
191 PrintWarnings();
192 std::fprintf(stdout, "------------------------------------------------\n");
193 }
194
PrintWarnings()195 void PrintWarnings() {
196 #if defined(__GNUC__) && !defined(__OPTIMIZE__)
197 std::fprintf(
198 stdout,
199 "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n");
200 #endif
201 #ifndef NDEBUG
202 std::fprintf(
203 stdout,
204 "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
205 #endif
206 }
207
PrintEnvironment()208 void PrintEnvironment() {
209 std::fprintf(stderr, "SQLite: version %s\n", SQLITE_VERSION);
210
211 #if defined(__linux)
212 time_t now = time(nullptr);
213 std::fprintf(stderr, "Date: %s",
214 ctime(&now)); // ctime() adds newline
215
216 FILE* cpuinfo = std::fopen("/proc/cpuinfo", "r");
217 if (cpuinfo != nullptr) {
218 char line[1000];
219 int num_cpus = 0;
220 std::string cpu_type;
221 std::string cache_size;
222 while (fgets(line, sizeof(line), cpuinfo) != nullptr) {
223 const char* sep = strchr(line, ':');
224 if (sep == nullptr) {
225 continue;
226 }
227 Slice key = TrimSpace(Slice(line, sep - 1 - line));
228 Slice val = TrimSpace(Slice(sep + 1));
229 if (key == "model name") {
230 ++num_cpus;
231 cpu_type = val.ToString();
232 } else if (key == "cache size") {
233 cache_size = val.ToString();
234 }
235 }
236 std::fclose(cpuinfo);
237 std::fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
238 std::fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
239 }
240 #endif
241 }
242
Start()243 void Start() {
244 start_ = Env::Default()->NowMicros() * 1e-6;
245 bytes_ = 0;
246 message_.clear();
247 last_op_finish_ = start_;
248 hist_.Clear();
249 done_ = 0;
250 next_report_ = 100;
251 }
252
FinishedSingleOp()253 void FinishedSingleOp() {
254 if (FLAGS_histogram) {
255 double now = Env::Default()->NowMicros() * 1e-6;
256 double micros = (now - last_op_finish_) * 1e6;
257 hist_.Add(micros);
258 if (micros > 20000) {
259 std::fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
260 std::fflush(stderr);
261 }
262 last_op_finish_ = now;
263 }
264
265 done_++;
266 if (done_ >= next_report_) {
267 if (next_report_ < 1000)
268 next_report_ += 100;
269 else if (next_report_ < 5000)
270 next_report_ += 500;
271 else if (next_report_ < 10000)
272 next_report_ += 1000;
273 else if (next_report_ < 50000)
274 next_report_ += 5000;
275 else if (next_report_ < 100000)
276 next_report_ += 10000;
277 else if (next_report_ < 500000)
278 next_report_ += 50000;
279 else
280 next_report_ += 100000;
281 std::fprintf(stderr, "... finished %d ops%30s\r", done_, "");
282 std::fflush(stderr);
283 }
284 }
285
Stop(const Slice & name)286 void Stop(const Slice& name) {
287 double finish = Env::Default()->NowMicros() * 1e-6;
288
289 // Pretend at least one op was done in case we are running a benchmark
290 // that does not call FinishedSingleOp().
291 if (done_ < 1) done_ = 1;
292
293 if (bytes_ > 0) {
294 char rate[100];
295 std::snprintf(rate, sizeof(rate), "%6.1f MB/s",
296 (bytes_ / 1048576.0) / (finish - start_));
297 if (!message_.empty()) {
298 message_ = std::string(rate) + " " + message_;
299 } else {
300 message_ = rate;
301 }
302 }
303
304 std::fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
305 name.ToString().c_str(), (finish - start_) * 1e6 / done_,
306 (message_.empty() ? "" : " "), message_.c_str());
307 if (FLAGS_histogram) {
308 std::fprintf(stdout, "Microseconds per op:\n%s\n",
309 hist_.ToString().c_str());
310 }
311 std::fflush(stdout);
312 }
313
314 public:
315 enum Order { SEQUENTIAL, RANDOM };
316 enum DBState { FRESH, EXISTING };
317
Benchmark()318 Benchmark()
319 : db_(nullptr),
320 db_num_(0),
321 num_(FLAGS_num),
322 reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
323 bytes_(0),
324 rand_(301) {
325 std::vector<std::string> files;
326 std::string test_dir;
327 Env::Default()->GetTestDirectory(&test_dir);
328 Env::Default()->GetChildren(test_dir, &files);
329 if (!FLAGS_use_existing_db) {
330 for (int i = 0; i < files.size(); i++) {
331 if (Slice(files[i]).starts_with("dbbench_sqlite3")) {
332 std::string file_name(test_dir);
333 file_name += "/";
334 file_name += files[i];
335 Env::Default()->RemoveFile(file_name.c_str());
336 }
337 }
338 }
339 }
340
~Benchmark()341 ~Benchmark() {
342 int status = sqlite3_close(db_);
343 ErrorCheck(status);
344 }
345
Run()346 void Run() {
347 PrintHeader();
348 Open();
349
350 const char* benchmarks = FLAGS_benchmarks;
351 while (benchmarks != nullptr) {
352 const char* sep = strchr(benchmarks, ',');
353 Slice name;
354 if (sep == nullptr) {
355 name = benchmarks;
356 benchmarks = nullptr;
357 } else {
358 name = Slice(benchmarks, sep - benchmarks);
359 benchmarks = sep + 1;
360 }
361
362 bytes_ = 0;
363 Start();
364
365 bool known = true;
366 bool write_sync = false;
367 if (name == Slice("fillseq")) {
368 Write(write_sync, SEQUENTIAL, FRESH, num_, FLAGS_value_size, 1);
369 WalCheckpoint(db_);
370 } else if (name == Slice("fillseqbatch")) {
371 Write(write_sync, SEQUENTIAL, FRESH, num_, FLAGS_value_size, 1000);
372 WalCheckpoint(db_);
373 } else if (name == Slice("fillrandom")) {
374 Write(write_sync, RANDOM, FRESH, num_, FLAGS_value_size, 1);
375 WalCheckpoint(db_);
376 } else if (name == Slice("fillrandbatch")) {
377 Write(write_sync, RANDOM, FRESH, num_, FLAGS_value_size, 1000);
378 WalCheckpoint(db_);
379 } else if (name == Slice("overwrite")) {
380 Write(write_sync, RANDOM, EXISTING, num_, FLAGS_value_size, 1);
381 WalCheckpoint(db_);
382 } else if (name == Slice("overwritebatch")) {
383 Write(write_sync, RANDOM, EXISTING, num_, FLAGS_value_size, 1000);
384 WalCheckpoint(db_);
385 } else if (name == Slice("fillrandsync")) {
386 write_sync = true;
387 Write(write_sync, RANDOM, FRESH, num_ / 100, FLAGS_value_size, 1);
388 WalCheckpoint(db_);
389 } else if (name == Slice("fillseqsync")) {
390 write_sync = true;
391 Write(write_sync, SEQUENTIAL, FRESH, num_ / 100, FLAGS_value_size, 1);
392 WalCheckpoint(db_);
393 } else if (name == Slice("fillrand100K")) {
394 Write(write_sync, RANDOM, FRESH, num_ / 1000, 100 * 1000, 1);
395 WalCheckpoint(db_);
396 } else if (name == Slice("fillseq100K")) {
397 Write(write_sync, SEQUENTIAL, FRESH, num_ / 1000, 100 * 1000, 1);
398 WalCheckpoint(db_);
399 } else if (name == Slice("readseq")) {
400 ReadSequential();
401 } else if (name == Slice("readrandom")) {
402 Read(RANDOM, 1);
403 } else if (name == Slice("readrand100K")) {
404 int n = reads_;
405 reads_ /= 1000;
406 Read(RANDOM, 1);
407 reads_ = n;
408 } else {
409 known = false;
410 if (name != Slice()) { // No error message for empty name
411 std::fprintf(stderr, "unknown benchmark '%s'\n",
412 name.ToString().c_str());
413 }
414 }
415 if (known) {
416 Stop(name);
417 }
418 }
419 }
420
Open()421 void Open() {
422 assert(db_ == nullptr);
423
424 int status;
425 char file_name[100];
426 char* err_msg = nullptr;
427 db_num_++;
428
429 // Open database
430 std::string tmp_dir;
431 Env::Default()->GetTestDirectory(&tmp_dir);
432 std::snprintf(file_name, sizeof(file_name), "%s/dbbench_sqlite3-%d.db",
433 tmp_dir.c_str(), db_num_);
434 status = sqlite3_open(file_name, &db_);
435 if (status) {
436 std::fprintf(stderr, "open error: %s\n", sqlite3_errmsg(db_));
437 std::exit(1);
438 }
439
440 // Change SQLite cache size
441 char cache_size[100];
442 std::snprintf(cache_size, sizeof(cache_size), "PRAGMA cache_size = %d",
443 FLAGS_num_pages);
444 status = sqlite3_exec(db_, cache_size, nullptr, nullptr, &err_msg);
445 ExecErrorCheck(status, err_msg);
446
447 // FLAGS_page_size is defaulted to 1024
448 if (FLAGS_page_size != 1024) {
449 char page_size[100];
450 std::snprintf(page_size, sizeof(page_size), "PRAGMA page_size = %d",
451 FLAGS_page_size);
452 status = sqlite3_exec(db_, page_size, nullptr, nullptr, &err_msg);
453 ExecErrorCheck(status, err_msg);
454 }
455
456 // Change journal mode to WAL if WAL enabled flag is on
457 if (FLAGS_WAL_enabled) {
458 std::string WAL_stmt = "PRAGMA journal_mode = WAL";
459
460 // LevelDB's default cache size is a combined 4 MB
461 std::string WAL_checkpoint = "PRAGMA wal_autocheckpoint = 4096";
462 status = sqlite3_exec(db_, WAL_stmt.c_str(), nullptr, nullptr, &err_msg);
463 ExecErrorCheck(status, err_msg);
464 status =
465 sqlite3_exec(db_, WAL_checkpoint.c_str(), nullptr, nullptr, &err_msg);
466 ExecErrorCheck(status, err_msg);
467 }
468
469 // Change locking mode to exclusive and create tables/index for database
470 std::string locking_stmt = "PRAGMA locking_mode = EXCLUSIVE";
471 std::string create_stmt =
472 "CREATE TABLE test (key blob, value blob, PRIMARY KEY(key))";
473 if (!FLAGS_use_rowids) create_stmt += " WITHOUT ROWID";
474 std::string stmt_array[] = {locking_stmt, create_stmt};
475 int stmt_array_length = sizeof(stmt_array) / sizeof(std::string);
476 for (int i = 0; i < stmt_array_length; i++) {
477 status =
478 sqlite3_exec(db_, stmt_array[i].c_str(), nullptr, nullptr, &err_msg);
479 ExecErrorCheck(status, err_msg);
480 }
481 }
482
Write(bool write_sync,Order order,DBState state,int num_entries,int value_size,int entries_per_batch)483 void Write(bool write_sync, Order order, DBState state, int num_entries,
484 int value_size, int entries_per_batch) {
485 // Create new database if state == FRESH
486 if (state == FRESH) {
487 if (FLAGS_use_existing_db) {
488 message_ = "skipping (--use_existing_db is true)";
489 return;
490 }
491 sqlite3_close(db_);
492 db_ = nullptr;
493 Open();
494 Start();
495 }
496
497 if (num_entries != num_) {
498 char msg[100];
499 std::snprintf(msg, sizeof(msg), "(%d ops)", num_entries);
500 message_ = msg;
501 }
502
503 char* err_msg = nullptr;
504 int status;
505
506 sqlite3_stmt *replace_stmt, *begin_trans_stmt, *end_trans_stmt;
507 std::string replace_str = "REPLACE INTO test (key, value) VALUES (?, ?)";
508 std::string begin_trans_str = "BEGIN TRANSACTION;";
509 std::string end_trans_str = "END TRANSACTION;";
510
511 // Check for synchronous flag in options
512 std::string sync_stmt =
513 (write_sync) ? "PRAGMA synchronous = FULL" : "PRAGMA synchronous = OFF";
514 status = sqlite3_exec(db_, sync_stmt.c_str(), nullptr, nullptr, &err_msg);
515 ExecErrorCheck(status, err_msg);
516
517 // Preparing sqlite3 statements
518 status = sqlite3_prepare_v2(db_, replace_str.c_str(), -1, &replace_stmt,
519 nullptr);
520 ErrorCheck(status);
521 status = sqlite3_prepare_v2(db_, begin_trans_str.c_str(), -1,
522 &begin_trans_stmt, nullptr);
523 ErrorCheck(status);
524 status = sqlite3_prepare_v2(db_, end_trans_str.c_str(), -1, &end_trans_stmt,
525 nullptr);
526 ErrorCheck(status);
527
528 bool transaction = (entries_per_batch > 1);
529 for (int i = 0; i < num_entries; i += entries_per_batch) {
530 // Begin write transaction
531 if (FLAGS_transaction && transaction) {
532 status = sqlite3_step(begin_trans_stmt);
533 StepErrorCheck(status);
534 status = sqlite3_reset(begin_trans_stmt);
535 ErrorCheck(status);
536 }
537
538 // Create and execute SQL statements
539 for (int j = 0; j < entries_per_batch; j++) {
540 const char* value = gen_.Generate(value_size).data();
541
542 // Create values for key-value pair
543 const int k =
544 (order == SEQUENTIAL) ? i + j : (rand_.Next() % num_entries);
545 char key[100];
546 std::snprintf(key, sizeof(key), "%016d", k);
547
548 // Bind KV values into replace_stmt
549 status = sqlite3_bind_blob(replace_stmt, 1, key, 16, SQLITE_STATIC);
550 ErrorCheck(status);
551 status = sqlite3_bind_blob(replace_stmt, 2, value, value_size,
552 SQLITE_STATIC);
553 ErrorCheck(status);
554
555 // Execute replace_stmt
556 bytes_ += value_size + strlen(key);
557 status = sqlite3_step(replace_stmt);
558 StepErrorCheck(status);
559
560 // Reset SQLite statement for another use
561 status = sqlite3_clear_bindings(replace_stmt);
562 ErrorCheck(status);
563 status = sqlite3_reset(replace_stmt);
564 ErrorCheck(status);
565
566 FinishedSingleOp();
567 }
568
569 // End write transaction
570 if (FLAGS_transaction && transaction) {
571 status = sqlite3_step(end_trans_stmt);
572 StepErrorCheck(status);
573 status = sqlite3_reset(end_trans_stmt);
574 ErrorCheck(status);
575 }
576 }
577
578 status = sqlite3_finalize(replace_stmt);
579 ErrorCheck(status);
580 status = sqlite3_finalize(begin_trans_stmt);
581 ErrorCheck(status);
582 status = sqlite3_finalize(end_trans_stmt);
583 ErrorCheck(status);
584 }
585
Read(Order order,int entries_per_batch)586 void Read(Order order, int entries_per_batch) {
587 int status;
588 sqlite3_stmt *read_stmt, *begin_trans_stmt, *end_trans_stmt;
589
590 std::string read_str = "SELECT * FROM test WHERE key = ?";
591 std::string begin_trans_str = "BEGIN TRANSACTION;";
592 std::string end_trans_str = "END TRANSACTION;";
593
594 // Preparing sqlite3 statements
595 status = sqlite3_prepare_v2(db_, begin_trans_str.c_str(), -1,
596 &begin_trans_stmt, nullptr);
597 ErrorCheck(status);
598 status = sqlite3_prepare_v2(db_, end_trans_str.c_str(), -1, &end_trans_stmt,
599 nullptr);
600 ErrorCheck(status);
601 status = sqlite3_prepare_v2(db_, read_str.c_str(), -1, &read_stmt, nullptr);
602 ErrorCheck(status);
603
604 bool transaction = (entries_per_batch > 1);
605 for (int i = 0; i < reads_; i += entries_per_batch) {
606 // Begin read transaction
607 if (FLAGS_transaction && transaction) {
608 status = sqlite3_step(begin_trans_stmt);
609 StepErrorCheck(status);
610 status = sqlite3_reset(begin_trans_stmt);
611 ErrorCheck(status);
612 }
613
614 // Create and execute SQL statements
615 for (int j = 0; j < entries_per_batch; j++) {
616 // Create key value
617 char key[100];
618 int k = (order == SEQUENTIAL) ? i + j : (rand_.Next() % reads_);
619 std::snprintf(key, sizeof(key), "%016d", k);
620
621 // Bind key value into read_stmt
622 status = sqlite3_bind_blob(read_stmt, 1, key, 16, SQLITE_STATIC);
623 ErrorCheck(status);
624
625 // Execute read statement
626 while ((status = sqlite3_step(read_stmt)) == SQLITE_ROW) {
627 }
628 StepErrorCheck(status);
629
630 // Reset SQLite statement for another use
631 status = sqlite3_clear_bindings(read_stmt);
632 ErrorCheck(status);
633 status = sqlite3_reset(read_stmt);
634 ErrorCheck(status);
635 FinishedSingleOp();
636 }
637
638 // End read transaction
639 if (FLAGS_transaction && transaction) {
640 status = sqlite3_step(end_trans_stmt);
641 StepErrorCheck(status);
642 status = sqlite3_reset(end_trans_stmt);
643 ErrorCheck(status);
644 }
645 }
646
647 status = sqlite3_finalize(read_stmt);
648 ErrorCheck(status);
649 status = sqlite3_finalize(begin_trans_stmt);
650 ErrorCheck(status);
651 status = sqlite3_finalize(end_trans_stmt);
652 ErrorCheck(status);
653 }
654
ReadSequential()655 void ReadSequential() {
656 int status;
657 sqlite3_stmt* pStmt;
658 std::string read_str = "SELECT * FROM test ORDER BY key";
659
660 status = sqlite3_prepare_v2(db_, read_str.c_str(), -1, &pStmt, nullptr);
661 ErrorCheck(status);
662 for (int i = 0; i < reads_ && SQLITE_ROW == sqlite3_step(pStmt); i++) {
663 bytes_ += sqlite3_column_bytes(pStmt, 1) + sqlite3_column_bytes(pStmt, 2);
664 FinishedSingleOp();
665 }
666
667 status = sqlite3_finalize(pStmt);
668 ErrorCheck(status);
669 }
670 };
671
672 } // namespace leveldb
673
main(int argc,char ** argv)674 int main(int argc, char** argv) {
675 std::string default_db_path;
676 for (int i = 1; i < argc; i++) {
677 double d;
678 int n;
679 char junk;
680 if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
681 FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
682 } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
683 (n == 0 || n == 1)) {
684 FLAGS_histogram = n;
685 } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
686 FLAGS_compression_ratio = d;
687 } else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&
688 (n == 0 || n == 1)) {
689 FLAGS_use_existing_db = n;
690 } else if (sscanf(argv[i], "--use_rowids=%d%c", &n, &junk) == 1 &&
691 (n == 0 || n == 1)) {
692 FLAGS_use_rowids = n;
693 } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
694 FLAGS_num = n;
695 } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {
696 FLAGS_reads = n;
697 } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
698 FLAGS_value_size = n;
699 } else if (leveldb::Slice(argv[i]) == leveldb::Slice("--no_transaction")) {
700 FLAGS_transaction = false;
701 } else if (sscanf(argv[i], "--page_size=%d%c", &n, &junk) == 1) {
702 FLAGS_page_size = n;
703 } else if (sscanf(argv[i], "--num_pages=%d%c", &n, &junk) == 1) {
704 FLAGS_num_pages = n;
705 } else if (sscanf(argv[i], "--WAL_enabled=%d%c", &n, &junk) == 1 &&
706 (n == 0 || n == 1)) {
707 FLAGS_WAL_enabled = n;
708 } else if (strncmp(argv[i], "--db=", 5) == 0) {
709 FLAGS_db = argv[i] + 5;
710 } else {
711 std::fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
712 std::exit(1);
713 }
714 }
715
716 // Choose a location for the test database if none given with --db=<path>
717 if (FLAGS_db == nullptr) {
718 leveldb::Env::Default()->GetTestDirectory(&default_db_path);
719 default_db_path += "/dbbench";
720 FLAGS_db = default_db_path.c_str();
721 }
722
723 leveldb::Benchmark benchmark;
724 benchmark.Run();
725 return 0;
726 }
727