Skip to content

Commit

Permalink
Adding perf test
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfault1602 committed Dec 3, 2023
1 parent e0201fb commit 8843cb2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)

add_subdirectory(perf)

set(TEST_SOURCES
main_tests.cpp
basic_oscillators_tests.cpp
Expand Down
17 changes: 17 additions & 0 deletions tests/perf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FetchContent_Declare(
nanobench
GIT_REPOSITORY https://github.com/martinus/nanobench.git
GIT_TAG v4.1.0
GIT_SHALLOW TRUE)

FetchContent_Declare(
doctest
GIT_REPOSITORY https://github.com/doctest/doctest.git
GIT_TAG v2.4.11
GIT_SHALLOW TRUE)

FetchContent_MakeAvailable(nanobench doctest)

add_executable(perf_tests perf_tests.cpp basicosc_perf.cpp)
target_include_directories(perf_tests PRIVATE ${doctest_SOURCE_DIR}/doctest)
target_link_libraries(perf_tests PRIVATE nanobench dsp doctest)
49 changes: 49 additions & 0 deletions tests/perf/basicosc_perf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "doctest.h"
#include "nanobench.h"
#include <chrono>

#include "basic_oscillators.h"

using namespace ankerl;
using namespace std::chrono_literals;

constexpr size_t kSamplerate = 48000;
constexpr size_t kOutputSize = kSamplerate * 5;
constexpr float kFreq = 750;
constexpr size_t kBlockSize = 512;

// Render 5 second of audio
TEST_CASE("Sine")
{
sfdsp::BasicOscillator osc;
osc.Init(kSamplerate, kFreq, sfdsp::OscillatorType::Sine);
auto out = std::make_unique<float[]>(kOutputSize);

nanobench::Bench bench;
bench.title("BasicOscillator (Sine)");
bench.relative(true);
bench.minEpochIterations(5);
bench.timeUnit(1ms, "ms");

bench.run("BasicOscillator::ProcessBlock (Sine)", [&]() {
const size_t block_count = kOutputSize / kBlockSize;
float* write_ptr = out.get();
for (size_t i = 0; i < block_count; ++i)
{
osc.ProcessBlock(write_ptr, kBlockSize);
write_ptr += kBlockSize;
}

// Process reminder
const size_t reminder = kOutputSize % kBlockSize;
osc.ProcessBlock(write_ptr, reminder);
});

sfdsp::BasicOscillator osc2;
osc2.Init(kSamplerate, kFreq, sfdsp::OscillatorType::Sine);

bench.run("BasicOscillator::Tick (Sine)", [&]() {
for (auto i = 0; i < kOutputSize; ++i)
out[i] = osc2.Tick();
});
}
2 changes: 2 additions & 0 deletions tests/perf/perf_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

0 comments on commit 8843cb2

Please sign in to comment.