Add benchmarks for RefId serialization and deserialization

depth-refraction
elsid 2 years ago
parent a762624581
commit 30fd8c9d23
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -50,6 +50,7 @@ variables:
- if [[ "${BUILD_TESTS_ONLY}" ]]; then ./openmw_test_suite --gtest_output="xml:openmw_tests.xml"; fi
- if [[ "${BUILD_TESTS_ONLY}" ]]; then ./openmw-cs-tests --gtest_output="xml:openmw_cs_tests.xml"; fi
- if [[ "${BUILD_TESTS_ONLY}" && ! "${BUILD_WITH_CODE_COVERAGE}" ]]; then ./openmw_detournavigator_navmeshtilescache_benchmark; fi
- if [[ "${BUILD_TESTS_ONLY}" && ! "${BUILD_WITH_CODE_COVERAGE}" ]]; then ./openmw_esm_refid_benchmark; fi
- ccache -s
- df -h
- if [[ "${BUILD_WITH_CODE_COVERAGE}" ]]; then gcovr --xml-pretty --exclude-unreachable-branches --print-summary --root "${CI_PROJECT_DIR}" -j $(nproc) -o ../coverage.xml; fi

@ -3,3 +3,4 @@ if(OPENMW_USE_SYSTEM_BENCHMARK)
endif()
add_subdirectory(detournavigator)
add_subdirectory(esm)

@ -0,0 +1,15 @@
openmw_add_executable(openmw_esm_refid_benchmark benchrefid.cpp)
target_link_libraries(openmw_esm_refid_benchmark benchmark::benchmark components)
if (UNIX AND NOT APPLE)
target_link_libraries(openmw_esm_refid_benchmark ${CMAKE_THREAD_LIBS_INIT})
endif()
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.16 AND MSVC)
target_precompile_headers(openmw_esm_refid_benchmark PRIVATE <algorithm>)
endif()
if (BUILD_WITH_CODE_COVERAGE)
target_compile_options(openmw_esm_refid_benchmark PRIVATE --coverage)
target_link_libraries(openmw_esm_refid_benchmark gcov)
endif()

@ -0,0 +1,82 @@
#include <benchmark/benchmark.h>
#include "components/esm/refid.hpp"
#include <algorithm>
#include <cstddef>
#include <random>
#include <string>
#include <vector>
namespace
{
constexpr std::size_t refIdsCount = 64 * 1024;
template <class Random>
std::string generateText(std::size_t size, Random& random)
{
std::uniform_int_distribution<int> distribution('A', 'z');
std::string result;
result.reserve(size);
std::generate_n(std::back_inserter(result), size, [&] { return distribution(random); });
return result;
}
template <class Random>
std::vector<ESM::RefId> generateStringRefIds(std::size_t size, Random& random)
{
std::vector<ESM::RefId> result;
result.reserve(refIdsCount);
std::generate_n(
std::back_inserter(result), refIdsCount, [&] { return ESM::StringRefId(generateText(size, random)); });
return result;
}
template <class Serialize>
std::vector<std::string> generateSerializedRefIds(const std::vector<ESM::RefId>& generated, Serialize&& serialize)
{
std::vector<std::string> result;
result.reserve(generated.size());
for (ESM::RefId refId : generated)
result.push_back(serialize(refId));
return result;
}
template <class Random, class Serialize>
std::vector<std::string> generateSerializedStringRefIds(std::size_t size, Random& random, Serialize&& serialize)
{
return generateSerializedRefIds(generateStringRefIds(size, random), serialize);
}
void serializeRefId(benchmark::State& state)
{
std::minstd_rand random;
std::vector<ESM::RefId> 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<std::string> 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;
}
}
}
BENCHMARK(serializeRefId)->RangeMultiplier(4)->Range(8, 64);
BENCHMARK(deserializeRefId)->RangeMultiplier(4)->Range(8, 64);
BENCHMARK_MAIN();
Loading…
Cancel
Save