Skip to content

Commit 62b88ad

Browse files
committed
Added Google Benchmark
1 parent 7247c5b commit 62b88ad

10 files changed

Lines changed: 245 additions & 117 deletions

File tree

3rd-party/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ add_custom_target(
2121
)
2222

2323
add_dependencies(sbgraph 3rd-party-libs)
24+
25+
include(FetchContent)
26+
27+
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "" FORCE)
28+
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
29+
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
30+
31+
FetchContent_Declare(
32+
google_benchmark
33+
GIT_REPOSITORY https://github.com/google/benchmark.git
34+
GIT_TAG v1.6.1
35+
)
36+
FetchContent_MakeAvailable(google_benchmark)

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ add_custom_target(test
2323
COMMAND echo "Running Tests."
2424
)
2525

26-
add_dependencies(test run-eval-test run-parser-test run-partitioner-test run-performance-test)
26+
add_dependencies(test run-eval-test run-parser-test run-partitioner-test)

test/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,3 @@ and actual results).
3838
## eval
3939

4040
Analogous to parser module, for the evaluator.
41-
42-
## Compilation and execution
43-
44-
Script /test/compile_run_tests.sh builds and runs all tests if no argument is
45-
specified. It also accepts one argument to run an individual suite such as:
46-
`performance`, `parser` or `eval`.

test/Test

Whitespace-only changes.

test/performance/CMakeLists.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,33 @@ set_target_properties(sbg-performance PROPERTIES
3737
)
3838

3939
add_custom_target(run-performance-test
40-
COMMAND echo "Running Partitioner Tests."
40+
COMMAND echo "Running Performance Tests."
4141
COMMAND cd ${BIN_DIR} && ./sbg-performance
4242
)
4343

4444
add_dependencies(run-performance-test sbg-performance)
45+
46+
# Benchmark executable
47+
add_executable(sbg-benchmark benchmark_main.cpp)
48+
49+
target_sources(sbg-benchmark
50+
PRIVATE
51+
benchmark_main.cpp
52+
ord_set_bm.cpp)
53+
54+
set_target_properties(sbg-benchmark PROPERTIES
55+
RUNTIME_OUTPUT_DIRECTORY
56+
${BIN_DIR}
57+
)
58+
59+
add_custom_target(run-benchmark
60+
COMMAND echo "Running SBG Benchmark."
61+
COMMAND cd ${BIN_DIR} && ./sbg-benchmark
62+
)
63+
64+
target_link_libraries(sbg-benchmark
65+
PUBLIC
66+
benchmark::benchmark
67+
PRIVATE
68+
sbg-dev
69+
sbg-eval-lib)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*****************************************************************************
2+
3+
This file is part of Set--Based Graph Library.
4+
5+
SBG Library is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
SBG Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
17+
18+
******************************************************************************/
19+
20+
#include <benchmark/benchmark.h>
21+
22+
BENCHMARK_MAIN();

test/performance/boost/ordinary_graph_builder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SBG::LIB::MD_NAT nextElem(SBG::LIB::MD_NAT curr, SBG::LIB::SetPiece mdi)
3232
else {
3333
res.emplaceBack(curr[j] + 1);
3434
for (unsigned int k = 1; k < mdi.arity() - j; ++k)
35-
res.emplaceBack(curr[j]);
35+
res.emplaceBack(curr[j + k]);
3636
break;
3737
}
3838
}

test/performance/dom_ord_pwmap_perf.cpp

Lines changed: 32 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -34,70 +34,16 @@ using SBG::LIB::Exp;
3434
using SBG::LIB::Map;
3535
using SBG::LIB::PWMap;
3636

