Files
2025-08-14 17:13:05 +09:00

118 lines
3.2 KiB
Markdown

# Benchmarks Directory (`benchmarks/`)
This directory contains performance benchmarks and profiling tools for the Pokemon battle simulator using Google Benchmark.
## Quick Start
To run the benchmarks:
```bash
# Build with benchmarks enabled
cmake -DBUILD_BENCHMARKS=ON ..
make -j$(nproc)
# Run benchmarks
./benchmarks/benchmarks
# Run specific benchmark
./benchmarks/benchmarks --benchmark_filter=BM_BattleSimulation
# Save results to JSON
./benchmarks/benchmarks --benchmark_out=results.json --benchmark_out_format=json
```
## Benchmark Examples
### Basic Benchmark
```cpp
static void BM_SimpleFunction(benchmark::State& state) {
for (auto _ : state) {
// Code to benchmark
function_to_test();
}
}
BENCHMARK(BM_SimpleFunction);
```
### Parameterized Benchmark
```cpp
static void BM_Parameterized(benchmark::State& state) {
int size = state.range(0);
for (auto _ : state) {
// Code using size parameter
}
}
BENCHMARK(BM_Parameterized)->Arg(100)->Arg(1000)->Arg(10000);
```
### Fixture-based Benchmark
```cpp
class MyFixture : public benchmark::Fixture {
public:
void SetUp(const benchmark::State& state) override {
// Setup code
}
void TearDown(const benchmark::State& state) override {
// Cleanup code
}
};
BENCHMARK_F(MyFixture, BM_TestWithFixture)(benchmark::State& state) {
for (auto _ : state) {
// Test code
}
}
```
## Current Benchmarks
### Core Benchmarks (`core/`)
- **Battle Simulation**: Measures battle simulation performance
- **Pokemon Creation**: Tests Pokemon instantiation speed
- **Bulk Operations**: Performance with multiple Pokemon
- **Health Operations**: Getter/setter performance
## Benchmark Output
The benchmarks provide detailed timing information including:
- **Mean**: Average execution time
- **Median**: Middle value of execution times
- **StdDev**: Standard deviation of results
- **Iterations**: Number of times the benchmark was run
## Performance Optimization Tips
1. **Use `benchmark::DoNotOptimize()`** to prevent compiler optimization
2. **Use `benchmark::ClobberMemory()`** for memory benchmarks
3. **Set appropriate iteration counts** for consistent measurements
4. **Use fixtures** for complex setup/teardown scenarios
5. **Run multiple repetitions** for statistical significance
## Key Performance Targets
### Battle Simulation
- **Target**: 1M+ battles per second on modern hardware
- **Memory**: < 1KB per Pokemon instance
- **Latency**: < 1µs per move calculation
### Data Loading
- **Target**: < 100ms to load all Gen 1 data
- **Memory**: Efficient data structures with minimal overhead
- **Caching**: Fast lookup tables for frequently accessed data
### AI Performance
- **Random AI**: < 1µs per decision
- **Minimax AI**: Configurable depth with time limits
- **Memory**: Bounded memory usage for tree search
## Profiling Integration
- **CPU Profiling**: Integration with perf, VTune
- **Memory Profiling**: Valgrind, AddressSanitizer integration
- **Cache Analysis**: Cache miss analysis and optimization
- **Flame Graphs**: Visual performance analysis
## Continuous Performance Monitoring
Benchmarks run automatically to detect performance regressions and track improvements over time.