From 67282cdf47428c88d626e38176986d900c6c0955 Mon Sep 17 00:00:00 2001 From: Omar Boukli-Hacene Date: Tue, 27 Jun 2023 03:07:18 +0200 Subject: [PATCH] benchmark: Add LRU Cache benchmarking --- CMakeLists.txt | 2 + benchmark/lc_lru_cache_benchmark.cpp | 96 ++++++++++++++++++++++++++++ vcpkg.json | 1 + 3 files changed, 99 insertions(+) create mode 100644 benchmark/lc_lru_cache_benchmark.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bf8aabf..f0e0e57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,11 +74,13 @@ add_executable( ) target_link_libraries(tests PRIVATE Catch2::Catch2WithMain) +find_package(fmt CONFIG REQUIRED) find_package(nameof CONFIG REQUIRED) find_package(nanobench CONFIG REQUIRED) add_executable( benchmark "benchmark/benchmark.cpp" + "benchmark/lc_lru_cache_benchmark.cpp" "benchmark/palindrome_benchmark.cpp" ) target_link_libraries( diff --git a/benchmark/lc_lru_cache_benchmark.cpp b/benchmark/lc_lru_cache_benchmark.cpp new file mode 100644 index 0000000..7177e0b --- /dev/null +++ b/benchmark/lc_lru_cache_benchmark.cpp @@ -0,0 +1,96 @@ +// Copyright (c) Omar Boukli-Hacene. All rights reserved. +// Distributed under an MIT-style license that can be +// found in the LICENSE file. + +// SPDX-License-Identifier: MIT + +#include + +#include + +#include + +#include + +#include "forfun/lc_lru_cache.hpp" + +template +std::enable_if_t, void> +test(int const capacity) { + int volatile val{}; + T cache(capacity); + + for (int i{0}; i < capacity; ++i) { + cache.put(i, i); + } + + val = cache.get(1); + + val = cache.get(2); + + val = cache.get(3); + + cache.put(capacity + 1, capacity + 1); + + val = cache.get(1); + + val = cache.get(4); + + val = cache.get(2); + + cache.put(5, 5); + + val = cache.get(3); + + for (int i{0}; i < capacity; ++i) { + val = cache.get(i); + } +} + +TEST_CASE("forfun::lrucache benchmarking") { + SECTION("small") { + constexpr int const lrucache_capacity{32}; + + ankerl::nanobench::Bench() + + .title( + fmt::format("LRU Cache with {} cache items", lrucache_capacity)) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::stl::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::naive::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + ; + } + + SECTION("large") { + constexpr int const lrucache_capacity{128}; + + ankerl::nanobench::Bench() + + .title( + fmt::format("LRU Cache with {} cache items", lrucache_capacity)) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::stl::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + .run( + NAMEOF_FULL_TYPE(forfun::lrucache::naive::LRUCache).data(), + []() { + test(lrucache_capacity); + }) + + ; + } +} diff --git a/vcpkg.json b/vcpkg.json index afa2d39..6ec40b4 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,6 +1,7 @@ { "dependencies": [ "catch2", + "fmt", "nameof", "nanobench" ]