37-
////////////////////////////////////////////////////////////////////////////////
38-
// Set operations --------------------------------------------------------------
39-
////////////////////////////////////////////////////////////////////////////////
40-
41-
TEST(DomOrdPWPerf, OrdDifference)
42-
{
43-
int N = 10000;
44-
45-
SBG::Eval::setSetFactory(1);
46-
47-
Set s1 = SBG::LIB::SET_FACT.createSet();
48-
Set s2 = SBG::LIB::SET_FACT.createSet();
49-
for (int j = 0; j < N; ++j) {
50-
Interval i(j*100+1, 1, (j+1)*100);
51-
s1.emplaceBack(i);
52-
}
53-
54-
for (int j = 0; j < N; ++j) {
55-
Interval i(j*105+1, 1, (j+1)*105);
56-
s2.emplaceBack(i);
57-
}
58-
59-
auto start = std::chrono::high_resolution_clock::now();
60-
s1.difference(s2);
61-
auto end = std::chrono::high_resolution_clock::now();
62-
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
63-
std::cout << "ORDERED DIFFERENCE TEST elapsed time: " << elapsed.count() << "ms\n";
64-
65-
SUCCEED();
66-
}
67-
68-
TEST(DomOrdPWPerf, OrdDisjointUnion)
69-
{
70-
int N = 10000;
71-
72-
SBG::Eval::setSetFactory(1);
73-
74-
Set s1 = SBG::LIB::SET_FACT.createSet();
75-
Set s2 = SBG::LIB::SET_FACT.createSet();
76-
for (int j = 0; j < N; j+=2) {
77-
Interval i(j*100+1, 1, (j+1)*100);
78-
s1.emplaceBack(i);
79-
Interval i2((j+1)*100+1, 1, (j+2)*100);
80-
s2.emplaceBack(i2);
81-
}
82-
83-
auto start = std::chrono::high_resolution_clock::now();
84-
s1.disjointCup(s2);
85-
auto end = std::chrono::high_resolution_clock::now();
86-
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
87-
std::cout << "ORDERED DISJOINT UNION TEST elapsed time: " << elapsed.count() << "ms\n";
88-
89-
SUCCEED();
90-
}
91-
92-
////////////////////////////////////////////////////////////////////////////////
93-
// PWMap operations ------------------------------------------------------------
94-
////////////////////////////////////////////////////////////////////////////////
95-
37+
/**
38+
* @class DomOrdPWPerf
39+
* @brief Test suite created to analyze the time growth of the different domain
40+
* ordered PWs operations.
41+
*/
9642
class DomOrdPWPerf : public ::testing::TestWithParam<SBG::LIB::NAT> {
9743
protected:
98-
std::pair<PWMap, PWMap> quadraticTest(SBG::LIB::NAT map_sz) {
44+
std::pair<PWMap, PWMap> contiguousMaps(SBG::LIB::NAT map_sz) {
9945
SBG::LIB::NAT inter_sz = 100;
100-
SBG::LIB::NAT set_sz = 1;
46+
SBG::LIB::NAT set_sz = 100;
10147

10248
Interval second_dim(0, 1, inter_sz - 1);
10349
PWMap pw1 = SBG::LIB::PW_FACT.createPWMap();
@@ -139,16 +85,14 @@ TEST_P(DomOrdPWPerf, DomOrdEq)
13985

14086
SBG::LIB::NAT map_sz = GetParam();
14187
bool result = false;
142-
if (map_sz < 10000) {
143-
auto [pw1, pw2] = quadraticTest(map_sz);
144-
145-
auto start = std::chrono::high_resolution_clock::now();
146-
result = pw1 == pw2;
147-
auto end = std::chrono::high_resolution_clock::now();
148-
auto curr_time = std::chrono::duration_cast<std::chrono::milliseconds>(
149-
end - start);
150-
std::cout << curr_time.count() << "\n";
151-
}
88+
89+
auto [pw1, pw2] = contiguousMaps(map_sz);
90+
auto start = std::chrono::high_resolution_clock::now();
91+
result = pw1 == pw2;
92+
auto end = std::chrono::high_resolution_clock::now();
93+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
94+
end - start);
95+
std::cout << elapsed.count() << "ms\n";
15296

15397
EXPECT_EQ(result, false);
15498
}
@@ -159,62 +103,42 @@ TEST_P(DomOrdPWPerf, DomOrdPlus)
159103
SBG::Eval::setPWFactory(2);
160104

161105
SBG::LIB::NAT map_sz = GetParam();
162-
auto [pw1, pw2] = quadraticTest(map_sz);
106+
auto [pw1, pw2] = contiguousMaps(map_sz);
163107

164108
auto start = std::chrono::high_resolution_clock::now();
165109
pw1 + pw2;
166110
auto end = std::chrono::high_resolution_clock::now();
167-
auto curr_time = std::chrono::duration_cast<std::chrono::milliseconds>(
111+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
168112
end - start);
169-
std::cout << curr_time.count() << "\n";
113+
std::cout << elapsed.count() << "ms\n";
170114

171115
SUCCEED();
172116
}
173117

