Overview
db_bench is RocksDB’s primary benchmarking tool for measuring database performance across various workloads. It provides extensive configuration options and supports dozens of benchmark types including sequential writes, random reads, compaction testing, and mixed workloads.
The tool is essential for:
- Performance testing and regression detection
- Tuning database configurations
- Evaluating hardware capabilities
- Comparing different compression algorithms
- Stress testing concurrent operations
Building db_bench
Build a release binary for accurate performance measurements:Always use release builds (
DEBUG_LEVEL=0) for benchmarking. Debug builds include assertions and logging that significantly impact performance.Basic Usage
Simple Sequential Write Benchmark
Random Read Benchmark
Available Benchmarks
db_bench supports a comprehensive set of benchmark operations. Specify multiple benchmarks as a comma-separated list in execution order.
Write Benchmarks
Read Benchmarks
Iterator Benchmarks
- seekrandom - N random seeks, calling Next() seek_nexts times per seek
- seekrandomwhilewriting - seekrandom with 1 thread doing overwrites
- newiterator - Repeated iterator creation
Compaction Benchmarks
- compact - Compact the entire DB
- compactall - Compact the entire DB
- compact0 - Compact L0 into L1
- compact1 - Compact L1 into L2
- waitforcompaction - Pause until compaction is complete
Mixed Workload Benchmarks
- readwhilewriting - 1 writer, N threads doing random reads
- readrandomwriterandom - N threads doing random read-write
- updaterandom - N threads doing read-modify-write for random keys
Key Configuration Options
Database Size and Keys
| Flag | Default | Description |
|---|---|---|
--num | 1000000 | Number of key/values to place in database |
--key_size | 16 | Size of each key in bytes |
--value_size | 100 | Size of each value in bytes |
--compression_ratio | 0.5 | Compression ratio for generated values |
Threading and Duration
| Flag | Default | Description |
|---|---|---|
--threads | 1 | Number of concurrent threads |
--duration | 0 | Time in seconds for random-ops tests (0 = use —num) |
--batch_size | 1 | Batch size for operations |
Memory Configuration
| Flag | Default | Description |
|---|---|---|
--cache_size | 32MB | Block cache size |
--write_buffer_size | 64MB | Size of write buffer (memtable) |
--max_write_buffer_number | 2 | Number of write buffers |
--db_write_buffer_size | 0 | Total size of all memtables |
Compaction Settings
| Flag | Default | Description |
|---|---|---|
--max_background_jobs | 2 | Max background compaction/flush jobs |
--compaction_style | 0 | 0=Level, 1=Universal, 2=FIFO |
--subcompactions | 1 | Number of subcompactions per job |
Compression
| Flag | Description |
|---|---|
--compression_type | Compression algorithm (snappy, zstd, lz4, etc.) |
--compression_level | Compression level for algorithms that support it |
Advanced Use Cases
Multi-Database Benchmarking
Test across multiple database instances:Column Family Testing
Skewed Access Patterns
Use exponential distribution for read keys:Transaction Testing
Backup and Restore
Output Interpretation
db_bench outputs performance metrics including:
- ops/sec - Operations per second (throughput)
- MB/s - Megabytes per second processed
- micros/op - Microseconds per operation (latency)
- Percentiles - P50, P75, P99, P99.9, P99.99 latencies
Statistics and Monitoring
Enable detailed statistics:- stats - Print database statistics
- resetstats - Reset database statistics
- levelstats - Print number of files and bytes per level
- sstables - Print SST file information
Performance Tips
Use appropriate cache size
Set
--cache_size to approximately the size of your working set for read-heavy workloads.Tune write buffer size
Larger write buffers reduce write amplification but use more memory. Balance based on your RAM availability.
Troubleshooting
Out of Memory
Reduce memory footprint:Slow Compaction
Increase compaction parallelism:High Write Amplification
Adjust compaction settings:Source Reference
Thedb_bench tool is implemented in:
tools/db_bench_tool.cc- Main implementation with all benchmark logic- Build with:
make db_bench(debug) orDEBUG_LEVEL=0 make db_bench(release)
For a complete list of all available flags and options, run
./db_bench --help or examine the DEFINE_* statements in tools/db_bench_tool.cc:111-708.