#include #include "components/esm/refid.hpp" #include #include #include #include #include namespace { constexpr std::size_t refIdsCount = 64 * 1024; template std::string generateText(std::size_t size, Random& random) { std::uniform_int_distribution distribution('A', 'z'); std::string result; result.reserve(size); std::generate_n(std::back_inserter(result), size, [&] { return distribution(random); }); return result; } template std::vector generateStringRefIds(std::size_t size, Random& random) { std::vector result; result.reserve(refIdsCount); std::generate_n( std::back_inserter(result), refIdsCount, [&] { return ESM::StringRefId(generateText(size, random)); }); return result; } template std::vector generateSerializedRefIds(const std::vector& generated, Serialize&& serialize) { std::vector result; result.reserve(generated.size()); for (ESM::RefId refId : generated) result.push_back(serialize(refId)); return result; } template std::vector generateSerializedStringRefIds(std::size_t size, Random& random, Serialize&& serialize) { return generateSerializedRefIds(generateStringRefIds(size, random), serialize); } template std::vector generateIndexRefIds(Random& random) { std::vector result; result.reserve(refIdsCount); std::uniform_int_distribution distribution(0, std::numeric_limits::max()); std::generate_n(std::back_inserter(result), refIdsCount, [&] { return ESM::IndexRefId(ESM::REC_ARMO, distribution(random)); }); return result; } template std::vector generateSerializedIndexRefIds(Random& random, Serialize&& serialize) { return generateSerializedRefIds(generateIndexRefIds(random), serialize); } template std::vector generateGeneratedRefIds(Random& random) { std::vector result; result.reserve(refIdsCount); std::uniform_int_distribution distribution(0, std::numeric_limits::max()); std::generate_n( std::back_inserter(result), refIdsCount, [&] { return ESM::GeneratedRefId(distribution(random)); }); return result; } template std::vector generateSerializedGeneratedRefIds(Random& random, Serialize&& serialize) { return generateSerializedRefIds(generateGeneratedRefIds(random), serialize); } template std::vector generateESM3ExteriorCellRefIds(Random& random) { std::vector result; result.reserve(refIdsCount); std::uniform_int_distribution distribution(-100, 100); std::generate_n(std::back_inserter(result), refIdsCount, [&] { return ESM::ESM3ExteriorCellRefId(distribution(random), distribution(random)); }); return result; } template std::vector generateSerializedESM3ExteriorCellRefIds(Random& random, Serialize&& serialize) { return generateSerializedRefIds(generateESM3ExteriorCellRefIds(random), serialize); } void serializeRefId(benchmark::State& state) { std::minstd_rand random; std::vector refIds = generateStringRefIds(state.range(0), random); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(refIds[i].serialize()); if (++i >= refIds.size()) i = 0; } } void deserializeRefId(benchmark::State& state) { std::minstd_rand random; std::vector serializedRefIds = generateSerializedStringRefIds(state.range(0), random, [](ESM::RefId v) { return v.serialize(); }); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(ESM::RefId::deserialize(serializedRefIds[i])); if (++i >= serializedRefIds.size()) i = 0; } } void serializeTextStringRefId(benchmark::State& state) { std::minstd_rand random; std::vector refIds = generateStringRefIds(state.range(0), random); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(refIds[i].serializeText()); if (++i >= refIds.size()) i = 0; } } void deserializeTextStringRefId(benchmark::State& state) { std::minstd_rand random; std::vector serializedRefIds = generateSerializedStringRefIds(state.range(0), random, [](ESM::RefId v) { return v.serializeText(); }); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(ESM::RefId::deserializeText(serializedRefIds[i])); if (++i >= serializedRefIds.size()) i = 0; } } void serializeTextGeneratedRefId(benchmark::State& state) { std::minstd_rand random; std::vector refIds = generateGeneratedRefIds(random); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(refIds[i].serializeText()); if (++i >= refIds.size()) i = 0; } } void deserializeTextGeneratedRefId(benchmark::State& state) { std::minstd_rand random; std::vector serializedRefIds = generateSerializedGeneratedRefIds(random, [](ESM::RefId v) { return v.serializeText(); }); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(ESM::RefId::deserializeText(serializedRefIds[i])); if (++i >= serializedRefIds.size()) i = 0; } } void serializeTextIndexRefId(benchmark::State& state) { std::minstd_rand random; std::vector refIds = generateIndexRefIds(random); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(refIds[i].serializeText()); if (++i >= refIds.size()) i = 0; } } void deserializeTextIndexRefId(benchmark::State& state) { std::minstd_rand random; std::vector serializedRefIds = generateSerializedIndexRefIds(random, [](ESM::RefId v) { return v.serializeText(); }); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(ESM::RefId::deserializeText(serializedRefIds[i])); if (++i >= serializedRefIds.size()) i = 0; } } void serializeTextESM3ExteriorCellRefId(benchmark::State& state) { std::minstd_rand random; std::vector refIds = generateESM3ExteriorCellRefIds(random); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(refIds[i].serializeText()); if (++i >= refIds.size()) i = 0; } } void deserializeTextESM3ExteriorCellRefId(benchmark::State& state) { std::minstd_rand random; std::vector serializedRefIds = generateSerializedESM3ExteriorCellRefIds(random, [](ESM::RefId v) { return v.serializeText(); }); std::size_t i = 0; for (auto _ : state) { benchmark::DoNotOptimize(ESM::RefId::deserializeText(serializedRefIds[i])); if (++i >= serializedRefIds.size()) i = 0; } } } BENCHMARK(serializeRefId)->RangeMultiplier(4)->Range(8, 64); BENCHMARK(deserializeRefId)->RangeMultiplier(4)->Range(8, 64); BENCHMARK(serializeTextStringRefId)->RangeMultiplier(4)->Range(8, 64); BENCHMARK(deserializeTextStringRefId)->RangeMultiplier(4)->Range(8, 64); BENCHMARK(serializeTextGeneratedRefId); BENCHMARK(deserializeTextGeneratedRefId); BENCHMARK(serializeTextIndexRefId); BENCHMARK(deserializeTextIndexRefId); BENCHMARK(serializeTextESM3ExteriorCellRefId); BENCHMARK(deserializeTextESM3ExteriorCellRefId); BENCHMARK_MAIN();