174-
INSTANTIATE_TEST_SUITE_P(
175-
DomOrdPWGrowth,
176-
DomOrdPWPerf,
177-
::testing::Values(1000, 10000, 20000)
178-
);
179-
180-
TEST(DomOrdPWPerfOld, DomOrdDom)
118+
TEST_P(DomOrdPWPerf, DomOrdDom)
181119
{
182-
SBG::LIB::NAT inter_sz = 100;
183-
SBG::LIB::NAT set_sz = 10;
184-
SBG::LIB::NAT map_sz = 1000;
185-
186120
SBG::Eval::setSetFactory(1);
187121
SBG::Eval::setPWFactory(2);
188122

189-
PWMap pw = SBG::LIB::PW_FACT.createPWMap();
190-
for (unsigned int k = 0; k < map_sz; ++k) {
191-
LExp le1(1, k);
192-
Exp exp;
193-
exp.emplaceBack(le1);
194-
exp.emplaceBack(le1);
195-
exp.emplaceBack(le1);
196-
SBG::LIB::NAT map_offset = k*inter_sz*set_sz;
197-
Set s = SBG::LIB::SET_FACT.createSet();
198-
for (unsigned int j = 0; j < set_sz; ++j) {
199-
Interval i1(map_offset + (j*inter_sz) + 1, 1, map_offset + (j+1)*inter_sz);
200-
SetPiece mdi;
201-
mdi.emplaceBack(i1);
202-
mdi.emplaceBack(i1);
203-
mdi.emplaceBack(i1);
204-
s.emplaceBack(mdi);
205-
}
206-
pw.emplaceBack(Map(s, exp));
207-
}
123+
SBG::LIB::NAT map_sz = GetParam();
124+
auto [pw1, pw2] = contiguousMaps(map_sz);
208125

209126
auto start = std::chrono::high_resolution_clock::now();
210-
pw.dom();
127+
pw1.dom();
211128
auto end = std::chrono::high_resolution_clock::now();
212-
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
213-
std::cout << "PWL MAP ORDERED DOM TEST elapsed time: " << elapsed.count() << "ms\n";
129+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
130+
end - start);
131+
std::cout << elapsed.count() << "ms\n";
214132

215133
SUCCEED();
216134
}
217135

