Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
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 be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include <sqlite3.h>6#include <stdio.h>7#include <stdlib.h>8#include <time.h>10#include "util/histogram.h"11#include "util/random.h"12#include "util/testutil.h"14// Comma-separated list of operations to run in the specified order15// Actual benchmarks:16//17// fillseq -- write N values in sequential key order in async mode18// fillseqsync -- write N/100 values in sequential key order in sync mode19// fillseqbatch -- batch write N values in sequential key order in async mode20// fillrandom -- write N values in random key order in async mode21// fillrandsync -- write N/100 values in random key order in sync mode22// fillrandbatch -- batch write N values in sequential key order in async mode23// overwrite -- overwrite N values in random key order in async mode24// fillrand100K -- write N/1000 100K values in random order in async mode25// fillseq100K -- write N/1000 100K values in sequential order in async mode26// readseq -- read N times sequentially27// readrandom -- read N times in random order28// readrand100K -- read N/1000 100K values in sequential order in async mode29static const char* FLAGS_benchmarks =30"fillseq,"31"fillseqsync,"32"fillseqbatch,"33"fillrandom,"34"fillrandsync,"35"fillrandbatch,"36"overwrite,"37"overwritebatch,"38"readrandom,"39"readseq,"40"fillrand100K,"41"fillseq100K,"42"readseq,"43"readrand100K,";45// Number of key/values to place in database46static int FLAGS_num = 1000000;48// Number of read operations to do. If negative, do FLAGS_num reads.49static int FLAGS_reads = -1;51// Size of each value52static int FLAGS_value_size = 100;54// Print histogram of operation timings55static bool FLAGS_histogram = false;57// Arrange to generate values that shrink to this fraction of58// their original size after compression59static double FLAGS_compression_ratio = 0.5;61// Page size. Default 1 KB.62static int FLAGS_page_size = 1024;64// Number of pages.65// Default cache size = FLAGS_page_size * FLAGS_num_pages = 4 MB.66static int FLAGS_num_pages = 4096;68// If true, do not destroy the existing database. If you set this69// flag and also specify a benchmark that wants a fresh database, that70// benchmark will fail.71static bool FLAGS_use_existing_db = false;73// If true, we allow batch writes to occur74static bool FLAGS_transaction = true;76// If true, we enable Write-Ahead Logging77static bool FLAGS_WAL_enabled = true;79// Use the db with the following name.80static const char* FLAGS_db = nullptr;82inline static void ExecErrorCheck(int status, char* err_msg) {83if (status != SQLITE_OK) {84fprintf(stderr, "SQL error: %s\n", err_msg);85sqlite3_free(err_msg);86exit(1);87}88}90inline static void StepErrorCheck(int status) {91if (status != SQLITE_DONE) {92fprintf(stderr, "SQL step error: status = %d\n", status);93exit(1);94}95}97inline static void ErrorCheck(int status) {98if (status != SQLITE_OK) {99fprintf(stderr, "sqlite3 error: status = %d\n", status);100exit(1);101}102}104inline static void WalCheckpoint(sqlite3* db_) {105// Flush all writes to disk106if (FLAGS_WAL_enabled) {107sqlite3_wal_checkpoint_v2(db_, nullptr, SQLITE_CHECKPOINT_FULL, nullptr,108nullptr);109}110}112namespace leveldb {114// Helper for quickly generating random data.115namespace {116class RandomGenerator {117private:118std::string data_;119int pos_;121public:122RandomGenerator() {123// We use a limited amount of data over and over again and ensure124// that it is larger than the compression window (32KB), and also125// large enough to serve all typical value sizes we want to write.126Random rnd(301);127std::string piece;128while (data_.size() < 1048576) {129// Add a short fragment that is as compressible as specified130// by FLAGS_compression_ratio.131test::CompressibleString(&rnd, FLAGS_compression_ratio, 100, &piece);132data_.append(piece);133}134pos_ = 0;135}137Slice Generate(int len) {138if (pos_ + len > data_.size()) {139pos_ = 0;140assert(len < data_.size());141}142pos_ += len;143return Slice(data_.data() + pos_ - len, len);144}145};147static Slice TrimSpace(Slice s) {148int start = 0;149while (start < s.size() && isspace(s[start])) {150start++;151}152int limit = s.size();153while (limit > start && isspace(s[limit - 1])) {154limit--;155}156return Slice(s.data() + start, limit - start);157}159} // namespace161class Benchmark {162private:163sqlite3* db_;164int db_num_;165int num_;166int reads_;167double start_;168double last_op_finish_;169int64_t bytes_;170std::string message_;171Histogram hist_;172RandomGenerator gen_;173Random rand_;175// State kept for progress messages176int done_;177int next_report_; // When to report next179void PrintHeader() {180const int kKeySize = 16;181PrintEnvironment();182fprintf(stdout, "Keys: %d bytes each\n", kKeySize);183fprintf(stdout, "Values: %d bytes each\n", FLAGS_value_size);184fprintf(stdout, "Entries: %d\n", num_);185fprintf(stdout, "RawSize: %.1f MB (estimated)\n",186((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_) /1871048576.0));188PrintWarnings();189fprintf(stdout, "------------------------------------------------\n");190}192void PrintWarnings() {193#if defined(__GNUC__) && !defined(__OPTIMIZE__)194fprintf(195stdout,196"WARNING: Optimization is disabled: benchmarks unnecessarily slow\n");197#endif198#ifndef NDEBUG199fprintf(stdout,200"WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");201#endif202}204void PrintEnvironment() {205fprintf(stderr, "SQLite: version %s\n", SQLITE_VERSION);207#if defined(__linux)208time_t now = time(nullptr);209fprintf(stderr, "Date: %s", ctime(&now)); // ctime() adds newline211FILE* cpuinfo = fopen("/proc/cpuinfo", "r");212if (cpuinfo != nullptr) {213char line[1000];214int num_cpus = 0;215std::string cpu_type;216std::string cache_size;217while (fgets(line, sizeof(line), cpuinfo) != nullptr) {218const char* sep = strchr(line, ':');219if (sep == nullptr) {220continue;221}222Slice key = TrimSpace(Slice(line, sep - 1 - line));223Slice val = TrimSpace(Slice(sep + 1));224if (key == "model name") {225++num_cpus;226cpu_type = val.ToString();227} else if (key == "cache size") {228cache_size = val.ToString();229}230}231fclose(cpuinfo);232fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());233fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());234}235#endif236}238void Start() {239start_ = Env::Default()->NowMicros() * 1e-6;240bytes_ = 0;241message_.clear();242last_op_finish_ = start_;243hist_.Clear();244done_ = 0;245next_report_ = 100;246}248void FinishedSingleOp() {249if (FLAGS_histogram) {250double now = Env::Default()->NowMicros() * 1e-6;
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.