小组成员:10215300402-朱维清 & 10222140408 谷杰
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.

541 regels
15 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/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. // readseq -- read N values sequentially
  26. // readreverse -- read N values in reverse order
  27. // readrandom -- read N values in random order
  28. // crc32c -- repeated crc32c of 4K of data
  29. // sha1 -- repeated SHA1 computation over 4K of data
  30. // Meta operations:
  31. // compact -- Compact the entire DB
  32. // heapprofile -- Dump a heap profile (if supported by this port)
  33. // sync -- switch to synchronous writes (not the default)
  34. // nosync -- switch to asynchronous writes (the default)
  35. // tenth -- divide N by 10 (i.e., following benchmarks are smaller)
  36. // normal -- reset N back to its normal value (1000000)
  37. static const char* FLAGS_benchmarks =
  38. "fillseq,"
  39. "fillsync,"
  40. "fillrandom,"
  41. "overwrite,"
  42. "readrandom,"
  43. "readrandom," // Extra run to allow previous compactions to quiesce
  44. "readseq,"
  45. "readreverse,"
  46. "compact,"
  47. "readrandom,"
  48. "readseq,"
  49. "readreverse,"
  50. "fill100K,"
  51. "crc32c,"
  52. "sha1"
  53. ;
  54. // Number of key/values to place in database
  55. static int FLAGS_num = 1000000;
  56. // Size of each value
  57. static int FLAGS_value_size = 100;
  58. // Arrange to generate values that shrink to this fraction of
  59. // their original size after compression
  60. static double FLAGS_compression_ratio = 0.5;
  61. // Print histogram of operation timings
  62. static bool FLAGS_histogram = false;
  63. // Number of bytes to buffer in memtable before compacting
  64. static int FLAGS_write_buffer_size = 1 << 20;
  65. namespace leveldb {
  66. // Helper for quickly generating random data.
  67. namespace {
  68. class RandomGenerator {
  69. private:
  70. std::string data_;
  71. int pos_;
  72. public:
  73. RandomGenerator() {
  74. // We use a limited amount of data over and over again and ensure
  75. // that it is larger than the compression window (32KB), and also
  76. // large enough to serve all typical value sizes we want to write.
  77. Random rnd(301);
  78. std::string piece;
  79. while (data_.size() < 1048576) {
  80. // Add a short fragment that is as compressible as specified
  81. // by FLAGS_compression_ratio.
  82. test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);
  83. data_.append(piece);
  84. }
  85. pos_ = 0;
  86. }
  87. Slice Generate(int len) {
  88. if (pos_ + len > data_.size()) {
  89. pos_ = 0;
  90. assert(len < data_.size());
  91. }
  92. pos_ += len;
  93. return Slice(data_.data() + pos_ - len, len);
  94. }
  95. };
  96. static Slice TrimSpace(Slice s) {
  97. int start = 0;
  98. while (start < s.size() && isspace(s[start])) {
  99. start++;
  100. }
  101. int limit = s.size();
  102. while (limit > start && isspace(s[limit-1])) {
  103. limit--;
  104. }
  105. return Slice(s.data() + start, limit - start);
  106. }
  107. }
  108. class Benchmark {
  109. private:
  110. Cache* cache_;
  111. DB* db_;
  112. int num_;
  113. int heap_counter_;
  114. double start_;
  115. double last_op_finish_;
  116. int64_t bytes_;
  117. std::string message_;
  118. Histogram hist_;
  119. RandomGenerator gen_;
  120. Random rand_;
  121. // State kept for progress messages
  122. int done_;
  123. int next_report_; // When to report next
  124. void PrintHeader() {
  125. const int kKeySize = 16;
  126. PrintEnvironment();
  127. fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
  128. fprintf(stdout, "Values: %d bytes each (%d bytes after compression)\n",
  129. FLAGS_value_size,
  130. static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
  131. fprintf(stdout, "Entries: %d\n", num_);
  132. fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
  133. (((kKeySize + FLAGS_value_size) * num_) / 1048576.0));
  134. fprintf(stdout, "FileSize: %.1f MB (estimated)\n",
  135. (((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_)
  136. / 1048576.0));
  137. PrintWarnings();
  138. fprintf(stdout, "------------------------------------------------\n");
  139. }
  140. void PrintWarnings() {
  141. #if defined(__GNUC__) && !defined(__OPTIMIZE__)
  142. fprintf(stdout,
  143. "WARNING: Optimization is disabled: benchmarks unnecessarily slow\n"
  144. );
  145. #endif
  146. #ifndef NDEBUG
  147. fprintf(stdout,
  148. "WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
  149. #endif
  150. }
  151. void PrintEnvironment() {
  152. fprintf(stderr, "LevelDB: version %d.%d\n",
  153. kMajorVersion, kMinorVersion);
  154. #if defined(__linux)
  155. time_t now = time(NULL);
  156. fprintf(stderr, "Date: %s", ctime(&now)); // ctime() adds newline
  157. FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
  158. if (cpuinfo != NULL) {
  159. char line[1000];
  160. int num_cpus = 0;
  161. std::string cpu_type;
  162. std::string cache_size;
  163. while (fgets(line, sizeof(line), cpuinfo) != NULL) {
  164. const char* sep = strchr(line, ':');
  165. if (sep == NULL) {
  166. continue;
  167. }
  168. Slice key = TrimSpace(Slice(line, sep - 1 - line));
  169. Slice val = TrimSpace(Slice(sep + 1));
  170. if (key == "model name") {
  171. ++num_cpus;
  172. cpu_type = val.ToString();
  173. } else if (key == "cache size") {
  174. cache_size = val.ToString();
  175. }
  176. }
  177. fclose(cpuinfo);
  178. fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
  179. fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
  180. }
  181. #endif
  182. }
  183. void Start() {
  184. start_ = Env::Default()->NowMicros() * 1e-6;
  185. bytes_ = 0;
  186. message_.clear();
  187. last_op_finish_ = start_;
  188. hist_.Clear();
  189. done_ = 0;
  190. next_report_ = 100;
  191. }
  192. void FinishedSingleOp() {
  193. if (FLAGS_histogram) {
  194. double now = Env::Default()->NowMicros() * 1e-6;
  195. double micros = (now - last_op_finish_) * 1e6;
  196. hist_.Add(micros);
  197. if (micros > 20000) {
  198. fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
  199. fflush(stderr);
  200. }
  201. last_op_finish_ = now;
  202. }
  203. done_++;
  204. if (done_ >= next_report_) {
  205. if (next_report_ < 1000) {
  206. next_report_ += 100;
  207. } else if (next_report_ < 10000) {
  208. next_report_ += 1000;
  209. } else if (next_report_ < 100000) {
  210. next_report_ += 10000;
  211. } else {
  212. next_report_ += 100000;
  213. }
  214. fprintf(stderr, "... finished %d ops%30s\r", done_, "");
  215. fflush(stderr);
  216. }
  217. }
  218. void Stop(const Slice& name) {
  219. double finish = Env::Default()->NowMicros() * 1e-6;
  220. // Pretend at least one op was done in case we are running a benchmark
  221. // that does nto call FinishedSingleOp().
  222. if (done_ < 1) done_ = 1;
  223. if (bytes_ > 0) {
  224. char rate[100];
  225. snprintf(rate, sizeof(rate), "%5.1f MB/s",
  226. (bytes_ / 1048576.0) / (finish - start_));
  227. if (!message_.empty()) {
  228. message_ = std::string(rate) + " " + message_;
  229. } else {
  230. message_ = rate;
  231. }
  232. }
  233. fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
  234. name.ToString().c_str(),
  235. (finish - start_) * 1e6 / done_,
  236. (message_.empty() ? "" : " "),
  237. message_.c_str());
  238. if (FLAGS_histogram) {
  239. fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
  240. }
  241. fflush(stdout);
  242. }
  243. public:
  244. enum Order {
  245. SEQUENTIAL,
  246. RANDOM
  247. };
  248. enum DBState {
  249. FRESH,
  250. EXISTING
  251. };
  252. Benchmark() : cache_(NewLRUCache(200<<20)),
  253. db_(NULL),
  254. num_(FLAGS_num),
  255. heap_counter_(0),
  256. bytes_(0),
  257. rand_(301) {
  258. std::vector<std::string> files;
  259. Env::Default()->GetChildren("/tmp/dbbench", &files);
  260. for (int i = 0; i < files.size(); i++) {
  261. if (Slice(files[i]).starts_with("heap-")) {
  262. Env::Default()->DeleteFile("/tmp/dbbench/" + files[i]);
  263. }
  264. }
  265. DestroyDB("/tmp/dbbench", Options());
  266. }
  267. ~Benchmark() {
  268. delete db_;
  269. delete cache_;
  270. }
  271. void Run() {
  272. PrintHeader();
  273. Open();
  274. const char* benchmarks = FLAGS_benchmarks;
  275. while (benchmarks != NULL) {
  276. const char* sep = strchr(benchmarks, ',');
  277. Slice name;
  278. if (sep == NULL) {
  279. name = benchmarks;
  280. benchmarks = NULL;
  281. } else {
  282. name = Slice(benchmarks, sep - benchmarks);
  283. benchmarks = sep + 1;
  284. }
  285. Start();
  286. WriteOptions write_options;
  287. write_options.sync = false;
  288. if (name == Slice("fillseq")) {
  289. Write(write_options, SEQUENTIAL, FRESH, num_, FLAGS_value_size);
  290. } else if (name == Slice("fillrandom")) {
  291. Write(write_options, RANDOM, FRESH, num_, FLAGS_value_size);
  292. } else if (name == Slice("overwrite")) {
  293. Write(write_options, RANDOM, EXISTING, num_, FLAGS_value_size);
  294. } else if (name == Slice("fillsync")) {
  295. write_options.sync = true;
  296. Write(write_options, RANDOM, FRESH, num_ / 100, FLAGS_value_size);
  297. } else if (name == Slice("fill100K")) {
  298. Write(write_options, RANDOM, FRESH, num_ / 1000, 100 * 1000);
  299. } else if (name == Slice("readseq")) {
  300. ReadSequential();
  301. } else if (name == Slice("readreverse")) {
  302. ReadReverse();
  303. } else if (name == Slice("readrandom")) {
  304. ReadRandom();
  305. } else if (name == Slice("compact")) {
  306. Compact();
  307. } else if (name == Slice("crc32c")) {
  308. Crc32c(4096, "(4K per op)");
  309. } else if (name == Slice("sha1")) {
  310. SHA1(4096, "(4K per op)");
  311. } else if (name == Slice("heapprofile")) {
  312. HeapProfile();
  313. } else {
  314. fprintf(stderr, "unknown benchmark '%s'\n", name.ToString().c_str());
  315. }
  316. Stop(name);
  317. }
  318. }
  319. private:
  320. void Crc32c(int size, const char* label) {
  321. // Checksum about 500MB of data total
  322. std::string data(size, 'x');
  323. int64_t bytes = 0;
  324. uint32_t crc = 0;
  325. while (bytes < 500 * 1048576) {
  326. crc = crc32c::Value(data.data(), size);
  327. FinishedSingleOp();
  328. bytes += size;
  329. }
  330. // Print so result is not dead
  331. fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
  332. bytes_ = bytes;
  333. message_ = label;
  334. }
  335. void SHA1(int size, const char* label) {
  336. // SHA1 about 100MB of data total
  337. std::string data(size, 'x');
  338. int64_t bytes = 0;
  339. char sha1[20];
  340. while (bytes < 100 * 1048576) {
  341. port::SHA1_Hash(data.data(), size, sha1);
  342. FinishedSingleOp();
  343. bytes += size;
  344. }
  345. // Print so result is not dead
  346. fprintf(stderr, "... sha1=%02x...\r", static_cast<unsigned int>(sha1[0]));
  347. bytes_ = bytes;
  348. message_ = label;
  349. }
  350. void Open() {
  351. assert(db_ == NULL);
  352. Options options;
  353. options.create_if_missing = true;
  354. options.max_open_files = 10000;
  355. options.block_cache = cache_;
  356. options.write_buffer_size = FLAGS_write_buffer_size;
  357. Status s = DB::Open(options, "/tmp/dbbench", &db_);
  358. if (!s.ok()) {
  359. fprintf(stderr, "open error: %s\n", s.ToString().c_str());
  360. exit(1);
  361. }
  362. }
  363. void Write(const WriteOptions& options, Order order, DBState state,
  364. int num_entries, int value_size) {
  365. if (state == FRESH) {
  366. delete db_;
  367. db_ = NULL;
  368. DestroyDB("/tmp/dbbench", Options());
  369. Open();
  370. Start(); // Do not count time taken to destroy/open
  371. }
  372. if (num_entries != num_) {
  373. char msg[100];
  374. snprintf(msg, sizeof(msg), "(%d ops)", num_entries);
  375. message_ = msg;
  376. }
  377. WriteBatch batch;
  378. Status s;
  379. std::string val;
  380. for (int i = 0; i < num_entries; i++) {
  381. const int k = (order == SEQUENTIAL) ? i : (rand_.Next() % FLAGS_num);
  382. char key[100];
  383. snprintf(key, sizeof(key), "%016d", k);
  384. batch.Clear();
  385. batch.Put(key, gen_.Generate(value_size));
  386. s = db_->Write(options, &batch);
  387. bytes_ += value_size + strlen(key);
  388. if (!s.ok()) {
  389. fprintf(stderr, "put error: %s\n", s.ToString().c_str());
  390. exit(1);
  391. }
  392. FinishedSingleOp();
  393. }
  394. }
  395. void ReadSequential() {
  396. Iterator* iter = db_->NewIterator(ReadOptions());
  397. int i = 0;
  398. for (iter->SeekToFirst(); i < num_ && iter->Valid(); iter->Next()) {
  399. bytes_ += iter->key().size() + iter->value().size();
  400. FinishedSingleOp();
  401. ++i;
  402. }
  403. delete iter;
  404. }
  405. void ReadReverse() {
  406. Iterator* iter = db_->NewIterator(ReadOptions());
  407. int i = 0;
  408. for (iter->SeekToLast(); i < num_ && iter->Valid(); iter->Prev()) {
  409. bytes_ += iter->key().size() + iter->value().size();
  410. FinishedSingleOp();
  411. ++i;
  412. }
  413. delete iter;
  414. }
  415. void ReadRandom() {
  416. ReadOptions options;
  417. std::string value;
  418. for (int i = 0; i < num_; i++) {
  419. char key[100];
  420. const int k = rand_.Next() % FLAGS_num;
  421. snprintf(key, sizeof(key), "%016d", k);
  422. db_->Get(options, key, &value);
  423. FinishedSingleOp();
  424. }
  425. }
  426. void Compact() {
  427. DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
  428. dbi->TEST_CompactMemTable();
  429. int max_level_with_files = 1;
  430. for (int level = 1; level < config::kNumLevels; level++) {
  431. uint64_t v;
  432. char name[100];
  433. snprintf(name, sizeof(name), "leveldb.num-files-at-level%d", level);
  434. if (db_->GetProperty(name, &v) && v > 0) {
  435. max_level_with_files = level;
  436. }
  437. }
  438. for (int level = 0; level < max_level_with_files; level++) {
  439. dbi->TEST_CompactRange(level, "", "~");
  440. }
  441. }
  442. static void WriteToFile(void* arg, const char* buf, int n) {
  443. reinterpret_cast<WritableFile*>(arg)->Append(Slice(buf, n));
  444. }
  445. void HeapProfile() {
  446. char fname[100];
  447. snprintf(fname, sizeof(fname), "/tmp/dbbench/heap-%04d", ++heap_counter_);
  448. WritableFile* file;
  449. Status s = Env::Default()->NewWritableFile(fname, &file);
  450. if (!s.ok()) {
  451. message_ = s.ToString();
  452. return;
  453. }
  454. bool ok = port::GetHeapProfile(WriteToFile, file);
  455. delete file;
  456. if (!ok) {
  457. message_ = "not supported";
  458. Env::Default()->DeleteFile(fname);
  459. }
  460. }
  461. };
  462. }
  463. int main(int argc, char** argv) {
  464. for (int i = 1; i < argc; i++) {
  465. double d;
  466. int n;
  467. char junk;
  468. if (leveldb::Slice(argv[i]).starts_with("--benchmarks=")) {
  469. FLAGS_benchmarks = argv[i] + strlen("--benchmarks=");
  470. } else if (sscanf(argv[i], "--compression_ratio=%lf%c", &d, &junk) == 1) {
  471. FLAGS_compression_ratio = d;
  472. } else if (sscanf(argv[i], "--histogram=%d%c", &n, &junk) == 1 &&
  473. (n == 0 || n == 1)) {
  474. FLAGS_histogram = n;
  475. } else if (sscanf(argv[i], "--num=%d%c", &n, &junk) == 1) {
  476. FLAGS_num = n;
  477. } else if (sscanf(argv[i], "--value_size=%d%c", &n, &junk) == 1) {
  478. FLAGS_value_size = n;
  479. } else if (sscanf(argv[i], "--write_buffer_size=%d%c", &n, &junk) == 1) {
  480. FLAGS_write_buffer_size = n;
  481. } else {
  482. fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
  483. exit(1);
  484. }
  485. }
  486. leveldb::Benchmark benchmark;
  487. benchmark.Run();
  488. return 0;
  489. }