Files
pokemon-battle-engine/benchmarks/core/pokemon_creation_bench.cpp
cdemeyer-teachx ee337f001a codebase refactor
2025-08-15 12:38:44 +09:00

116 lines
3.5 KiB
C++

#include <benchmark/benchmark.h>
#include <pokemon_battle_sim.h>
#include <vector>
#include <string>
#include <memory>
// Benchmark for Pokemon creation performance
static void BM_PokemonCreation(benchmark::State& state) {
for (auto _ : state) {
// Create a Pokemon - this is what we're measuring
PokEng::Pokemon pokemon("Pikachu", 100);
// Prevent compiler optimization from eliminating the Pokemon
benchmark::DoNotOptimize(pokemon);
}
}
BENCHMARK(BM_PokemonCreation)
->Unit(benchmark::kNanosecond)
->Iterations(10000);
// Benchmark for Pokemon creation with different names and health values
static void BM_PokemonCreationParameterized(benchmark::State& state) {
std::string name = "Pokemon" + std::to_string(static_cast<int>(state.range(0)));
int health = static_cast<int>(state.range(1));
for (auto _ : state) {
PokEng::Pokemon pokemon(name, health);
benchmark::DoNotOptimize(pokemon);
}
}
BENCHMARK(BM_PokemonCreationParameterized)
->Args({1, 50}) // Pokemon1, 50 HP
->Args({2, 100}) // Pokemon2, 100 HP
->Args({3, 200}) // Pokemon3, 200 HP
->Unit(benchmark::kNanosecond);
// Benchmark for bulk Pokemon creation
static void BM_BulkPokemonCreation(benchmark::State& state) {
int numPokemon = static_cast<int>(state.range(0));
for (auto _ : state) {
std::vector<PokEng::Pokemon> pokemonList;
// Create multiple Pokemon
for (int i = 0; i < numPokemon; ++i) {
pokemonList.emplace_back("Pokemon" + std::to_string(i), 100 + i * 10);
}
benchmark::DoNotOptimize(pokemonList);
benchmark::ClobberMemory(); // Ensure memory operations are not optimized away
}
}
BENCHMARK(BM_BulkPokemonCreation)
->Arg(10) // Create 10 Pokemon
->Arg(100) // Create 100 Pokemon
->Arg(1000) // Create 1000 Pokemon
->Unit(benchmark::kMicrosecond);
// Benchmark for Pokemon health modifications
static void BM_PokemonHealthOperations(benchmark::State& state) {
PokEng::Pokemon pokemon("TestPokemon", 100);
for (auto _ : state) {
// Perform various health operations
pokemon.setHealth(50);
int health = pokemon.getHealth();
pokemon.setHealth(health + 25);
std::string name = pokemon.getName();
benchmark::DoNotOptimize(health);
benchmark::DoNotOptimize(name);
}
}
BENCHMARK(BM_PokemonHealthOperations)
->Unit(benchmark::kNanosecond)
->Iterations(100000);
// Fixture for benchmarks that need setup/teardown
class PokemonFixture : public benchmark::Fixture {
public:
void SetUp(const benchmark::State& /*state*/) override {
// Set up Pokemon for each benchmark iteration
pokemon1 = std::make_unique<PokEng::Pokemon>("Pikachu", 100);
pokemon2 = std::make_unique<PokEng::Pokemon>("Charizard", 150);
}
void TearDown(const benchmark::State& /*state*/) override {
// Clean up after each benchmark iteration
pokemon1.reset();
pokemon2.reset();
}
protected:
std::unique_ptr<PokEng::Pokemon> pokemon1;
std::unique_ptr<PokEng::Pokemon> pokemon2;
};
// Benchmark using fixture
BENCHMARK_F(PokemonFixture, BM_BattleWithFixture)(benchmark::State& state) {
for (auto _ : state) {
PokEng::simulateBattle(*pokemon1, *pokemon2);
// Reset health
pokemon1->setHealth(100);
pokemon2->setHealth(150);
}
}
BENCHMARK_REGISTER_F(PokemonFixture, BM_BattleWithFixture)
->Unit(benchmark::kMicrosecond)
->Iterations(1000);