作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

721 lines
22 KiB

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