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.

877 line
25 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 <sys/types.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "db/db_impl.h"
  8. #include "db/version_set.h"
  9. #include "leveldb/cache.h"
  10. #include "leveldb/db.h"
  11. #include "leveldb/env.h"
  12. #include "leveldb/write_batch.h"
  13. #include "port/port.h"
  14. #include "util/crc32c.h"
  15. #include "util/histogram.h"
  16. #include "util/mutexlock.h"
  17. #include "util/random.h"
  18. #include "util/testutil.h"
  19. // Comma-separated list of operations to run in the specified order
  20. // Actual benchmarks:
  21. // fillseq -- write N values in sequential key order in async mode
  22. // fillrandom -- write N values in random key order in async mode
  23. // overwrite -- overwrite N values in random key order in async mode
  24. // fillsync -- write N/100 values in random key order in sync mode
  25. // fill100K -- write N/1000 100K values in random order in async mode
  26. // readseq -- read N times sequentially
  27. // readreverse -- read N times in reverse order
  28. // readrandom -- read N times in random order
  29. // readhot -- read N times in random order from 1% section of DB
  30. // crc32c -- repeated crc32c of 4K of data
  31. // acquireload -- load N*1000 times
  32. // Meta operations:
  33. // compact -- Compact the entire DB
  34. // stats -- Print DB stats
  35. // heapprofile -- Dump a heap profile (if supported by this port)
  36. static const char* FLAGS_benchmarks =
  37. "fillseq,"
  38. "fillsync,"
  39. "fillrandom,"
  40. "overwrite,"
  41. "readrandom,"
  42. "readrandom," // Extra run to allow previous compactions to quiesce
  43. "readseq,"
  44. "readreverse,"
  45. "compact,"
  46. "readrandom,"
  47. "readseq,"
  48. "readreverse,"
  49. "fill100K,"
  50. "crc32c,"
  51. "snappycomp,"
  52. "snappyuncomp,"
  53. "acquireload,"
  54. ;
  55. // Number of key/values to place in database
  56. static int FLAGS_num = 1000000;
  57. // Number of read operations to do. If negative, do FLAGS_num reads.
  58. static int FLAGS_reads = -1;
  59. // Number of concurrent threads to run.
  60. static int FLAGS_threads = 1;
  61. // Size of each value
  62. static int FLAGS_value_size = 100;
  63. // Arrange to generate values that shrink to this fraction of
  64. // their original size after compression
  65. static double FLAGS_compression_ratio = 0.5;
  66. // Print histogram of operation timings
  67. static bool FLAGS_histogram = false;
  68. // Number of bytes to buffer in memtable before compacting
  69. // (initialized to default value by "main")
  70. static int FLAGS_write_buffer_size = 0;
  71. // Number of bytes to use as a cache of uncompressed data.
  72. // Negative means use default settings.
  73. static int FLAGS_cache_size = -1;
  74. // Maximum number of files to keep open at the same time (use default if == 0)
  75. static int FLAGS_open_files = 0;
  76. // If true, do not destroy the existing database. If you set this
  77. // flag and also specify a benchmark that wants a fresh database, that
  78. // benchmark will fail.
  79. static bool FLAGS_use_existing_db = false;
  80. // Use the db with the following name.
  81. static const char* FLAGS_db = "/tmp/dbbench";
  82. namespace leveldb {
  83. namespace {
  84. // Helper for quickly generating random data.
  85. class RandomGenerator {
  86. private:
  87. std::string data_;
  88. int pos_;
  89. public:
  90. RandomGenerator() {
  91. // We use a limited amount of data over and over again and ensure
  92. // that it is larger than the compression window (32KB), and also
  93. // large enough to serve all typical value sizes we want to write.
  94. Random rnd(301);
  95. std::string piece;
  96. while (data_.size() < 1048576) {
  97. // Add a short fragment that is as compressible as specified
  98. // by FLAGS_compression_ratio.
  99. test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);
  100. data_.append(piece);
  101. }
  102. pos_ = 0;
  103. }
  104. Slice Generate(int len) {
  105. if (pos_ + len > data_.size()) {
  106. pos_ = 0;
  107. assert(len < data_.size());
  108. }
  109. pos_ += len;
  110. return Slice(data_.data() + pos_ - len, len);
  111. }
  112. };
  113. static Slice TrimSpace(Slice s) {
  114. int start = 0;
  115. while (start < s.size() && isspace(s[start])) {
  116. start++;
  117. }
  118. int limit = s.size();
  119. while (limit > start && isspace(s[limit-1])) {
  120. limit--;
  121. }
  122. return Slice(s.data() + start, limit - start);
  123. }
  124. static void AppendWithSpace(std::string* str, Slice msg) {
  125. if (msg.empty()) return;
  126. if (!str->empty()) {
  127. str->push_back(' ');
  128. }
  129. str->append(msg.data(), msg.size());
  130. }
  131. class Stats {
  132. private:
  133. double start_;
  134. double finish_;
  135. double seconds_;
  136. int done_;
  137. int next_report_;
  138. int64_t bytes_;
  139. double last_op_finish_;
  140. Histogram hist_;
  141. std::string message_;
  142. public:
  143. Stats() { Start(); }
  144. void Start() {
  145. next_report_ = 100;
  146. last_op_finish_ = start_;
  147. hist_.Clear();
  148. done_ = 0;
  149. bytes_ = 0;
  150. seconds_ = 0;
  151. start_ = Env::Default()->NowMicros();
  152. finish_ = start_;
  153. message_.clear();
  154. }
  155. void Merge(const Stats& other) {
  156. hist_.Merge(other.hist_);
  157. done_ += other.done_;
  158. bytes_ += other.bytes_;
  159. seconds_ += other.seconds_;
  160. if (other.start_ < start_) start_ = other.start_;
  161. if (other.finish_ > finish_) finish_ = other.finish_;
  162. // Just keep the messages from one thread
  163. if (message_.empty()) message_ = other.message_;
  164. }
  165. void Stop() {
  166. finish_ = Env::Default()->NowMicros();
  167. seconds_ = (finish_ - start_) * 1e-6;
  168. }
  169. void AddMessage(Slice msg) {
  170. AppendWithSpace(&message_, msg);
  171. }
  172. void FinishedSingleOp() {
  173. if (FLAGS_histogram) {
  174. double now = Env::Default()->NowMicros();
  175. double micros = now - last_op_finish_;
  176. hist_.Add(micros);
  177. if (micros > 20000) {
  178. fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
  179. fflush(stderr);
  180. }
  181. last_op_finish_ = now;
  182. }
  183. done_++;
  184. if (done_ >= next_report_) {
  185. if (next_report_ < 1000) next_report_ += 100;
  186. else if (next_report_ < 5000) next_report_ += 500;
  187. else if (next_report_ < 10000) next_report_ += 1000;
  188. else if (next_report_ < 50000) next_report_ += 5000;
  189. else if (next_report_ < 100000) next_report_ += 10000;
  190. else if (next_report_ < 500000) next_report_ += 50000;
  191. else next_report_ += 100000;
  192. fprintf(stderr, "... finished %d ops%30s\r", done_, "");
  193. fflush(stderr);
  194. }
  195. }
  196. void AddBytes(int64_t n) {
  197. bytes_ += n;
  198. }
  199. void Report(const Slice& name) {
  200. // Pretend at least one op was done in case we are running a benchmark
  201. // that does not call FinishedSingleOp().
  202. if (done_ < 1) done_ = 1;
  203. std::string extra;
  204. if (bytes_ > 0) {
  205. // Rate is computed on actual elapsed time, not the sum of per-thread
  206. // elapsed times.
  207. double elapsed = (finish_ - start_) * 1e-6;
  208. char rate[100];
  209. snprintf(rate, sizeof(rate), "%6.1f MB/s",
  210. (bytes_ / 1048576.0) / elapsed);
  211. extra = rate;
  212. }
  213. AppendWithSpace(&extra, message_);
  214. fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
  215. name.ToString().c_str(),
  216. seconds_ * 1e6 / done_,
  217. (extra.empty() ? "" : " "),
  218. extra.c_str());
  219. if (FLAGS_histogram) {
  220. fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
  221. }
  222. fflush(stdout);
  223. }
  224. };
  225. // State shared by all concurrent executions of the same benchmark.
  226. struct SharedState {
  227. port::Mutex mu;
  228. port::CondVar cv;
  229. int total;
  230. // Each thread goes through the following states:
  231. // (1) initializing
  232. // (2) waiting for others to be initialized
  233. // (3) running
  234. // (4) done
  235. int num_initialized;
  236. int num_done;
  237. bool start;
  238. SharedState() : cv(&mu) { }
  239. };
  240. // Per-thread state for concurrent executions of the same benchmark.
  241. struct ThreadState {
  242. int tid; // 0..n-1 when running in n threads
  243. Random rand; // Has different seeds for different threads
  244. Stats stats;
  245. SharedState* shared;
  246. ThreadState(int index)
  247. : tid(index),
  248. rand(1000 + index) {
  249. }
  250. };
  251. } // namespace
  252. class Benchmark {
  253. private:
  254. Cache* cache_;
  255. DB* db_;
  256. int num_;
  257. int value_size_;
  258. int entries_per_batch_;
  259. WriteOptions write_options_;
  260. int reads_;
  261. int heap_counter_;
  262. void PrintHeader() {
  263. const int kKeySize = 16;
  264. PrintEnvironment();
  265. fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
  266. fprintf(stdout, "Values: %d bytes each (%d bytes after compression)\n",
  267. FLAGS_value_size,
  268. static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
  269. fprintf(stdout, "Entries: %d\n", num_);
  270. fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
  271. ((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_)
  272. / 1048576.0));
  273. fprintf(stdout, "FileSize: %.1f MB (estimated)\n",
  274. (((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_)
  275. / 1048576.0));
  276. PrintWarnings();
  277. fprintf(stdout, "------------------------------------------------\n");
  278. }
  279. void PrintWarnings() {
  280. #if defined(__GNUC__) && !defined(__OPTIMIZE__)
  281. fprintf(stdout,
  282. "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n"
  283. );
  284. #endif
  285. #ifndef NDEBUG
  286. fprintf(stdout,
  287. "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
  288. #endif
  289. // See if snappy is working by attempting to compress a compressible string
  290. const char text[] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
  291. std::string compressed;
  292. if (!port::Snappy_Compress(text, sizeof(text), &compressed)) {
  293. fprintf(stdout, "WARNING: Snappy compression is not enabled\n");
  294. } else if (compressed.size() >= sizeof(text)) {
  295. fprintf(stdout, "WARNING: Snappy compression is not effective\n");
  296. }
  297. }
  298. void PrintEnvironment() {
  299. fprintf(stderr, "LevelDB: version %d.%d\n",
  300. kMajorVersion, kMinorVersion);
  301. #if defined(__linux)
  302. time_t now = time(NULL);
  303. fprintf(stderr, "Date: %s", ctime(&now)); // ctime() adds newline
  304. FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
  305. if (cpuinfo != NULL) {
  306. char line[1000];
  307. int num_cpus = 0;
  308. std::string cpu_type;
  309. std::string cache_size;
  310. while (fgets(line, sizeof(line), cpuinfo) != NULL) {
  311. const char* sep = strchr(line, ':');
  312. if (sep == NULL) {
  313. continue;
  314. }
  315. Slice key = TrimSpace(Slice(line, sep - 1 - line));
  316. Slice val = TrimSpace(Slice(sep + 1));
  317. if (key == "model name") {
  318. ++num_cpus;
  319. cpu_type = val.ToString();
  320. } else if (key == "cache size") {
  321. cache_size = val.ToString();
  322. }
  323. }
  324. fclose(cpuinfo);
  325. fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
  326. fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
  327. }
  328. #endif
  329. }
  330. public:
  331. Benchmark()
  332. : cache_(FLAGS_cache_size >= 0 ? NewLRUCache(FLAGS_cache_size) : NULL),
  333. db_(NULL),
  334. num_(FLAGS_num),
  335. value_size_(FLAGS_value_size),
  336. entries_per_batch_(1),
  337. reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
  338. heap_counter_(0) {
  339. std::vector<std::string> files;
  340. Env::Default()->GetChildren(FLAGS_db, &files);
  341. for (int i = 0; i < files.size(); i++) {
  342. if (Slice(files[i]).starts_with("heap-")) {
  343. Env::Default()->DeleteFile(std::string(FLAGS_db) + "/" + files[i]);
  344. }
  345. }
  346. if (!FLAGS_use_existing_db) {
  347. DestroyDB(FLAGS_db, Options());
  348. }
  349. }
  350. ~Benchmark() {
  351. delete db_;
  352. delete cache_;
  353. }
  354. void Run() {
  355. PrintHeader();
  356. Open();
  357. const char* benchmarks = FLAGS_benchmarks;
  358. while (benchmarks != NULL) {
  359. const char* sep = strchr(benchmarks, ',');
  360. Slice name;
  361. if (sep == NULL) {
  362. name = benchmarks;
  363. benchmarks = NULL;
  364. } else {
  365. name = Slice(benchmarks, sep - benchmarks);
  366. benchmarks = sep + 1;
  367. }
  368. // Reset parameters that may be overriddden bwlow
  369. num_ = FLAGS_num;
  370. reads_ = (FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads);
  371. value_size_ = FLAGS_value_size;
  372. entries_per_batch_ = 1;
  373. write_options_ = WriteOptions();
  374. void (Benchmark::*method)(ThreadState*) = NULL;
  375. bool fresh_db = false;
  376. int num_threads = FLAGS_threads;
  377. if (name == Slice("fillseq")) {
  378. fresh_db = true;
  379. method = &Benchmark::WriteSeq;
  380. } else if (name == Slice("fillbatch")) {
  381. fresh_db = true;
  382. entries_per_batch_ = 1000;
  383. method = &Benchmark::WriteSeq;
  384. } else if (name == Slice("fillrandom")) {
  385. fresh_db = true;
  386. method = &Benchmark::WriteRandom;
  387. } else if (name == Slice("overwrite")) {
  388. fresh_db = false;
  389. method = &Benchmark::WriteRandom;
  390. } else if (name == Slice("fillsync")) {
  391. fresh_db = true;
  392. num_ /= 1000;
  393. write_options_.sync = true;
  394. method = &Benchmark::WriteRandom;
  395. } else if (name == Slice("fill100K")) {
  396. fresh_db = true;
  397. num_ /= 1000;
  398. value_size_ = 100 * 1000;
  399. method = &Benchmark::WriteRandom;
  400. } else if (name == Slice("readseq")) {
  401. method = &Benchmark::ReadSequential;
  402. } else if (name == Slice("readreverse")) {
  403. method = &Benchmark::ReadReverse;
  404. } else if (name == Slice("readrandom")) {
  405. method = &Benchmark::ReadRandom;
  406. } else if (name == Slice("readhot")) {
  407. method = &Benchmark::ReadHot;
  408. } else if (name == Slice("readrandomsmall")) {
  409. reads_ /= 1000;
  410. method = &Benchmark::ReadRandom;
  411. } else if (name == Slice("readwhilewriting")) {
  412. num_threads++; // Add extra thread for writing
  413. method = &Benchmark::ReadWhileWriting;
  414. } else if (name == Slice("compact")) {
  415. method = &Benchmark::Compact;
  416. } else if (name == Slice("crc32c")) {
  417. method = &Benchmark::Crc32c;
  418. } else if (name == Slice("acquireload")) {
  419. method = &Benchmark::AcquireLoad;
  420. } else if (name == Slice("snappycomp")) {
  421. method = &Benchmark::SnappyCompress;
  422. } else if (name == Slice("snappyuncomp")) {
  423. method = &Benchmark::SnappyUncompress;
  424. } else if (name == Slice("heapprofile")) {
  425. HeapProfile();
  426. } else if (name == Slice("stats")) {
  427. PrintStats();
  428. } else {
  429. if (name != Slice()) { // No error message for empty name
  430. fprintf(stderr, "unknown benchmark '%s'\n", name.ToString().c_str());
  431. }
  432. }
  433. if (fresh_db) {
  434. if (FLAGS_use_existing_db) {
  435. fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n",
  436. name.ToString().c_str());
  437. method = NULL;
  438. } else {
  439. delete db_;
  440. db_ = NULL;
  441. DestroyDB(FLAGS_db, Options());
  442. Open();
  443. }
  444. }
  445. if (method != NULL) {
  446. RunBenchmark(num_threads, name, method);
  447. }
  448. }
  449. }
  450. private:
  451. struct ThreadArg {
  452. Benchmark* bm;
  453. SharedState* shared;
  454. ThreadState* thread;
  455. void (Benchmark::*method)(ThreadState*);
  456. };
  457. static void ThreadBody(void* v) {
  458. ThreadArg* arg = reinterpret_cast<ThreadArg*>(v);
  459. SharedState* shared = arg->shared;
  460. ThreadState* thread = arg->thread;
  461. {
  462. MutexLock l(&shared->mu);
  463. shared->num_initialized++;
  464. if (shared->num_initialized >= shared->total) {
  465. shared->cv.SignalAll();
  466. }
  467. while (!shared->start) {
  468. shared->cv.Wait();
  469. }
  470. }
  471. thread->stats.Start();
  472. (arg->bm->*(arg->method))(thread);
  473. thread->stats.Stop();
  474. {
  475. MutexLock l(&shared->mu);
  476. shared->num_done++;
  477. if (shared->num_done >= shared->total) {
  478. shared->cv.SignalAll();
  479. }
  480. }
  481. }
  482. void RunBenchmark(int n, Slice name,
  483. void (Benchmark::*method)(ThreadState*)) {
  484. SharedState shared;
  485. shared.total = n;
  486. shared.num_initialized = 0;
  487. shared.num_done = 0;
  488. shared.start = false;
  489. ThreadArg* arg = new ThreadArg[n];
  490. for (int i = 0; i < n; i++) {
  491. arg[i].bm = this;
  492. arg[i].method = method;
  493. arg[i].shared = &shared;
  494. arg[i].thread = new ThreadState(i);
  495. arg[i].thread->shared = &shared;
  496. Env::Default()->StartThread(ThreadBody, &arg[i]);
  497. }
  498. shared.mu.Lock();
  499. while (shared.num_initialized < n) {
  500. shared.cv.Wait();
  501. }
  502. shared.start = true;
  503. shared.cv.SignalAll();
  504. while (shared.num_done < n) {
  505. shared.cv.Wait();
  506. }
  507. shared.mu.Unlock();
  508. for (int i = 1; i < n; i++) {
  509. arg[0].thread->stats.Merge(arg[i].thread->stats);
  510. }
  511. arg[0].thread->stats.Report(name);
  512. for (int i = 0; i < n; i++) {
  513. delete arg[i].thread;
  514. }
  515. delete[] arg;
  516. }
  517. void Crc32c(ThreadState* thread) {
  518. // Checksum about 500MB of data total
  519. const int size = 4096;
  520. const char* label = "(4K per op)";
  521. std::string data(size, 'x');
  522. int64_t bytes = 0;
  523. uint32_t crc = 0;
  524. while (bytes < 500 * 1048576) {
  525. crc = crc32c::Value(data.data(), size);
  526. thread->stats.FinishedSingleOp();
  527. bytes += size;
  528. }
  529. // Print so result is not dead
  530. fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
  531. thread->stats.AddBytes(bytes);
  532. thread->stats.AddMessage(label);
  533. }
  534. void AcquireLoad(ThreadState* thread) {
  535. int dummy;
  536. port::AtomicPointer ap(&dummy);
  537. int count = 0;
  538. void *ptr = NULL;
  539. thread->stats.AddMessage("(each op is 1000 loads)");
  540. while (count < 100000) {
  541. for (int i = 0; i < 1000; i++) {
  542. ptr = ap.Acquire_Load();
  543. }
  544. count++;
  545. thread->stats.FinishedSingleOp();
  546. }
  547. if (ptr == NULL) exit(1); // Disable unused variable warning.
  548. }
  549. void SnappyCompress(ThreadState* thread) {
  550. RandomGenerator gen;
  551. Slice input = gen.Generate(Options().block_size);
  552. int64_t bytes = 0;
  553. int64_t produced = 0;
  554. bool ok = true;
  555. std::string compressed;
  556. while (ok && bytes < 1024 * 1048576) { // Compress 1G
  557. ok = port::Snappy_Compress(input.data(), input.size(), &compressed);
  558. produced += compressed.size();
  559. bytes += input.size();
  560. thread->stats.FinishedSingleOp();
  561. }
  562. if (!ok) {
  563. thread->stats.AddMessage("(snappy failure)");
  564. } else {
  565. char buf[100];
  566. snprintf(buf, sizeof(buf), "(output: %.1f%%)",
  567. (produced * 100.0) / bytes);
  568. thread->stats.AddMessage(buf);
  569. thread->stats.AddBytes(bytes);
  570. }
  571. }
  572. void SnappyUncompress(ThreadState* thread) {
  573. RandomGenerator gen;
  574. Slice input = gen.Generate(Options().block_size);
  575. std::string compressed;
  576. bool ok = port::Snappy_Compress(input.data(), input.size(), &compressed);
  577. int64_t bytes = 0;
  578. char* uncompressed = new char[input.size()];
  579. while (ok && bytes < 1024 * 1048576) { // Compress 1G
  580. ok = port::Snappy_Uncompress(compressed.data(), compressed.size(),
  581. uncompressed);
  582. bytes += input.size();
  583. thread->stats.FinishedSingleOp();
  584. }
  585. delete[] uncompressed;
  586. if (!ok) {
  587. thread->stats.AddMessage("(snappy failure)");
  588. } else {
  589. thread->stats.AddBytes(bytes);
  590. }
  591. }
  592. void Open() {
  593. assert(db_ == NULL);
  594. Options options;
  595. options.create_if_missing = !FLAGS_use_existing_db;
  596. options.block_cache = cache_;
  597. options.write_buffer_size = FLAGS_write_buffer_size;
  598. Status s = DB::Open(options, FLAGS_db, &db_);
  599. if (!s.ok()) {
  600. fprintf(stderr, "open error: %s\n", s.ToString().c_str());
  601. exit(1);
  602. }
  603. }
  604. void WriteSeq(ThreadState* thread) {
  605. DoWrite(thread, true);
  606. }
  607. void WriteRandom(ThreadState* thread) {
  608. DoWrite(thread, false);
  609. }
  610. void DoWrite(ThreadState* thread, bool seq) {
  611. if (num_ != FLAGS_num) {
  612. char msg[100];
  613. snprintf(msg, sizeof(msg), "(%d ops)", num_);
  614. thread->stats.AddMessage(msg);
  615. }
  616. RandomGenerator gen;
  617. WriteBatch batch;
  618. Status s;
  619. int64_t bytes = 0;
  620. for (int i = 0; i < num_; i += entries_per_batch_) {
  621. batch.Clear();
  622. for (int j = 0; j < entries_per_batch_; j++) {
  623. const int k = seq ? i+j : (thread->rand.Next() % FLAGS_num);
  624. char key[100];
  625. snprintf(key, sizeof(key), "%016d", k);
  626. batch.Put(key, gen.Generate(value_size_));
  627. bytes += value_size_ + strlen(key);
  628. thread->stats.FinishedSingleOp();
  629. }
  630. s = db_->Write(write_options_, &batch);
  631. if (!s.ok()) {
  632. fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  633. exit(1);
  634. }
  635. }
  636. thread->stats.AddBytes(bytes);
  637. }
  638. void ReadSequential(ThreadState* thread) {
  639. Iterator* iter = db_->NewIterator(ReadOptions());
  640. int i = 0;
  641. int64_t bytes = 0;
  642. for (iter->SeekToFirst(); i < reads_ && iter->Valid(); iter->Next()) {
  643. bytes += iter->key().size() + iter->value().size();
  644. thread->stats.FinishedSingleOp();
  645. ++i;
  646. }
  647. delete iter;
  648. thread->stats.AddBytes(bytes);
  649. }
  650. void ReadReverse(ThreadState* thread) {
  651. Iterator* iter = db_->NewIterator(ReadOptions());
  652. int i = 0;
  653. int64_t bytes = 0;
  654. for (iter->SeekToLast(); i < reads_ && iter->Valid(); iter->Prev()) {
  655. bytes += iter->key().size() + iter->value().size();
  656. thread->stats.FinishedSingleOp();
  657. ++i;
  658. }
  659. delete iter;
  660. thread->stats.AddBytes(bytes);
  661. }
  662. void ReadRandom(ThreadState* thread) {
  663. ReadOptions options;
  664. std::string value;
  665. for (int i = 0; i < reads_; i++) {
  666. char key[100];
  667. const int k = thread->rand.Next() % FLAGS_num;
  668. snprintf(key, sizeof(key), "%016d", k);
  669. db_->Get(options, key, &value);
  670. thread->stats.FinishedSingleOp();
  671. }
  672. }
  673. void ReadHot(ThreadState* thread) {
  674. ReadOptions options;
  675. std::string value;
  676. const int range = (FLAGS_num + 99) / 100;
  677. for (int i = 0; i < reads_; i++) {
  678. char key[100];
  679. const int k = thread->rand.Next() % range;
  680. snprintf(key, sizeof(key), "%016d", k);
  681. db_->Get(options, key, &value);
  682. thread->stats.FinishedSingleOp();
  683. }
  684. }
  685. void ReadWhileWriting(ThreadState* thread) {
  686. if (thread->tid > 0) {
  687. ReadRandom(thread);
  688. } else {
  689. // Special thread that keeps writing until other threads are done.
  690. RandomGenerator gen;
  691. while (true) {
  692. {
  693. MutexLock l(&thread->shared->mu);
  694. if (thread->shared->num_done + 1 >= thread->shared->num_initialized) {
  695. // Other threads have finished
  696. break;
  697. }
  698. }
  699. const int k = thread->rand.Next() % FLAGS_num;
  700. char key[100];
  701. snprintf(key, sizeof(key), "%016d", k);
  702. Status s = db_->Put(write_options_, key, gen.Generate(value_size_));
  703. if (!s.ok()) {
  704. fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  705. exit(1);
  706. }
  707. }
  708. // Do not count any of the preceding work/delay in stats.
  709. thread->stats.Start();
  710. }
  711. }
  712. void Compact(ThreadState* thread) {
  713. db_->CompactRange(NULL, NULL);
  714. }
  715. void PrintStats() {
  716. std::string stats;
  717. if (!db_->GetProperty("leveldb.stats", &stats)) {
  718. stats = "(failed)";
  719. }
  720. fprintf(stdout, "\n%s\n", stats.c_str());
  721. }
  722. static void WriteToFile(void* arg, const char* buf, int n) {
  723. reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n));
  724. }
  725. void HeapProfile() {
  726. char fname[100];
  727. snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db, ++heap_counter_);
  728. WritableFile* file;
  729. Status s = Env::Default()->NewWritableFile(fname, &file);
  730. if (!s.ok()) {
  731. fprintf(stderr, "%s\n", s.ToString().c_str());
  732. return;
  733. }
  734. bool ok = port::GetHeapProfile(WriteToFile, file);
  735. delete file;
  736. if (!ok) {
  737. fprintf(stderr, "heap profiling not supported\n");
  738. Env::Default()->DeleteFile(fname);
  739. }
  740. }
  741. };
  742. } // namespace leveldb
  743. int main(int argc, char** argv) {
  744. FLAGS_write_buffer_size = leveldb::Options().write_buffer_size;
  745. FLAGS_open_files = leveldb::Options().max_open_files;
  746. for (int i = 1; i < argc; i++) {
  747. double d;
  748. int n;
  749. char junk;
  750. if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
  751. FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
  752. } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
  753. FLAGS_compression_ratio = d;
  754. } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
  755. (n == 0 || n == 1)) {
  756. FLAGS_histogram = n;
  757. } else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&
  758. (n == 0 || n == 1)) {
  759. FLAGS_use_existing_db = n;
  760. } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
  761. FLAGS_num = n;
  762. } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {
  763. FLAGS_reads = n;
  764. } else if (sscanf(argv[i], "--threads=%d%c", &n, &junk) == 1) {
  765. FLAGS_threads = n;
  766. } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
  767. FLAGS_value_size = n;
  768. } else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) {
  769. FLAGS_write_buffer_size = n;
  770. } else if (sscanf(argv[i], "--cache_size=%d%c", &n, &junk) == 1) {
  771. FLAGS_cache_size = n;
  772. } else if (sscanf(argv[i], "--open_files=%d%c", &n, &junk) == 1) {
  773. FLAGS_open_files = n;
  774. } else if (strncmp(argv[i], "--db=", 5) == 0) {
  775. FLAGS_db = argv[i] + 5;
  776. } else {
  777. fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
  778. exit(1);
  779. }
  780. }
  781. leveldb::Benchmark benchmark;
  782. benchmark.Run();
  783. return 0;
  784. }