Skip to content

Commit 3c6e731

Browse files
committed
add test cases for perf event integration
1 parent 94c6015 commit 3c6e731

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

test/cpp-utility/perf/CounterTest.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "PerfTest.hpp"
2+
#include <catch2/catch.hpp>
3+
#include <cpp-utility/perf/Counter.hpp>
4+
#include <cpp-utility/perf/PerfEvent.hpp>
5+
#include <iostream>
6+
7+
using namespace utility::perf;
8+
9+
TEST_CASE("Perf Event") {
10+
const size_t n = 1e6;
11+
12+
std::unordered_map<Counter, double> oracle{{INSTRUCTIONS, NAN}, {CYCLES, NAN}, {BRANCH_MISSES, NAN},
13+
{IPC, NAN}, {DURATION, 0}, {CPUS, NAN}};
14+
std::vector<Counter> counters{};
15+
16+
SECTION("instructions") {
17+
counters = {INSTRUCTIONS};
18+
oracle[INSTRUCTIONS] = n;
19+
}
20+
SECTION("instructions, cycles") {
21+
counters = {INSTRUCTIONS, CYCLES};
22+
oracle[INSTRUCTIONS] = n;
23+
oracle[CYCLES] = n;
24+
oracle[IPC] = 0;
25+
}
26+
SECTION("ipc") {
27+
counters = {INSTRUCTIONS, CYCLES};
28+
oracle[INSTRUCTIONS] = n;
29+
oracle[CYCLES] = n;
30+
oracle[IPC] = 0;
31+
}
32+
SECTION("duration") {
33+
counters = {DURATION};
34+
oracle[DURATION] = n;
35+
}
36+
SECTION("cpus") {
37+
counters = {CPUS};
38+
oracle[DURATION] = n;
39+
oracle[CPUS] = 0;
40+
}
41+
42+
PerfEvent event(counters);
43+
event.start();
44+
work(1e6);
45+
event.stop();
46+
47+
for (auto value : oracle) {
48+
auto result = event.get(value.first);
49+
REQUIRE(((std::isnan(result) and std::isnan(value.second)) or result > value.second));
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "PerfTest.hpp"
2+
#include <catch2/catch.hpp>
3+
#include <cpp-utility/perf/PerfCounter.hpp>
4+
#include <iostream>
5+
#include <linux/perf_event.h>
6+
7+
using namespace utility::perf;
8+
9+
TEST_CASE("Perf Counter") {
10+
perf_hw_id eventId = PERF_COUNT_HW_MAX;
11+
12+
SECTION("instructions") {
13+
eventId = PERF_COUNT_HW_INSTRUCTIONS;
14+
}
15+
SECTION("cycles") {
16+
eventId = PERF_COUNT_HW_CPU_CYCLES;
17+
}
18+
19+
PerfCounter counter(std::make_pair(PERF_TYPE_HARDWARE, eventId));
20+
counter.start();
21+
work(1e6);
22+
counter.stop();
23+
REQUIRE(counter.get() > 1e6);
24+
}

test/cpp-utility/perf/PerfTest.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
5+
namespace utility::perf {
6+
7+
static int work(size_t n) {
8+
volatile int counter = 0;
9+
for (size_t i = 0; i < n; i++) {
10+
counter = counter + 1;
11+
}
12+
return counter;
13+
}
14+
15+
} // namespace utility::perf

0 commit comments

Comments
 (0)