27 static const char* FLAGS_benchmarks =
45 static int FLAGS_num = 1000000;
48 static int FLAGS_reads = -1;
51 static int FLAGS_value_size = 100;
54 static bool FLAGS_histogram =
false;
58 static double FLAGS_compression_ratio = 0.5;
61 static int FLAGS_page_size = 1024;
65 static int FLAGS_num_pages = 4096;
70 static bool FLAGS_use_existing_db =
false;
73 static bool FLAGS_transaction =
true;
76 static bool FLAGS_WAL_enabled =
true;
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_) {
109 if (FLAGS_WAL_enabled) {
110 sqlite3_wal_checkpoint_v2(db_, NULL, SQLITE_CHECKPOINT_FULL, NULL, NULL);
118 class RandomGenerator {
130 while (data_.size() < 1048576) {
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);
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");
195 #if defined(__GNUC__) && !defined(__OPTIMIZE__)
197 "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n"
202 "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
207 fprintf(stderr,
"SQLite: version %s\n", SQLITE_VERSION);
210 time_t now = time(NULL);
211 fprintf(stderr,
"Date: %s", ctime(&now));
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));
226 if (key ==
"model name") {
229 }
else if (key ==
"cache size") {
234 fprintf(stderr,
"CPU: %d * %s\n", num_cpus, cpu_type.c_str());
235 fprintf(stderr,
"CPUCache: %s\n", cache_size.c_str());
251 if (FLAGS_histogram) {
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_,
"");
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",
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;
326 if (!FLAGS_use_existing_db) {
327 for (
int i = 0; i < files.size(); i++) {
329 std::string file_name(test_dir);
331 file_name += files[i];
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")) {
367 }
else if (name ==
Slice(
"fillseqbatch")) {
370 }
else if (name ==
Slice(
"fillrandom")) {
373 }
else if (name ==
Slice(
"fillrandbatch")) {
376 }
else if (name ==
Slice(
"overwrite")) {
379 }
else if (name ==
Slice(
"overwritebatch")) {
382 }
else if (name ==
Slice(
"fillrandsync")) {
386 }
else if (name ==
Slice(
"fillseqsync")) {
390 }
else if (name ==
Slice(
"fillrand100K")) {
393 }
else if (name ==
Slice(
"fillseq100K")) {
396 }
else if (name ==
Slice(
"readseq")) {
398 }
else if (name ==
Slice(
"readrandom")) {
400 }
else if (name ==
Slice(
"readrand100K")) {
407 if (name !=
Slice()) {
408 fprintf(stderr,
"unknown benchmark '%s'\n", name.
ToString().c_str());
422 char* err_msg = NULL;
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_));
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);
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);
455 if (FLAGS_WAL_enabled) {
456 std::string WAL_stmt =
"PRAGMA journal_mode = WAL";
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);
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);
479 int num_entries,
int value_size,
int entries_per_batch) {
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;";
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);
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) {
526 if (FLAGS_transaction && transaction) {
527 status = sqlite3_step(begin_trans_stmt);
528 StepErrorCheck(status);
529 status = sqlite3_reset(begin_trans_stmt);
534 for (
int j = 0; j < entries_per_batch; j++) {
535 const char*
value = gen_.Generate(value_size).data();
539 (rand_.
Next() % num_entries);
541 snprintf(key,
sizeof(key),
"%016d", k);
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);
551 bytes_ += value_size + strlen(key);
552 status = sqlite3_step(replace_stmt);
553 StepErrorCheck(status);
556 status = sqlite3_clear_bindings(replace_stmt);
558 status = sqlite3_reset(replace_stmt);
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);
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;";
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) {
602 if (FLAGS_transaction && transaction) {
603 status = sqlite3_step(begin_trans_stmt);
604 StepErrorCheck(status);
605 status = sqlite3_reset(begin_trans_stmt);
610 for (
int j = 0; j < entries_per_batch; j++) {
614 snprintf(key,
sizeof(key),
"%016d", k);
617 status = sqlite3_bind_blob(read_stmt, 1, key, 16, SQLITE_STATIC);
621 while ((status = sqlite3_step(read_stmt)) == SQLITE_ROW) {}
622 StepErrorCheck(status);
625 status = sqlite3_clear_bindings(read_stmt);
627 status = sqlite3_reset(read_stmt);
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);
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);
669 int main(
int argc,
char** argv) {
670 std::string default_db_path;
671 for (
int i = 1; i < argc; i++) {
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;
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]);
709 if (FLAGS_db == NULL) {
711 default_db_path +=
"/dbbench";
712 FLAGS_db = default_db_path.c_str();
virtual Status DeleteFile(const std::string &fname)=0
int main(int argc, char **argv)
bool starts_with(const Slice &x) const
Slice CompressibleString(Random *rnd, double compressed_fraction, int len, std::string *dst)
virtual Status GetChildren(const std::string &dir, std::vector< std::string > *result)=0
std::string ToString() const
virtual Status GetTestDirectory(std::string *path)=0
void Write(bool write_sync, Order order, DBState state, int num_entries, int value_size, int entries_per_batch)
void Stop(const Slice &name)
void Read(Order order, int entries_per_batch)
std::string ToString() const
virtual uint64_t NowMicros()=0