作者: 韩晨旭 10225101440 李畅 10225102463
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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