136+
INSTANTIATE_TEST_SUITE_P(
137+
DomOrdPWGrowth,
138+
DomOrdPWPerf,
139+
::testing::Values(1000, 10000, 100000, 1000000)
140+
);
141+
218142
TEST(DomOrdPWPerfOld, DomOrdRestrict)
219143
{
220144
unsigned int inter_sz = 100;
@@ -380,7 +304,7 @@ TEST(DomOrdPWPerfOld, DomOrdFirstInvSet)
380304

381305
// equalImage
382306

383-
TEST(DomOrdPWPerf, DomOrdLessImage)
307+
TEST(DomOrdPWPerfOld, DomOrdLessImage)
384308
{
385309
unsigned int inter_sz = 100;
386310
unsigned int set_sz = 10;

test/performance/ord_set_bm.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*****************************************************************************
2+
3+
This file is part of Set--Based Graph Library.
4+
5+
SBG Library is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
SBG Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with SBG Library. If not, see <http://www.gnu.org/licenses/>.
17+
18+
******************************************************************************/
19+
20+
#include <benchmark/benchmark.h>
21+
22+
#include "eval/user_impl_map.hpp"
23+
24+
namespace Test {
25+
26+
namespace Internal {
27+
28+
using SBG::LIB::Interval;
29+
using SBG::LIB::SetPiece;
30+
using SBG::LIB::Set;
31+
32+
std::pair<Set, Set> contiguousPieces(int set_sz) {
33+
SBG::LIB::NAT inter_sz = 100;
34+
35+
Interval second_dim(0, 1, inter_sz - 1);
36+
Set s1 = SBG::LIB::SET_FACT.createSet();
37+
Set s2 = SBG::LIB::SET_FACT.createSet();
38+
for (unsigned int h = 0; h < set_sz; ++h) {
39+
Interval i1(h*inter_sz, 1, (h + 1)*inter_sz - 1);
40+
SetPiece mdi1;
41+
mdi1.emplaceBack(i1);
42+
mdi1.emplaceBack(second_dim);
43+
s1.emplaceBack(mdi1);
44+
45+
SBG::LIB::NAT off = inter_sz/2;
46+
Interval i2(off + (h*inter_sz), 1, off + (h + 1)*inter_sz - 1);
47+
SetPiece mdi2;
48+
mdi2.emplaceBack(i2);
49+
mdi2.emplaceBack(second_dim);
50+
s2.emplaceBack(mdi2);
51+
}
52+
53+
return {s1, s2};
54+
}
55+
56+
static void BM_OrdSetDiff(benchmark::State& state) {
57+
int set_sz = state.range(0);
58+
SBG::Eval::setSetFactory(1);
59+
auto [s1, s2] = contiguousPieces(set_sz);
60+
61+
for (auto _ : state) {
62+
benchmark::DoNotOptimize(s1.difference(s2));
63+
}
64+
state.SetComplexityN(set_sz);
65+
}
66+
BENCHMARK(BM_OrdSetDiff)->RangeMultiplier(10)->Range(1000, 1e6)->Complexity();
67+
68+
static void BM_OrdSetDisjointUnion(benchmark::State& state) {
69+
int set_sz = state.range(0);
70+
SBG::Eval::setSetFactory(1);
71+
Set s1 = SBG::LIB::SET_FACT.createSet();
72+
Set s2 = SBG::LIB::SET_FACT.createSet();
73+
for (SBG::LIB::NAT j = 0; j < set_sz; j += 2) {
74+
Interval i(j*100 + 1, 1, (j + 1)*100);
75+
s1.emplaceBack(i);
76+
Interval i2((j + 1)*100+1, 1, (j + 2)*100);
77+
s2.emplaceBack(i2);
78+
}
79+
80+
for (auto _ : state) {
81+
benchmark::DoNotOptimize(s1.disjointCup(s2));
82+
}
83+
state.SetComplexityN(set_sz);
84+
}
85+
BENCHMARK(BM_OrdSetDisjointUnion)->RangeMultiplier(10)->Range(1000, 1e6)
86+
->Complexity();
87+
88+
static void BM_OrdSetEq(benchmark::State& state) {
89+
int set_sz = state.range(0);
90+
SBG::Eval::setSetFactory(1);
91+
auto [s1, s2] = contiguousPieces(set_sz);
92+
93+
for (auto _ : state) {
94+
benchmark::DoNotOptimize(s1 == s2);
95+
}
96+
state.SetComplexityN(set_sz);
97+
}
98+
BENCHMARK(BM_OrdSetEq)->RangeMultiplier(10)->Range(1000, 1e6)->Complexity();
99+
100+
} // namespace Internal
101+
102+
} // namespace Test

0 commit comments

Comments
 (0)