作者: 谢瑞阳 10225101483 徐翔宇 10225101535

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