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.
8 #include "util/histogram.h"
9 #include "util/random.h"
10 #include "util/testutil.h"
12 // Comma-separated list of operations to run in the specified order
15 // fillseq -- write N values in sequential key order in async mode
16 // fillseqsync -- write N/100 values in sequential key order in sync mode
17 // fillseqbatch -- batch write N values in sequential key order in async mode
18 // fillrandom -- write N values in random key order in async mode
19 // fillrandsync -- write N/100 values in random key order in sync mode
20 // fillrandbatch -- batch write N values in sequential key order in async mode
21 // overwrite -- overwrite N values in random key order in async mode
22 // fillrand100K -- write N/1000 100K values in random order in async mode
23 // fillseq100K -- write N/1000 100K values in sequential order in async mode
24 // readseq -- read N times sequentially
25 // readrandom -- read N times in random order
26 // readrand100K -- read N/1000 100K values in sequential order in async mode
27 static const char* FLAGS_benchmarks =
44 // Number of key/values to place in database
45 static int FLAGS_num = 1000000;
47 // Number of read operations to do. If negative, do FLAGS_num reads.
48 static int FLAGS_reads = -1;
51 static int FLAGS_value_size = 100;
53 // Print histogram of operation timings
54 static bool FLAGS_histogram = false;
56 // Arrange to generate values that shrink to this fraction of
57 // their original size after compression
58 static double FLAGS_compression_ratio = 0.5;
60 // Page size. Default 1 KB.
61 static int FLAGS_page_size = 1024;
64 // Default cache size = FLAGS_page_size * FLAGS_num_pages = 4 MB.
65 static int FLAGS_num_pages = 4096;
67 // If true, do not destroy the existing database. If you set this
68 // flag and also specify a benchmark that wants a fresh database, that
69 // benchmark will fail.
70 static bool FLAGS_use_existing_db = false;
72 // If true, we allow batch writes to occur
73 static bool FLAGS_transaction = true;
75 // If true, we enable Write-Ahead Logging
76 static bool FLAGS_WAL_enabled = true;
78 // Use the db with the following name.
79 static const char* FLAGS_db = NULL;
82 static void ExecErrorCheck(int status, char *err_msg) {
83 if (status != SQLITE_OK) {
84 fprintf(stderr, "SQL error: %s\n", err_msg);
85 sqlite3_free(err_msg);
91 static void StepErrorCheck(int status) {
92 if (status != SQLITE_DONE) {
93 fprintf(stderr, "SQL step error: status = %d\n", status);
99 static void ErrorCheck(int status) {
100 if (status != SQLITE_OK) {
101 fprintf(stderr, "sqlite3 error: status = %d\n", status);
107 static void WalCheckpoint(sqlite3* db_) {
108 // Flush all writes to disk
109 if (FLAGS_WAL_enabled) {
110 sqlite3_wal_checkpoint_v2(db_, NULL, SQLITE_CHECKPOINT_FULL, NULL, NULL);
116 // Helper for quickly generating random data.
118 class RandomGenerator {
125 // We use a limited amount of data over and over again and ensure
126 // that it is larger than the compression window (32KB), and also
127 // large enough to serve all typical value sizes we want to write.
130 while (data_.size() < 1048576) {
131 // Add a short fragment that is as compressible as specified
132 // by FLAGS_compression_ratio.
133 test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);
139 Slice Generate(int len) {
140 if (pos_ + len > data_.size()) {
142 assert(len < data_.size());
145 return Slice(data_.data() + pos_ - len, len);
149 static Slice TrimSpace(Slice s) {
151 while (start < s.size() && isspace(s[start])) {
154 int limit = s.size();
155 while (limit > start && isspace(s[limit-1])) {
158 return Slice(s.data() + start, limit - start);
170 double last_op_finish_;
172 std::string message_;
174 RandomGenerator gen_;
177 // State kept for progress messages
179 int next_report_; // When to report next
182 const int kKeySize = 16;
184 fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
185 fprintf(stdout, "Values: %d bytes each\n", FLAGS_value_size);
186 fprintf(stdout, "Entries: %d\n", num_);
187 fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
188 ((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_)
191 fprintf(stdout, "------------------------------------------------\n");
194 void PrintWarnings() {
195 #if defined(__GNUC__) && !defined(__OPTIMIZE__)
197 "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n"
202 "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
206 void PrintEnvironment() {
207 fprintf(stderr, "SQLite: version %s\n", SQLITE_VERSION);
210 time_t now = time(NULL);
211 fprintf(stderr, "Date: %s", ctime(&now)); // ctime() adds newline
213 FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
214 if (cpuinfo != NULL) {
217 std::string cpu_type;
218 std::string cache_size;
219 while (fgets(line, sizeof(line), cpuinfo) != NULL) {
220 const char* sep = strchr(line, ':');
224 Slice key = TrimSpace(Slice(line, sep - 1 - line));
225 Slice val = TrimSpace(Slice(sep + 1));
226 if (key == "model name") {
228 cpu_type = val.ToString();
229 } else if (key == "cache size") {
230 cache_size = val.ToString();
234 fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
235 fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
241 start_ = Env::Default()->NowMicros() * 1e-6;
244 last_op_finish_ = start_;
250 void FinishedSingleOp() {
251 if (FLAGS_histogram) {
252 double now = Env::Default()->NowMicros() * 1e-6;
253 double micros = (now - last_op_finish_) * 1e6;
255 if (micros > 20000) {
256 fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
259 last_op_finish_ = now;
263 if (done_ >= next_report_) {
264 if (next_report_ < 1000) next_report_ += 100;
265 else if (next_report_ < 5000) next_report_ += 500;
266 else if (next_report_ < 10000) next_report_ += 1000;
267 else if (next_report_ < 50000) next_report_ += 5000;
268 else if (next_report_ < 100000) next_report_ += 10000;
269 else if (next_report_ < 500000) next_report_ += 50000;
270 else next_report_ += 100000;
271 fprintf(stderr, "... finished %d ops%30s\r", done_, "");
276 void Stop(const Slice& name) {
277 double finish = Env::Default()->NowMicros() * 1e-6;
279 // Pretend at least one op was done in case we are running a benchmark
280 // that does not call FinishedSingleOp().
281 if (done_ < 1) done_ = 1;
285 snprintf(rate, sizeof(rate), "%6.1f MB/s",
286 (bytes_ / 1048576.0) / (finish - start_));
287 if (!message_.empty()) {
288 message_ = std::string(rate) + " " + message_;
294 fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
295 name.ToString().c_str(),
296 (finish - start_) * 1e6 / done_,
297 (message_.empty() ? "" : " "),
299 if (FLAGS_histogram) {
300 fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
319 reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
322 std::vector<std::string> files;
323 std::string test_dir;
324 Env::Default()->GetTestDirectory(&test_dir);
325 Env::Default()->GetChildren(test_dir, &files);
326 if (!FLAGS_use_existing_db) {
327 for (int i = 0; i < files.size(); i++) {
328 if (Slice(files[i]).starts_with("dbbench_sqlite3")) {
329 std::string file_name(test_dir);
331 file_name += files[i];
332 Env::Default()->DeleteFile(file_name.c_str());
339 int status = sqlite3_close(db_);
347 const char* benchmarks = FLAGS_benchmarks;
348 while (benchmarks != NULL) {
349 const char* sep = strchr(benchmarks, ',');
355 name = Slice(benchmarks, sep - benchmarks);
356 benchmarks = sep + 1;
363 bool write_sync = false;
364 if (name == Slice("fillseq")) {
365 Write(write_sync, SEQUENTIAL, FRESH, num_, FLAGS_value_size, 1);
367 } else if (name == Slice("fillseqbatch")) {
368 Write(write_sync, SEQUENTIAL, FRESH, num_, FLAGS_value_size, 1000);
370 } else if (name == Slice("fillrandom")) {
371 Write(write_sync, RANDOM, FRESH, num_, FLAGS_value_size, 1);
373 } else if (name == Slice("fillrandbatch")) {
374 Write(write_sync, RANDOM, FRESH, num_, FLAGS_value_size, 1000);
376 } else if (name == Slice("overwrite")) {
377 Write(write_sync, RANDOM, EXISTING, num_, FLAGS_value_size, 1);
379 } else if (name == Slice("overwritebatch")) {
380 Write(write_sync, RANDOM, EXISTING, num_, FLAGS_value_size, 1000);
382 } else if (name == Slice("fillrandsync")) {
384 Write(write_sync, RANDOM, FRESH, num_ / 100, FLAGS_value_size, 1);
386 } else if (name == Slice("fillseqsync")) {
388 Write(write_sync, SEQUENTIAL, FRESH, num_ / 100, FLAGS_value_size, 1);
390 } else if (name == Slice("fillrand100K")) {
391 Write(write_sync, RANDOM, FRESH, num_ / 1000, 100 * 1000, 1);
393 } else if (name == Slice("fillseq100K")) {
394 Write(write_sync, SEQUENTIAL, FRESH, num_ / 1000, 100 * 1000, 1);
396 } else if (name == Slice("readseq")) {
398 } else if (name == Slice("readrandom")) {
400 } else if (name == Slice("readrand100K")) {
407 if (name != Slice()) { // No error message for empty name
408 fprintf(stderr, "unknown benchmark '%s'\n", name.ToString().c_str());
422 char* err_msg = NULL;
427 Env::Default()->GetTestDirectory(&tmp_dir);
428 snprintf(file_name, sizeof(file_name),
429 "%s/dbbench_sqlite3-%d.db",
432 status = sqlite3_open(file_name, &db_);
434 fprintf(stderr, "open error: %s\n", sqlite3_errmsg(db_));
438 // Change SQLite cache size
439 char cache_size[100];
440 snprintf(cache_size, sizeof(cache_size), "PRAGMA cache_size = %d",
442 status = sqlite3_exec(db_, cache_size, NULL, NULL, &err_msg);
443 ExecErrorCheck(status, err_msg);
445 // FLAGS_page_size is defaulted to 1024
446 if (FLAGS_page_size != 1024) {
448 snprintf(page_size, sizeof(page_size), "PRAGMA page_size = %d",
450 status = sqlite3_exec(db_, page_size, NULL, NULL, &err_msg);
451 ExecErrorCheck(status, err_msg);
454 // Change journal mode to WAL if WAL enabled flag is on
455 if (FLAGS_WAL_enabled) {
456 std::string WAL_stmt = "PRAGMA journal_mode = WAL";
458 // LevelDB's default cache size is a combined 4 MB
459 std::string WAL_checkpoint = "PRAGMA wal_autocheckpoint = 4096";
460 status = sqlite3_exec(db_, WAL_stmt.c_str(), NULL, NULL, &err_msg);
461 ExecErrorCheck(status, err_msg);
462 status = sqlite3_exec(db_, WAL_checkpoint.c_str(), NULL, NULL, &err_msg);
463 ExecErrorCheck(status, err_msg);
466 // Change locking mode to exclusive and create tables/index for database
467 std::string locking_stmt = "PRAGMA locking_mode = EXCLUSIVE";
468 std::string create_stmt =
469 "CREATE TABLE test (key blob, value blob, PRIMARY KEY(key))";
470 std::string stmt_array[] = { locking_stmt, create_stmt };
471 int stmt_array_length = sizeof(stmt_array) / sizeof(std::string);
472 for (int i = 0; i < stmt_array_length; i++) {
473 status = sqlite3_exec(db_, stmt_array[i].c_str(), NULL, NULL, &err_msg);
474 ExecErrorCheck(status, err_msg);
478 void Write(bool write_sync, Order order, DBState state,
479 int num_entries, int value_size, int entries_per_batch) {
480 // Create new database if state == FRESH
481 if (state == FRESH) {
482 if (FLAGS_use_existing_db) {
483 message_ = "skipping (--use_existing_db is true)";
492 if (num_entries != num_) {
494 snprintf(msg, sizeof(msg), "(%d ops)", num_entries);
498 char* err_msg = NULL;
501 sqlite3_stmt *replace_stmt, *begin_trans_stmt, *end_trans_stmt;
502 std::string replace_str = "REPLACE INTO test (key, value) VALUES (?, ?)";
503 std::string begin_trans_str = "BEGIN TRANSACTION;";
504 std::string end_trans_str = "END TRANSACTION;";
506 // Check for synchronous flag in options
507 std::string sync_stmt = (write_sync) ? "PRAGMA synchronous = FULL" :
508 "PRAGMA synchronous = OFF";
509 status = sqlite3_exec(db_, sync_stmt.c_str(), NULL, NULL, &err_msg);
510 ExecErrorCheck(status, err_msg);
512 // Preparing sqlite3 statements
513 status = sqlite3_prepare_v2(db_, replace_str.c_str(), -1,
514 &replace_stmt, NULL);
516 status = sqlite3_prepare_v2(db_, begin_trans_str.c_str(), -1,
517 &begin_trans_stmt, NULL);
519 status = sqlite3_prepare_v2(db_, end_trans_str.c_str(), -1,
520 &end_trans_stmt, NULL);
523 bool transaction = (entries_per_batch > 1);
524 for (int i = 0; i < num_entries; i += entries_per_batch) {
525 // Begin write transaction
526 if (FLAGS_transaction && transaction) {
527 status = sqlite3_step(begin_trans_stmt);
528 StepErrorCheck(status);
529 status = sqlite3_reset(begin_trans_stmt);
533 // Create and execute SQL statements
534 for (int j = 0; j < entries_per_batch; j++) {
535 const char* value = gen_.Generate(value_size).data();
537 // Create values for key-value pair
538 const int k = (order == SEQUENTIAL) ? i + j :
539 (rand_.Next() % num_entries);
541 snprintf(key, sizeof(key), "%016d", k);
543 // Bind KV values into replace_stmt
544 status = sqlite3_bind_blob(replace_stmt, 1, key, 16, SQLITE_STATIC);
546 status = sqlite3_bind_blob(replace_stmt, 2, value,
547 value_size, SQLITE_STATIC);
550 // Execute replace_stmt
551 bytes_ += value_size + strlen(key);
552 status = sqlite3_step(replace_stmt);
553 StepErrorCheck(status);
555 // Reset SQLite statement for another use
556 status = sqlite3_clear_bindings(replace_stmt);
558 status = sqlite3_reset(replace_stmt);
564 // End write transaction
565 if (FLAGS_transaction && transaction) {
566 status = sqlite3_step(end_trans_stmt);
567 StepErrorCheck(status);
568 status = sqlite3_reset(end_trans_stmt);
573 status = sqlite3_finalize(replace_stmt);
575 status = sqlite3_finalize(begin_trans_stmt);
577 status = sqlite3_finalize(end_trans_stmt);
581 void Read(Order order, int entries_per_batch) {
583 sqlite3_stmt *read_stmt, *begin_trans_stmt, *end_trans_stmt;
585 std::string read_str = "SELECT * FROM test WHERE key = ?";
586 std::string begin_trans_str = "BEGIN TRANSACTION;";
587 std::string end_trans_str = "END TRANSACTION;";
589 // Preparing sqlite3 statements
590 status = sqlite3_prepare_v2(db_, begin_trans_str.c_str(), -1,
591 &begin_trans_stmt, NULL);
593 status = sqlite3_prepare_v2(db_, end_trans_str.c_str(), -1,
594 &end_trans_stmt, NULL);
596 status = sqlite3_prepare_v2(db_, read_str.c_str(), -1, &read_stmt, NULL);
599 bool transaction = (entries_per_batch > 1);
600 for (int i = 0; i < reads_; i += entries_per_batch) {
601 // Begin read transaction
602 if (FLAGS_transaction && transaction) {
603 status = sqlite3_step(begin_trans_stmt);
604 StepErrorCheck(status);
605 status = sqlite3_reset(begin_trans_stmt);
609 // Create and execute SQL statements
610 for (int j = 0; j < entries_per_batch; j++) {
613 int k = (order == SEQUENTIAL) ? i + j : (rand_.Next() % reads_);
614 snprintf(key, sizeof(key), "%016d", k);
616 // Bind key value into read_stmt
617 status = sqlite3_bind_blob(read_stmt, 1, key, 16, SQLITE_STATIC);
620 // Execute read statement
621 while ((status = sqlite3_step(read_stmt)) == SQLITE_ROW) {}
622 StepErrorCheck(status);
624 // Reset SQLite statement for another use
625 status = sqlite3_clear_bindings(read_stmt);
627 status = sqlite3_reset(read_stmt);
632 // End read transaction
633 if (FLAGS_transaction && transaction) {
634 status = sqlite3_step(end_trans_stmt);
635 StepErrorCheck(status);
636 status = sqlite3_reset(end_trans_stmt);
641 status = sqlite3_finalize(read_stmt);
643 status = sqlite3_finalize(begin_trans_stmt);
645 status = sqlite3_finalize(end_trans_stmt);
649 void ReadSequential() {
652 std::string read_str = "SELECT * FROM test ORDER BY key";
654 status = sqlite3_prepare_v2(db_, read_str.c_str(), -1, &pStmt, NULL);
656 for (int i = 0; i < reads_ && SQLITE_ROW == sqlite3_step(pStmt); i++) {
657 bytes_ += sqlite3_column_bytes(pStmt, 1) + sqlite3_column_bytes(pStmt, 2);
661 status = sqlite3_finalize(pStmt);
667 } // namespace leveldb
669 int main(int argc, char** argv) {
670 std::string default_db_path;
671 for (int i = 1; i < argc; i++) {
675 if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
676 FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
677 } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
678 (n == 0 || n == 1)) {
680 } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
681 FLAGS_compression_ratio = d;
682 } else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&
683 (n == 0 || n == 1)) {
684 FLAGS_use_existing_db = n;
685 } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
687 } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {
689 } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
690 FLAGS_value_size = n;
691 } else if (leveldb::Slice(argv[i]) == leveldb::Slice("--no_transaction")) {
692 FLAGS_transaction = false;
693 } else if (sscanf(argv[i], "--page_size=%d%c", &n, &junk) == 1) {
695 } else if (sscanf(argv[i], "--num_pages=%d%c", &n, &junk) == 1) {
697 } else if (sscanf(argv[i], "--WAL_enabled=%d%c", &n, &junk) == 1 &&
698 (n == 0 || n == 1)) {
699 FLAGS_WAL_enabled = n;
700 } else if (strncmp(argv[i], "--db=", 5) == 0) {
701 FLAGS_db = argv[i] + 5;
703 fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
708 // Choose a location for the test database if none given with --db=<path>
709 if (FLAGS_db == NULL) {
710 leveldb::Env::Default()->GetTestDirectory(&default_db_path);
711 default_db_path += "/dbbench";
712 FLAGS_db = default_db_path.c_str();
715 leveldb::Benchmark benchmark;