Skip to content

Commit c166ff5

Browse files
committed
Added more tests.
1 parent 556daaa commit c166ff5

File tree

7 files changed

+321
-13
lines changed

7 files changed

+321
-13
lines changed

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ Include(FetchContent)
22
FetchContent_Declare(
33
Catch2
44
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
5-
GIT_TAG v3.8.1 # or a later release
5+
GIT_TAG v3.8.1 # or a later release
66
)
77
FetchContent_MakeAvailable(Catch2)
88

99
set(PERF_CPP_TEST
1010
test/access_benchmark.cpp
11+
test/config.cpp
1112
test/counter_definition.cpp
1213
test/counter_result.cpp
1314
test/metric.cpp

test/config.cpp

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
#include <perfcpp/counter/config.hpp>
3+
#include <perfcpp/sample/config.hpp>
4+
5+
TEST_CASE("Config defaults", "[Config]")
6+
{
7+
const auto config = perf::Config{};
8+
9+
SECTION("boolean defaults")
10+
{
11+
REQUIRE_FALSE(config.is_include_child_threads());
12+
REQUIRE(config.is_include_kernel());
13+
REQUIRE(config.is_include_user());
14+
REQUIRE(config.is_include_hypervisor());
15+
REQUIRE(config.is_include_idle());
16+
REQUIRE(config.is_include_guest());
17+
REQUIRE(config.is_include_host());
18+
REQUIRE_FALSE(config.is_pinned());
19+
REQUIRE_FALSE(config.is_debug());
20+
}
21+
22+
SECTION("cpu core defaults to any")
23+
{
24+
REQUIRE(config.cpu_core() == perf::CpuCore::Any);
25+
REQUIRE(config.cpu_core().is_any());
26+
}
27+
28+
SECTION("process defaults to calling")
29+
{
30+
REQUIRE(config.process() == perf::Process::Calling);
31+
REQUIRE(config.process().is_calling());
32+
}
33+
}
34+
35+
TEST_CASE("Config setter roundtrips", "[Config]")
36+
{
37+
auto config = perf::Config{};
38+
39+
SECTION("include_child_threads")
40+
{
41+
config.include_child_threads(true);
42+
REQUIRE(config.is_include_child_threads());
43+
config.include_child_threads(false);
44+
REQUIRE_FALSE(config.is_include_child_threads());
45+
}
46+
47+
SECTION("include_kernel")
48+
{
49+
config.include_kernel(false);
50+
REQUIRE_FALSE(config.is_include_kernel());
51+
}
52+
53+
SECTION("include_user")
54+
{
55+
config.include_user(false);
56+
REQUIRE_FALSE(config.is_include_user());
57+
}
58+
59+
SECTION("include_hypervisor")
60+
{
61+
config.include_hypervisor(false);
62+
REQUIRE_FALSE(config.is_include_hypervisor());
63+
}
64+
65+
SECTION("include_idle")
66+
{
67+
config.include_idle(false);
68+
REQUIRE_FALSE(config.is_include_idle());
69+
}
70+
71+
SECTION("include_guest")
72+
{
73+
config.include_guest(false);
74+
REQUIRE_FALSE(config.is_include_guest());
75+
}
76+
77+
SECTION("include_host")
78+
{
79+
config.include_host(false);
80+
REQUIRE_FALSE(config.is_include_host());
81+
}
82+
83+
SECTION("pinned")
84+
{
85+
config.pinned(true);
86+
REQUIRE(config.is_pinned());
87+
}
88+
89+
SECTION("debug")
90+
{
91+
config.debug(true);
92+
REQUIRE(config.is_debug());
93+
}
94+
95+
SECTION("num_physical_counters")
96+
{
97+
config.num_physical_counters(2U);
98+
REQUIRE(config.num_physical_counters() == 2U);
99+
}
100+
101+
SECTION("num_events_per_physical_counter")
102+
{
103+
config.num_events_per_physical_counter(8U);
104+
REQUIRE(config.num_events_per_physical_counter() == 8U);
105+
}
106+
107+
SECTION("cpu_core from CpuCore")
108+
{
109+
config.cpu_core(perf::CpuCore{ std::uint16_t{ 5U } });
110+
REQUIRE(config.cpu_core() == std::uint16_t{ 5U });
111+
REQUIRE_FALSE(config.cpu_core().is_any());
112+
}
113+
114+
SECTION("cpu_core from integer")
115+
{
116+
config.cpu_core(3U);
117+
REQUIRE(config.cpu_core() == std::uint16_t{ 3U });
118+
}
119+
120+
SECTION("cpu_core revert to any")
121+
{
122+
config.cpu_core(5U);
123+
config.cpu_core(perf::CpuCore::Any);
124+
REQUIRE(config.cpu_core().is_any());
125+
}
126+
127+
SECTION("process from Process")
128+
{
129+
config.process(perf::Process{ 1337 });
130+
REQUIRE(config.process() == perf::Process{ 1337 });
131+
REQUIRE_FALSE(config.process().is_calling());
132+
REQUIRE_FALSE(config.process().is_any());
133+
}
134+
135+
SECTION("process from pid")
136+
{
137+
config.process(static_cast<pid_t>(42));
138+
REQUIRE(config.process() == 42);
139+
}
140+
141+
SECTION("process revert to any")
142+
{
143+
config.process(perf::Process{ 1337 });
144+
config.process(perf::Process::Any);
145+
REQUIRE(config.process().is_any());
146+
}
147+
}
148+
149+
TEST_CASE("Config with explicit counter limits", "[Config]")
150+
{
151+
const auto config = perf::Config{ 2U, 1U };
152+
153+
REQUIRE(config.num_physical_counters() == 2U);
154+
REQUIRE(config.num_events_per_physical_counter() == 1U);
155+
}
156+
157+
TEST_CASE("Process and CpuCore", "[Config]")
158+
{
159+
SECTION("Process statics are distinct")
160+
{
161+
REQUIRE_FALSE(perf::Process::Any == perf::Process::Calling);
162+
REQUIRE(perf::Process::Any.is_any());
163+
REQUIRE_FALSE(perf::Process::Any.is_calling());
164+
REQUIRE(perf::Process::Calling.is_calling());
165+
REQUIRE_FALSE(perf::Process::Calling.is_any());
166+
}
167+
168+
SECTION("Process from pid")
169+
{
170+
const auto process = perf::Process{ 1234 };
171+
REQUIRE(process == 1234);
172+
REQUIRE_FALSE(process.is_any());
173+
REQUIRE_FALSE(process.is_calling());
174+
}
175+
176+
SECTION("CpuCore any")
177+
{
178+
REQUIRE(perf::CpuCore::Any.is_any());
179+
}
180+
181+
SECTION("CpuCore from id")
182+
{
183+
const auto core = perf::CpuCore{ std::uint16_t{ 7U } };
184+
REQUIRE(core == std::uint16_t{ 7U });
185+
REQUIRE_FALSE(core.is_any());
186+
}
187+
}
188+
189+
TEST_CASE("SampleConfig defaults", "[SampleConfig]")
190+
{
191+
const auto config = perf::SampleConfig{};
192+
193+
SECTION("inherits Config defaults")
194+
{
195+
REQUIRE(config.is_include_kernel());
196+
REQUIRE(config.is_include_user());
197+
REQUIRE_FALSE(config.is_debug());
198+
}
199+
200+
SECTION("buffer pages")
201+
{
202+
REQUIRE(config.buffer_pages() == 4097U);
203+
}
204+
205+
SECTION("precision")
206+
{
207+
REQUIRE(config.precise_ip() == perf::Precision::MustHaveConstantSkid);
208+
}
209+
}
210+
211+
TEST_CASE("SampleConfig setter roundtrips", "[SampleConfig]")
212+
{
213+
auto config = perf::SampleConfig{};
214+
215+
SECTION("period")
216+
{
217+
config.period(50000U);
218+
REQUIRE(std::holds_alternative<perf::Period>(config.period_or_frequency()));
219+
REQUIRE(std::get<perf::Period>(config.period_or_frequency()).get() == 50000U);
220+
}
221+
222+
SECTION("frequency")
223+
{
224+
config.frequency(1000U);
225+
REQUIRE(std::holds_alternative<perf::Frequency>(config.period_or_frequency()));
226+
REQUIRE(std::get<perf::Frequency>(config.period_or_frequency()).get() == 1000U);
227+
}
228+
229+
SECTION("period and frequency are mutually exclusive")
230+
{
231+
config.period(50000U);
232+
config.frequency(1000U);
233+
REQUIRE(std::holds_alternative<perf::Frequency>(config.period_or_frequency()));
234+
}
235+
236+
SECTION("precision")
237+
{
238+
config.precision(perf::Precision::RequestZeroSkid);
239+
REQUIRE(config.precise_ip() == perf::Precision::RequestZeroSkid);
240+
}
241+
242+
SECTION("buffer_pages")
243+
{
244+
config.buffer_pages(8193U);
245+
REQUIRE(config.buffer_pages() == 8193U);
246+
}
247+
}

test/counter_definition.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,69 @@
11
#include <catch2/catch_test_macros.hpp>
22
#include <perfcpp/counter_definition.hpp>
33

4+
TEST_CASE("supports", "[CounterDefinition]")
5+
{
6+
const auto definition = perf::CounterDefinition{};
7+
8+
SECTION("built-in hardware events")
9+
{
10+
REQUIRE(definition.supports("instructions"));
11+
REQUIRE(definition.supports("cycles"));
12+
REQUIRE(definition.supports("cache-misses"));
13+
REQUIRE(definition.supports("cache-references"));
14+
REQUIRE(definition.supports("branches"));
15+
REQUIRE(definition.supports("branch-misses"));
16+
}
17+
18+
SECTION("built-in software events")
19+
{
20+
REQUIRE(definition.supports("cpu-clock"));
21+
REQUIRE(definition.supports("task-clock"));
22+
REQUIRE(definition.supports("page-faults"));
23+
REQUIRE(definition.supports("context-switches"));
24+
}
25+
26+
SECTION("built-in time events")
27+
{
28+
REQUIRE(definition.supports("seconds"));
29+
REQUIRE(definition.supports("milliseconds"));
30+
REQUIRE(definition.supports("microseconds"));
31+
REQUIRE(definition.supports("nanoseconds"));
32+
}
33+
34+
SECTION("built-in metrics")
35+
{
36+
REQUIRE(definition.supports("cycles-per-instruction"));
37+
REQUIRE(definition.supports("instructions-per-cycle"));
38+
REQUIRE(definition.supports("cache-hit-ratio"));
39+
REQUIRE(definition.supports("cache-miss-ratio"));
40+
}
41+
42+
SECTION("unknown event")
43+
{
44+
REQUIRE_FALSE(definition.supports("does-not-exist"));
45+
REQUIRE_FALSE(definition.supports(""));
46+
}
47+
48+
SECTION("user-added event")
49+
{
50+
auto extended = perf::CounterDefinition{};
51+
REQUIRE_FALSE(extended.supports("my-custom-event"));
52+
53+
extended.add("my-custom-event", 0x1234);
54+
REQUIRE(extended.supports("my-custom-event"));
55+
}
56+
57+
SECTION("user-added metric")
58+
{
59+
auto extended = perf::CounterDefinition{};
60+
REQUIRE_FALSE(extended.supports("my-custom-metric"));
61+
62+
extended.add("my-custom-metric", "cycles / instructions");
63+
REQUIRE(extended.supports("my-custom-metric"));
64+
}
65+
}
66+
467
TEST_CASE("adding new events and metrics", "[CounterDefinition]")
568
{
669
auto definition = perf::CounterDefinition{};

test/counter_result.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ TEST_CASE("access", "[CounterResult]")
1717

1818
SECTION("get by name")
1919
{
20-
auto result = perf::CounterResult{
21-
std::vector<std::pair<std::string_view, double>>{ { "instructions", 100.0 }, { "cycles", 200.0 } }
22-
};
20+
auto result = perf::CounterResult{ std::vector<std::pair<std::string_view, double>>{ { "instructions", 100.0 },
21+
{ "cycles", 200.0 } } };
2322

2423
REQUIRE(result.get("instructions").has_value());
2524
REQUIRE(result.get("instructions").value() == 100.0);
@@ -30,8 +29,7 @@ TEST_CASE("access", "[CounterResult]")
3029

3130
SECTION("get with package prefix")
3231
{
33-
auto result =
34-
perf::CounterResult{ std::vector<std::pair<std::string_view, double>>{ { "cycles", 42.0 } } };
32+
auto result = perf::CounterResult{ std::vector<std::pair<std::string_view, double>>{ { "cycles", 42.0 } } };
3533

3634
/// Single slash triggers fallback: "cpu/cycles" -> "cycles".
3735
REQUIRE(result.get("cpu/cycles").has_value());
@@ -46,9 +44,8 @@ TEST_CASE("access", "[CounterResult]")
4644

4745
SECTION("operator[]")
4846
{
49-
auto result = perf::CounterResult{
50-
std::vector<std::pair<std::string_view, double>>{ { "instructions", 100.0 }, { "cycles", 200.0 } }
51-
};
47+
auto result = perf::CounterResult{ std::vector<std::pair<std::string_view, double>>{ { "instructions", 100.0 },
48+
{ "cycles", 200.0 } } };
5249

5350
REQUIRE(result["instructions"].has_value());
5451
REQUIRE(result["instructions"].value() == 100.0);

test/event_counter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ TEST_CASE("counter scheduling", "[EventCounter]")
106106
auto counter_definition = perf::CounterDefinition{};
107107
counter_definition.add("non-existing", "cycles", 10000U, 10000U);
108108

109-
auto event_counter = perf::EventCounter{counter_definition};
109+
auto event_counter = perf::EventCounter{ counter_definition };
110110

111111
event_counter.add(std::vector<std::string>{ "cpu/cycles" }, perf::EventCounter::Schedule::Separate);
112112

test/metric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <catch2/catch_test_macros.hpp>
2-
#include <perfcpp/metric/metric.hpp>
32
#include <perfcpp/counter/requested_event.hpp>
3+
#include <perfcpp/metric/metric.hpp>
44

55
TEST_CASE("calculating", "[Metric][CyclesPerInstruction]")
66
{

test/requested_event.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <catch2/catch_test_macros.hpp>
2-
#include <perfcpp/counter_definition.hpp>
3-
#include <perfcpp/counter/result.hpp>
42
#include <perfcpp/counter/requested_event.hpp>
3+
#include <perfcpp/counter/result.hpp>
4+
#include <perfcpp/counter_definition.hpp>
55

66
TEST_CASE("empty RequestedEventSet", "[RequestedEventSet]")
77
{

0 commit comments

Comments
 (0)