Skip to content

Commit

Permalink
benchmark: Add LRU cache benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jun 27, 2023
1 parent 1c52ce0 commit 253b05f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ 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(
benchmark
PRIVATE Catch2::Catch2WithMain
PRIVATE fmt::fmt
)
96 changes: 96 additions & 0 deletions benchmark/lc_lru_cache_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -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 <catch2/catch_test_macros.hpp>

#include <nanobench.h>

#include <nameof.hpp>

#include <fmt/format.h>

#include "forfun/lc_lru_cache.hpp"

template <typename T>
std::enable_if_t<std::is_base_of_v<forfun::lrucache::LRUCacheBase, 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<forfun::lrucache::stl::LRUCache>(lrucache_capacity);
})

.run(
NAMEOF_FULL_TYPE(forfun::lrucache::naive::LRUCache).data(),
[]() {
test<forfun::lrucache::naive::LRUCache>(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<forfun::lrucache::stl::LRUCache>(lrucache_capacity);
})

.run(
NAMEOF_FULL_TYPE(forfun::lrucache::naive::LRUCache).data(),
[]() {
test<forfun::lrucache::naive::LRUCache>(lrucache_capacity);
})

;
}
}
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": [
"catch2",
"fmt",
"nameof",
"nanobench"
]
Expand Down

0 comments on commit 253b05f

Please sign in to comment.