-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_performance.cpp
More file actions
104 lines (90 loc) · 2.18 KB
/
test_performance.cpp
File metadata and controls
104 lines (90 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <gtest/gtest.h>
#include "public/perfmon.h"
#include <atomic>
#include <thread>
namespace {
std::atomic<int> global_gcd_1_result(0);
std::atomic<int> global_gcd_2_result(0);
std::atomic<int> global_gcd_4_result(0);
int N = 2 * 1024;
int gcd(int a, int b) {
for (;;) {
if (a == 0) {
return b;
}
b %= a;
if (b == 0) {
return a;
}
a %= b;
}
}
void gcd_1_thread() {
for (int t = 0; t < 4; ++t) {
PERFMON_SCOPE("gcd_1_thread");
int result = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
PERFMON_SCOPE("congestion_point");
result += gcd(i, j);
}
}
global_gcd_1_result += result;
}
}
void gcd_2_thread() {
for (int t = 0; t < 2; ++t) {
PERFMON_SCOPE("gcd_2_thread");
int result = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
PERFMON_SCOPE("congestion_point");
result += gcd(i, j);
}
}
global_gcd_2_result += result;
}
}
void gcd_4_thread() {
PERFMON_SCOPE("gcd_4_thread");
int result = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
PERFMON_SCOPE("congestion_point");
result += gcd(i, j);
}
}
global_gcd_4_result += result;
}
} // namespace
TEST(Performance, Gcd) {
{ // 1 thread
std::thread t1(gcd_1_thread);
t1.join();
}
{ // 2 threads
std::thread t1(gcd_2_thread);
std::thread t2(gcd_2_thread);
t1.join();
t2.join();
}
{ // 4 threads
std::thread t1(gcd_4_thread);
std::thread t2(gcd_4_thread);
std::thread t3(gcd_4_thread);
std::thread t4(gcd_4_thread);
t1.join();
t2.join();
t3.join();
t4.join();
}
EXPECT_EQ(global_gcd_1_result, global_gcd_2_result);
EXPECT_EQ(global_gcd_1_result, global_gcd_4_result);
const auto counters = PERFMON_COUNTERS();
EXPECT_EQ(counters["gcd_1_thread"].Calls(), counters["gcd_2_thread"].Calls());
EXPECT_EQ(counters["gcd_1_thread"].Calls(), counters["gcd_4_thread"].Calls());
EXPECT_GT(counters["gcd_1_thread"].Ticks(),
0.8 * counters["gcd_2_thread"].Ticks());
EXPECT_GT(counters["gcd_2_thread"].Ticks(),
0.8 * counters["gcd_4_thread"].Ticks());
}