提供基本的ttl测试用例
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

990 rader
30 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
4 år sedan
Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
10 år sedan
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
4 år sedan
  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 <cstdio>
  6. #include <cstdlib>
  7. #include "leveldb/cache.h"
  8. #include "leveldb/db.h"
  9. #include "leveldb/env.h"
  10. #include "leveldb/filter_policy.h"
  11. #include "leveldb/write_batch.h"
  12. #include "port/port.h"
  13. #include "util/crc32c.h"
  14. #include "util/histogram.h"
  15. #include "util/mutexlock.h"
  16. #include "util/random.h"
  17. #include "util/testutil.h"
  18. // Comma-separated list of operations to run in the specified order
  19. // Actual benchmarks:
  20. // fillseq -- write N values in sequential key order in async mode
  21. // fillrandom -- write N values in random key order in async mode
  22. // overwrite -- overwrite N values in random key order in async mode
  23. // fillsync -- write N/100 values in random key order in sync mode
  24. // fill100K -- write N/1000 100K values in random order in async mode
  25. // deleteseq -- delete N keys in sequential order
  26. // deleterandom -- delete N keys in random order
  27. // readseq -- read N times sequentially
  28. // readreverse -- read N times in reverse order
  29. // readrandom -- read N times in random order
  30. // readmissing -- read N missing keys in random order
  31. // readhot -- read N times in random order from 1% section of DB
  32. // seekrandom -- N random seeks
  33. // open -- cost of opening a DB
  34. // crc32c -- repeated crc32c of 4K of data
  35. // Meta operations:
  36. // compact -- Compact the entire DB
  37. // stats -- Print DB stats
  38. // sstables -- Print sstable info
  39. // heapprofile -- Dump a heap profile (if supported by this port)
  40. static const char* FLAGS_benchmarks =
  41. "fillseq,"
  42. "fillsync,"
  43. "fillrandom,"
  44. "overwrite,"
  45. "readrandom,"
  46. "readrandom," // Extra run to allow previous compactions to quiesce
  47. "readseq,"
  48. "readreverse,"
  49. "compact,"
  50. "readrandom,"
  51. "readseq,"
  52. "readreverse,"
  53. "fill100K,"
  54. "crc32c,"
  55. "snappycomp,"
  56. "snappyuncomp,";
  57. // Number of key/values to place in database
  58. static int FLAGS_num = 1000000;
  59. // Number of read operations to do. If negative, do FLAGS_num reads.
  60. static int FLAGS_reads = -1;
  61. // Number of concurrent threads to run.
  62. static int FLAGS_threads = 1;
  63. // Size of each value
  64. static int FLAGS_value_size = 100;
  65. // Arrange to generate values that shrink to this fraction of
  66. // their original size after compression
  67. static double FLAGS_compression_ratio = 0.5;
  68. // Print histogram of operation timings
  69. static bool FLAGS_histogram = false;
  70. // Number of bytes to buffer in memtable before compacting
  71. // (initialized to default value by "main")
  72. static int FLAGS_write_buffer_size = 0;
  73. // Number of bytes written to each file.
  74. // (initialized to default value by "main")
  75. static int FLAGS_max_file_size = 0;
  76. // Approximate size of user data packed per block (before compression.
  77. // (initialized to default value by "main")
  78. static int FLAGS_block_size = 0;
  79. // Number of bytes to use as a cache of uncompressed data.
  80. // Negative means use default settings.
  81. static int FLAGS_cache_size = -1;
  82. // Maximum number of files to keep open at the same time (use default if == 0)
  83. static int FLAGS_open_files = 0;
  84. // Bloom filter bits per key.
  85. // Negative means use default settings.
  86. static int FLAGS_bloom_bits = -1;
  87. // If true, do not destroy the existing database. If you set this
  88. // flag and also specify a benchmark that wants a fresh database, that
  89. // benchmark will fail.
  90. static bool FLAGS_use_existing_db = false;
  91. // If true, reuse existing log/MANIFEST files when re-opening a database.
  92. static bool FLAGS_reuse_logs = false;
  93. // Use the db with the following name.
  94. static const char* FLAGS_db = nullptr;
  95. namespace leveldb {
  96. namespace {
  97. leveldb::Env* g_env = nullptr;
  98. // Helper for quickly generating random data.
  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(size_t 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. #if defined(__linux)
  128. static Slice TrimSpace(Slice s) {
  129. size_t start = 0;
  130. while (start < s.size() && isspace(s[start])) {
  131. start++;
  132. }
  133. size_t limit = s.size();
  134. while (limit > start && isspace(s[limit - 1])) {
  135. limit--;
  136. }
  137. return Slice(s.data() + start, limit - start);
  138. }
  139. #endif
  140. static void AppendWithSpace(std::string* str, Slice msg) {
  141. if (msg.empty()) return;
  142. if (!str->empty()) {
  143. str->push_back(' ');
  144. }
  145. str->append(msg.data(), msg.size());
  146. }
  147. class Stats {
  148. private:
  149. double start_;
  150. double finish_;
  151. double seconds_;
  152. int done_;
  153. int next_report_;
  154. int64_t bytes_;
  155. double last_op_finish_;
  156. Histogram hist_;
  157. std::string message_;
  158. public:
  159. Stats() { Start(); }
  160. void Start() {
  161. next_report_ = 100;
  162. hist_.Clear();
  163. done_ = 0;
  164. bytes_ = 0;
  165. seconds_ = 0;
  166. message_.clear();
  167. start_ = finish_ = last_op_finish_ = g_env->NowMicros();
  168. }
  169. void Merge(const Stats& other) {
  170. hist_.Merge(other.hist_);
  171. done_ += other.done_;
  172. bytes_ += other.bytes_;
  173. seconds_ += other.seconds_;
  174. if (other.start_ < start_) start_ = other.start_;
  175. if (other.finish_ > finish_) finish_ = other.finish_;
  176. // Just keep the messages from one thread
  177. if (message_.empty()) message_ = other.message_;
  178. }
  179. void Stop() {
  180. finish_ = g_env->NowMicros();
  181. seconds_ = (finish_ - start_) * 1e-6;
  182. }
  183. void AddMessage(Slice msg) { AppendWithSpace(&message_, msg); }
  184. void FinishedSingleOp() {
  185. if (FLAGS_histogram) {
  186. double now = g_env->NowMicros();
  187. double micros = now - last_op_finish_;
  188. hist_.Add(micros);
  189. if (micros > 20000) {
  190. std::fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
  191. std::fflush(stderr);
  192. }
  193. last_op_finish_ = now;
  194. }
  195. done_++;
  196. if (done_ >= next_report_) {
  197. if (next_report_ < 1000)
  198. next_report_ += 100;
  199. else if (next_report_ < 5000)
  200. next_report_ += 500;
  201. else if (next_report_ < 10000)
  202. next_report_ += 1000;
  203. else if (next_report_ < 50000)
  204. next_report_ += 5000;
  205. else if (next_report_ < 100000)
  206. next_report_ += 10000;
  207. else if (next_report_ < 500000)
  208. next_report_ += 50000;
  209. else
  210. next_report_ += 100000;
  211. std::fprintf(stderr, "... finished %d ops%30s\r", done_, "");
  212. std::fflush(stderr);
  213. }
  214. }
  215. void AddBytes(int64_t n) { bytes_ += n; }
  216. void Report(const Slice& name) {
  217. // Pretend at least one op was done in case we are running a benchmark
  218. // that does not call FinishedSingleOp().
  219. if (done_ < 1) done_ = 1;
  220. std::string extra;
  221. if (bytes_ > 0) {
  222. // Rate is computed on actual elapsed time, not the sum of per-thread
  223. // elapsed times.
  224. double elapsed = (finish_ - start_) * 1e-6;
  225. char rate[100];
  226. std::snprintf(rate, sizeof(rate), "%6.1f MB/s",
  227. (bytes_ / 1048576.0) / elapsed);
  228. extra = rate;
  229. }
  230. AppendWithSpace(&extra, message_);
  231. std::fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
  232. name.ToString().c_str(), seconds_ * 1e6 / done_,
  233. (extra.empty() ? "" : " "), extra.c_str());
  234. if (FLAGS_histogram) {
  235. std::fprintf(stdout, "Microseconds per op:\n%s\n",
  236. hist_.ToString().c_str());
  237. }
  238. std::fflush(stdout);
  239. }
  240. };
  241. // State shared by all concurrent executions of the same benchmark.
  242. struct SharedState {
  243. port::Mutex mu;
  244. port::CondVar cv GUARDED_BY(mu);
  245. int total GUARDED_BY(mu);
  246. // Each thread goes through the following states:
  247. // (1) initializing
  248. // (2) waiting for others to be initialized
  249. // (3) running
  250. // (4) done
  251. int num_initialized GUARDED_BY(mu);
  252. int num_done GUARDED_BY(mu);
  253. bool start GUARDED_BY(mu);
  254. SharedState(int total)
  255. : cv(&mu), total(total), num_initialized(0), num_done(0), start(false) {}
  256. };
  257. // Per-thread state for concurrent executions of the same benchmark.
  258. struct ThreadState {
  259. int tid; // 0..n-1 when running in n threads
  260. Random rand; // Has different seeds for different threads
  261. Stats stats;
  262. SharedState* shared;
  263. ThreadState(int index) : tid(index), rand(1000 + index), shared(nullptr) {}
  264. };
  265. } // namespace
  266. class Benchmark {
  267. private:
  268. Cache* cache_;
  269. const FilterPolicy* filter_policy_;
  270. DB* db_;
  271. int num_;
  272. int value_size_;
  273. int entries_per_batch_;
  274. WriteOptions write_options_;
  275. int reads_;
  276. int heap_counter_;
  277. void PrintHeader() {
  278. const int kKeySize = 16;
  279. PrintEnvironment();
  280. std::fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
  281. std::fprintf(
  282. stdout, "Values: %d bytes each (%d bytes after compression)\n",
  283. FLAGS_value_size,
  284. static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
  285. std::fprintf(stdout, "Entries: %d\n", num_);
  286. std::fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
  287. ((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_) /
  288. 1048576.0));
  289. std::fprintf(
  290. stdout, "FileSize: %.1f MB (estimated)\n",
  291. (((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_) /
  292. 1048576.0));
  293. PrintWarnings();
  294. std::fprintf(stdout, "------------------------------------------------\n");
  295. }
  296. void PrintWarnings() {
  297. #if defined(__GNUC__) && !defined(__OPTIMIZE__)
  298. std::fprintf(
  299. stdout,
  300. "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n");
  301. #endif
  302. #ifndef NDEBUG
  303. std::fprintf(
  304. stdout,
  305. "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
  306. #endif
  307. // See if snappy is working by attempting to compress a compressible string
  308. const char text[] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
  309. std::string compressed;
  310. if (!port::Snappy_Compress(text, sizeof(text), &compressed)) {
  311. std::fprintf(stdout, "WARNING: Snappy compression is not enabled\n");
  312. } else if (compressed.size() >= sizeof(text)) {
  313. std::fprintf(stdout, "WARNING: Snappy compression is not effective\n");
  314. }
  315. }
  316. void PrintEnvironment() {
  317. std::fprintf(stderr, "LevelDB: version %d.%d\n", kMajorVersion,
  318. kMinorVersion);
  319. #if defined(__linux)
  320. time_t now = time(nullptr);
  321. std::fprintf(stderr, "Date: %s",
  322. ctime(&now)); // ctime() adds newline
  323. FILE* cpuinfo = std::fopen("/proc/cpuinfo", "r");
  324. if (cpuinfo != nullptr) {
  325. char line[1000];
  326. int num_cpus = 0;
  327. std::string cpu_type;
  328. std::string cache_size;
  329. while (fgets(line, sizeof(line), cpuinfo) != nullptr) {
  330. const char* sep = strchr(line, ':');
  331. if (sep == nullptr) {
  332. continue;
  333. }
  334. Slice key = TrimSpace(Slice(line, sep - 1 - line));
  335. Slice val = TrimSpace(Slice(sep + 1));
  336. if (key == "model name") {
  337. ++num_cpus;
  338. cpu_type = val.ToString();
  339. } else if (key == "cache size") {
  340. cache_size = val.ToString();
  341. }
  342. }
  343. std::fclose(cpuinfo);
  344. std::fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
  345. std::fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
  346. }
  347. #endif
  348. }
  349. public:
  350. Benchmark()
  351. : cache_(FLAGS_cache_size >= 0 ? NewLRUCache(FLAGS_cache_size) : nullptr),
  352. filter_policy_(FLAGS_bloom_bits >= 0
  353. ? NewBloomFilterPolicy(FLAGS_bloom_bits)
  354. : nullptr),
  355. db_(nullptr),
  356. num_(FLAGS_num),
  357. value_size_(FLAGS_value_size),
  358. entries_per_batch_(1),
  359. reads_(FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads),
  360. heap_counter_(0) {
  361. std::vector<std::string> files;
  362. g_env->GetChildren(FLAGS_db, &files);
  363. for (size_t i = 0; i < files.size(); i++) {
  364. if (Slice(files[i]).starts_with("heap-")) {
  365. g_env->RemoveFile(std::string(FLAGS_db) + "/" + files[i]);
  366. }
  367. }
  368. if (!FLAGS_use_existing_db) {
  369. DestroyDB(FLAGS_db, Options());
  370. }
  371. }
  372. ~Benchmark() {
  373. delete db_;
  374. delete cache_;
  375. delete filter_policy_;
  376. }
  377. void Run() {
  378. PrintHeader();
  379. Open();
  380. const char* benchmarks = FLAGS_benchmarks;
  381. while (benchmarks != nullptr) {
  382. const char* sep = strchr(benchmarks, ',');
  383. Slice name;
  384. if (sep == nullptr) {
  385. name = benchmarks;
  386. benchmarks = nullptr;
  387. } else {
  388. name = Slice(benchmarks, sep - benchmarks);
  389. benchmarks = sep + 1;
  390. }
  391. // Reset parameters that may be overridden below
  392. num_ = FLAGS_num;
  393. reads_ = (FLAGS_reads < 0 ? FLAGS_num : FLAGS_reads);
  394. value_size_ = FLAGS_value_size;
  395. entries_per_batch_ = 1;
  396. write_options_ = WriteOptions();
  397. void (Benchmark::*method)(ThreadState*) = nullptr;
  398. bool fresh_db = false;
  399. int num_threads = FLAGS_threads;
  400. if (name == Slice("open")) {
  401. method = &Benchmark::OpenBench;
  402. num_ /= 10000;
  403. if (num_ < 1) num_ = 1;
  404. } else if (name == Slice("fillseq")) {
  405. fresh_db = true;
  406. method = &Benchmark::WriteSeq;
  407. } else if (name == Slice("fillbatch")) {
  408. fresh_db = true;
  409. entries_per_batch_ = 1000;
  410. method = &Benchmark::WriteSeq;
  411. } else if (name == Slice("fillrandom")) {
  412. fresh_db = true;
  413. method = &Benchmark::WriteRandom;
  414. } else if (name == Slice("overwrite")) {
  415. fresh_db = false;
  416. method = &Benchmark::WriteRandom;
  417. } else if (name == Slice("fillsync")) {
  418. fresh_db = true;
  419. num_ /= 1000;
  420. write_options_.sync = true;
  421. method = &Benchmark::WriteRandom;
  422. } else if (name == Slice("fill100K")) {
  423. fresh_db = true;
  424. num_ /= 1000;
  425. value_size_ = 100 * 1000;
  426. method = &Benchmark::WriteRandom;
  427. } else if (name == Slice("readseq")) {
  428. method = &Benchmark::ReadSequential;
  429. } else if (name == Slice("readreverse")) {
  430. method = &Benchmark::ReadReverse;
  431. } else if (name == Slice("readrandom")) {
  432. method = &Benchmark::ReadRandom;
  433. } else if (name == Slice("readmissing")) {
  434. method = &Benchmark::ReadMissing;
  435. } else if (name == Slice("seekrandom")) {
  436. method = &Benchmark::SeekRandom;
  437. } else if (name == Slice("readhot")) {
  438. method = &Benchmark::ReadHot;
  439. } else if (name == Slice("readrandomsmall")) {
  440. reads_ /= 1000;
  441. method = &Benchmark::ReadRandom;
  442. } else if (name == Slice("deleteseq")) {
  443. method = &Benchmark::DeleteSeq;
  444. } else if (name == Slice("deleterandom")) {
  445. method = &Benchmark::DeleteRandom;
  446. } else if (name == Slice("readwhilewriting")) {
  447. num_threads++; // Add extra thread for writing
  448. method = &Benchmark::ReadWhileWriting;
  449. } else if (name == Slice("compact")) {
  450. method = &Benchmark::Compact;
  451. } else if (name == Slice("crc32c")) {
  452. method = &Benchmark::Crc32c;
  453. } else if (name == Slice("snappycomp")) {
  454. method = &Benchmark::SnappyCompress;
  455. } else if (name == Slice("snappyuncomp")) {
  456. method = &Benchmark::SnappyUncompress;
  457. } else if (name == Slice("heapprofile")) {
  458. HeapProfile();
  459. } else if (name == Slice("stats")) {
  460. PrintStats("leveldb.stats");
  461. } else if (name == Slice("sstables")) {
  462. PrintStats("leveldb.sstables");
  463. } else {
  464. if (!name.empty()) { // No error message for empty name
  465. std::fprintf(stderr, "unknown benchmark '%s'\n",
  466. name.ToString().c_str());
  467. }
  468. }
  469. if (fresh_db) {
  470. if (FLAGS_use_existing_db) {
  471. std::fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n",
  472. name.ToString().c_str());
  473. method = nullptr;
  474. } else {
  475. delete db_;
  476. db_ = nullptr;
  477. DestroyDB(FLAGS_db, Options());
  478. Open();
  479. }
  480. }
  481. if (method != nullptr) {
  482. RunBenchmark(num_threads, name, method);
  483. }
  484. }
  485. }
  486. private:
  487. struct ThreadArg {
  488. Benchmark* bm;
  489. SharedState* shared;
  490. ThreadState* thread;
  491. void (Benchmark::*method)(ThreadState*);
  492. };
  493. static void ThreadBody(void* v) {
  494. ThreadArg* arg = reinterpret_cast<ThreadArg*>(v);
  495. SharedState* shared = arg->shared;
  496. ThreadState* thread = arg->thread;
  497. {
  498. MutexLock l(&shared->mu);
  499. shared->num_initialized++;
  500. if (shared->num_initialized >= shared->total) {
  501. shared->cv.SignalAll();
  502. }
  503. while (!shared->start) {
  504. shared->cv.Wait();
  505. }
  506. }
  507. thread->stats.Start();
  508. (arg->bm->*(arg->method))(thread);
  509. thread->stats.Stop();
  510. {
  511. MutexLock l(&shared->mu);
  512. shared->num_done++;
  513. if (shared->num_done >= shared->total) {
  514. shared->cv.SignalAll();
  515. }
  516. }
  517. }
  518. void RunBenchmark(int n, Slice name,
  519. void (Benchmark::*method)(ThreadState*)) {
  520. SharedState shared(n);
  521. ThreadArg* arg = new ThreadArg[n];
  522. for (int i = 0; i < n; i++) {
  523. arg[i].bm = this;
  524. arg[i].method = method;
  525. arg[i].shared = &shared;
  526. arg[i].thread = new ThreadState(i);
  527. arg[i].thread->shared = &shared;
  528. g_env->StartThread(ThreadBody, &arg[i]);
  529. }
  530. shared.mu.Lock();
  531. while (shared.num_initialized < n) {
  532. shared.cv.Wait();
  533. }
  534. shared.start = true;
  535. shared.cv.SignalAll();
  536. while (shared.num_done < n) {
  537. shared.cv.Wait();
  538. }
  539. shared.mu.Unlock();
  540. for (int i = 1; i < n; i++) {
  541. arg[0].thread->stats.Merge(arg[i].thread->stats);
  542. }
  543. arg[0].thread->stats.Report(name);
  544. for (int i = 0; i < n; i++) {
  545. delete arg[i].thread;
  546. }
  547. delete[] arg;
  548. }
  549. void Crc32c(ThreadState* thread) {
  550. // Checksum about 500MB of data total
  551. const int size = 4096;
  552. const char* label = "(4K per op)";
  553. std::string data(size, 'x');
  554. int64_t bytes = 0;
  555. uint32_t crc = 0;
  556. while (bytes < 500 * 1048576) {
  557. crc = crc32c::Value(data.data(), size);
  558. thread->stats.FinishedSingleOp();
  559. bytes += size;
  560. }
  561. // Print so result is not dead
  562. std::fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
  563. thread->stats.AddBytes(bytes);
  564. thread->stats.AddMessage(label);
  565. }
  566. void SnappyCompress(ThreadState* thread) {
  567. RandomGenerator gen;
  568. Slice input = gen.Generate(Options().block_size);
  569. int64_t bytes = 0;
  570. int64_t produced = 0;
  571. bool ok = true;
  572. std::string compressed;
  573. while (ok && bytes < 1024 * 1048576) { // Compress 1G
  574. ok = port::Snappy_Compress(input.data(), input.size(), &compressed);
  575. produced += compressed.size();
  576. bytes += input.size();
  577. thread->stats.FinishedSingleOp();
  578. }
  579. if (!ok) {
  580. thread->stats.AddMessage("(snappy failure)");
  581. } else {
  582. char buf[100];
  583. std::snprintf(buf, sizeof(buf), "(output: %.1f%%)",
  584. (produced * 100.0) / bytes);
  585. thread->stats.AddMessage(buf);
  586. thread->stats.AddBytes(bytes);
  587. }
  588. }
  589. void SnappyUncompress(ThreadState* thread) {
  590. RandomGenerator gen;
  591. Slice input = gen.Generate(Options().block_size);
  592. std::string compressed;
  593. bool ok = port::Snappy_Compress(input.data(), input.size(), &compressed);
  594. int64_t bytes = 0;
  595. char* uncompressed = new char[input.size()];
  596. while (ok && bytes < 1024 * 1048576) { // Compress 1G
  597. ok = port::Snappy_Uncompress(compressed.data(), compressed.size(),
  598. uncompressed);
  599. bytes += input.size();
  600. thread->stats.FinishedSingleOp();
  601. }
  602. delete[] uncompressed;
  603. if (!ok) {
  604. thread->stats.AddMessage("(snappy failure)");
  605. } else {
  606. thread->stats.AddBytes(bytes);
  607. }
  608. }
  609. void Open() {
  610. assert(db_ == nullptr);
  611. Options options;
  612. options.env = g_env;
  613. options.create_if_missing = !FLAGS_use_existing_db;
  614. options.block_cache = cache_;
  615. options.write_buffer_size = FLAGS_write_buffer_size;
  616. options.max_file_size = FLAGS_max_file_size;
  617. options.block_size = FLAGS_block_size;
  618. options.max_open_files = FLAGS_open_files;
  619. options.filter_policy = filter_policy_;
  620. options.reuse_logs = FLAGS_reuse_logs;
  621. Status s = DB::Open(options, FLAGS_db, &db_);
  622. if (!s.ok()) {
  623. std::fprintf(stderr, "open error: %s\n", s.ToString().c_str());
  624. std::exit(1);
  625. }
  626. }
  627. void OpenBench(ThreadState* thread) {
  628. for (int i = 0; i < num_; i++) {
  629. delete db_;
  630. Open();
  631. thread->stats.FinishedSingleOp();
  632. }
  633. }
  634. void WriteSeq(ThreadState* thread) { DoWrite(thread, true); }
  635. void WriteRandom(ThreadState* thread) { DoWrite(thread, false); }
  636. void DoWrite(ThreadState* thread, bool seq) {
  637. if (num_ != FLAGS_num) {
  638. char msg[100];
  639. std::snprintf(msg, sizeof(msg), "(%d ops)", num_);
  640. thread->stats.AddMessage(msg);
  641. }
  642. RandomGenerator gen;
  643. WriteBatch batch;
  644. Status s;
  645. int64_t bytes = 0;
  646. for (int i = 0; i < num_; i += entries_per_batch_) {
  647. batch.Clear();
  648. for (int j = 0; j < entries_per_batch_; j++) {
  649. const int k = seq ? i + j : (thread->rand.Next() % FLAGS_num);
  650. char key[100];
  651. std::snprintf(key, sizeof(key), "%016d", k);
  652. batch.Put(key, gen.Generate(value_size_));
  653. bytes += value_size_ + strlen(key);
  654. thread->stats.FinishedSingleOp();
  655. }
  656. s = db_->Write(write_options_, &batch);
  657. if (!s.ok()) {
  658. std::fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  659. std::exit(1);
  660. }
  661. }
  662. thread->stats.AddBytes(bytes);
  663. }
  664. void ReadSequential(ThreadState* thread) {
  665. Iterator* iter = db_->NewIterator(ReadOptions());
  666. int i = 0;
  667. int64_t bytes = 0;
  668. for (iter->SeekToFirst(); i < reads_ && iter->Valid(); iter->Next()) {
  669. bytes += iter->key().size() + iter->value().size();
  670. thread->stats.FinishedSingleOp();
  671. ++i;
  672. }
  673. delete iter;
  674. thread->stats.AddBytes(bytes);
  675. }
  676. void ReadReverse(ThreadState* thread) {
  677. Iterator* iter = db_->NewIterator(ReadOptions());
  678. int i = 0;
  679. int64_t bytes = 0;
  680. for (iter->SeekToLast(); i < reads_ && iter->Valid(); iter->Prev()) {
  681. bytes += iter->key().size() + iter->value().size();
  682. thread->stats.FinishedSingleOp();
  683. ++i;
  684. }
  685. delete iter;
  686. thread->stats.AddBytes(bytes);
  687. }
  688. void ReadRandom(ThreadState* thread) {
  689. ReadOptions options;
  690. std::string value;
  691. int found = 0;
  692. for (int i = 0; i < reads_; i++) {
  693. char key[100];
  694. const int k = thread->rand.Next() % FLAGS_num;
  695. std::snprintf(key, sizeof(key), "%016d", k);
  696. if (db_->Get(options, key, &value).ok()) {
  697. found++;
  698. }
  699. thread->stats.FinishedSingleOp();
  700. }
  701. char msg[100];
  702. std::snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
  703. thread->stats.AddMessage(msg);
  704. }
  705. void ReadMissing(ThreadState* thread) {
  706. ReadOptions options;
  707. std::string value;
  708. for (int i = 0; i < reads_; i++) {
  709. char key[100];
  710. const int k = thread->rand.Next() % FLAGS_num;
  711. std::snprintf(key, sizeof(key), "%016d.", k);
  712. db_->Get(options, key, &value);
  713. thread->stats.FinishedSingleOp();
  714. }
  715. }
  716. void ReadHot(ThreadState* thread) {
  717. ReadOptions options;
  718. std::string value;
  719. const int range = (FLAGS_num + 99) / 100;
  720. for (int i = 0; i < reads_; i++) {
  721. char key[100];
  722. const int k = thread->rand.Next() % range;
  723. std::snprintf(key, sizeof(key), "%016d", k);
  724. db_->Get(options, key, &value);
  725. thread->stats.FinishedSingleOp();
  726. }
  727. }
  728. void SeekRandom(ThreadState* thread) {
  729. ReadOptions options;
  730. int found = 0;
  731. for (int i = 0; i < reads_; i++) {
  732. Iterator* iter = db_->NewIterator(options);
  733. char key[100];
  734. const int k = thread->rand.Next() % FLAGS_num;
  735. std::snprintf(key, sizeof(key), "%016d", k);
  736. iter->Seek(key);
  737. if (iter->Valid() && iter->key() == key) found++;
  738. delete iter;
  739. thread->stats.FinishedSingleOp();
  740. }
  741. char msg[100];
  742. std::snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
  743. thread->stats.AddMessage(msg);
  744. }
  745. void DoDelete(ThreadState* thread, bool seq) {
  746. RandomGenerator gen;
  747. WriteBatch batch;
  748. Status s;
  749. for (int i = 0; i < num_; i += entries_per_batch_) {
  750. batch.Clear();
  751. for (int j = 0; j < entries_per_batch_; j++) {
  752. const int k = seq ? i + j : (thread->rand.Next() % FLAGS_num);
  753. char key[100];
  754. std::snprintf(key, sizeof(key), "%016d", k);
  755. batch.Delete(key);
  756. thread->stats.FinishedSingleOp();
  757. }
  758. s = db_->Write(write_options_, &batch);
  759. if (!s.ok()) {
  760. std::fprintf(stderr, "del error: %s\n", s.ToString().c_str());
  761. std::exit(1);
  762. }
  763. }
  764. }
  765. void DeleteSeq(ThreadState* thread) { DoDelete(thread, true); }
  766. void DeleteRandom(ThreadState* thread) { DoDelete(thread, false); }
  767. void ReadWhileWriting(ThreadState* thread) {
  768. if (thread->tid > 0) {
  769. ReadRandom(thread);
  770. } else {
  771. // Special thread that keeps writing until other threads are done.
  772. RandomGenerator gen;
  773. while (true) {
  774. {
  775. MutexLock l(&thread->shared->mu);
  776. if (thread->shared->num_done + 1 >= thread->shared->num_initialized) {
  777. // Other threads have finished
  778. break;
  779. }
  780. }
  781. const int k = thread->rand.Next() % FLAGS_num;
  782. char key[100];
  783. std::snprintf(key, sizeof(key), "%016d", k);
  784. Status s = db_->Put(write_options_, key, gen.Generate(value_size_));
  785. if (!s.ok()) {
  786. std::fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  787. std::exit(1);
  788. }
  789. }
  790. // Do not count any of the preceding work/delay in stats.
  791. thread->stats.Start();
  792. }
  793. }
  794. void Compact(ThreadState* thread) { db_->CompactRange(nullptr, nullptr); }
  795. void PrintStats(const char* key) {
  796. std::string stats;
  797. if (!db_->GetProperty(key, &stats)) {
  798. stats = "(failed)";
  799. }
  800. std::fprintf(stdout, "\n%s\n", stats.c_str());
  801. }
  802. static void WriteToFile(void* arg, const char* buf, int n) {
  803. reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n));
  804. }
  805. void HeapProfile() {
  806. char fname[100];
  807. std::snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db,
  808. ++heap_counter_);
  809. WritableFile* file;
  810. Status s = g_env->NewWritableFile(fname, &file);
  811. if (!s.ok()) {
  812. std::fprintf(stderr, "%s\n", s.ToString().c_str());
  813. return;
  814. }
  815. bool ok = port::GetHeapProfile(WriteToFile, file);
  816. delete file;
  817. if (!ok) {
  818. std::fprintf(stderr, "heap profiling not supported\n");
  819. g_env->RemoveFile(fname);
  820. }
  821. }
  822. };
  823. } // namespace leveldb
  824. int main(int argc, char** argv) {
  825. FLAGS_write_buffer_size = leveldb::Options().write_buffer_size;
  826. FLAGS_max_file_size = leveldb::Options().max_file_size;
  827. FLAGS_block_size = leveldb::Options().block_size;
  828. FLAGS_open_files = leveldb::Options().max_open_files;
  829. std::string default_db_path;
  830. for (int i = 1; i < argc; i++) {
  831. double d;
  832. int n;
  833. char junk;
  834. if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
  835. FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
  836. } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
  837. FLAGS_compression_ratio = d;
  838. } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
  839. (n == 0 || n == 1)) {
  840. FLAGS_histogram = n;
  841. } else if (sscanf(argv[i], "--use_existing_db=%d%c", &n, &junk) == 1 &&
  842. (n == 0 || n == 1)) {
  843. FLAGS_use_existing_db = n;
  844. } else if (sscanf(argv[i], "--reuse_logs=%d%c", &n, &junk) == 1 &&
  845. (n == 0 || n == 1)) {
  846. FLAGS_reuse_logs = n;
  847. } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
  848. FLAGS_num = n;
  849. } else if (sscanf(argv[i], "--reads=%d%c", &n, &junk) == 1) {
  850. FLAGS_reads = n;
  851. } else if (sscanf(argv[i], "--threads=%d%c", &n, &junk) == 1) {
  852. FLAGS_threads = n;
  853. } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
  854. FLAGS_value_size = n;
  855. } else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) {
  856. FLAGS_write_buffer_size = n;
  857. } else if (sscanf(argv[i], "--max_file_size=%d%c", &n, &junk) == 1) {
  858. FLAGS_max_file_size = n;
  859. } else if (sscanf(argv[i], "--block_size=%d%c", &n, &junk) == 1) {
  860. FLAGS_block_size = n;
  861. } else if (sscanf(argv[i], "--cache_size=%d%c", &n, &junk) == 1) {
  862. FLAGS_cache_size = n;
  863. } else if (sscanf(argv[i], "--bloom_bits=%d%c", &n, &junk) == 1) {
  864. FLAGS_bloom_bits = n;
  865. } else if (sscanf(argv[i], "--open_files=%d%c", &n, &junk) == 1) {
  866. FLAGS_open_files = n;
  867. } else if (strncmp(argv[i], "--db=", 5) == 0) {
  868. FLAGS_db = argv[i] + 5;
  869. } else {
  870. std::fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
  871. std::exit(1);
  872. }
  873. }
  874. leveldb::g_env = leveldb::Env::Default();
  875. // Choose a location for the test database if none given with --db=<path>
  876. if (FLAGS_db == nullptr) {
  877. leveldb::g_env->GetTestDirectory(&default_db_path);
  878. default_db_path += "/dbbench";
  879. FLAGS_db = default_db_path.c_str();
  880. }
  881. leveldb::Benchmark benchmark;
  882. benchmark.Run();
  883. return 0;
  884. }