Skip to content

Commit

Permalink
add test cases for perf event integration
Browse files Browse the repository at this point in the history
  • Loading branch information
toschmidt committed Mar 10, 2021
1 parent 94c6015 commit 3c6e731
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/cpp-utility/perf/CounterTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "PerfTest.hpp"
#include <catch2/catch.hpp>
#include <cpp-utility/perf/Counter.hpp>
#include <cpp-utility/perf/PerfEvent.hpp>
#include <iostream>

using namespace utility::perf;

TEST_CASE("Perf Event") {
const size_t n = 1e6;

std::unordered_map<Counter, double> oracle{{INSTRUCTIONS, NAN}, {CYCLES, NAN}, {BRANCH_MISSES, NAN},
{IPC, NAN}, {DURATION, 0}, {CPUS, NAN}};
std::vector<Counter> counters{};

SECTION("instructions") {
counters = {INSTRUCTIONS};
oracle[INSTRUCTIONS] = n;
}
SECTION("instructions, cycles") {
counters = {INSTRUCTIONS, CYCLES};
oracle[INSTRUCTIONS] = n;
oracle[CYCLES] = n;
oracle[IPC] = 0;
}
SECTION("ipc") {
counters = {INSTRUCTIONS, CYCLES};
oracle[INSTRUCTIONS] = n;
oracle[CYCLES] = n;
oracle[IPC] = 0;
}
SECTION("duration") {
counters = {DURATION};
oracle[DURATION] = n;
}
SECTION("cpus") {
counters = {CPUS};
oracle[DURATION] = n;
oracle[CPUS] = 0;
}

PerfEvent event(counters);
event.start();
work(1e6);
event.stop();

for (auto value : oracle) {
auto result = event.get(value.first);
REQUIRE(((std::isnan(result) and std::isnan(value.second)) or result > value.second));
}
}
24 changes: 24 additions & 0 deletions test/cpp-utility/perf/PerfCounterTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "PerfTest.hpp"
#include <catch2/catch.hpp>
#include <cpp-utility/perf/PerfCounter.hpp>
#include <iostream>
#include <linux/perf_event.h>

using namespace utility::perf;

TEST_CASE("Perf Counter") {
perf_hw_id eventId = PERF_COUNT_HW_MAX;

SECTION("instructions") {
eventId = PERF_COUNT_HW_INSTRUCTIONS;
}
SECTION("cycles") {
eventId = PERF_COUNT_HW_CPU_CYCLES;
}

PerfCounter counter(std::make_pair(PERF_TYPE_HARDWARE, eventId));
counter.start();
work(1e6);
counter.stop();
REQUIRE(counter.get() > 1e6);
}
15 changes: 15 additions & 0 deletions test/cpp-utility/perf/PerfTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <cstddef>

namespace utility::perf {

static int work(size_t n) {
volatile int counter = 0;
for (size_t i = 0; i < n; i++) {
counter = counter + 1;
}
return counter;
}

} // namespace utility::perf

0 comments on commit 3c6e731

Please sign in to comment.