Skip to content

Commit d8e7986

Browse files
committed
feat : add benchmark code
1 parent 5c7d210 commit d8e7986

File tree

3 files changed

+159
-55
lines changed

3 files changed

+159
-55
lines changed

CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
1717
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
1818

1919

20-
if(MSVC)
21-
set(gtest_force_shared_crt ON CACHE BOOL "Force Google Test to use shared (DLL) run-time lib.")
22-
endif()
23-
2420
add_library(tlsf-allocator STATIC
2521

2622
src/tlsf.cpp
@@ -49,14 +45,14 @@ option(TLSF_BUILD_BENCHMARK "Build the benchmark application for the TLSF Alloca
4945

5046

5147
if(TLSF_BUILD_TESTS)
52-
#test folder
48+
5349
if(MSVC)
5450
set(gtest_force_shared_crt ON CACHE BOOL "Force Google Test to use shared (DLL) run-time lib.")
5551
endif()
5652

5753
enable_testing()
54+
#test folder
5855
add_subdirectory(test)
59-
6056
endif()
6157

6258

benchmark/benchmark.cpp

Lines changed: 157 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,209 @@
1+
12
#include <benchmark/benchmark.h>
23
#include <tlsf.h>
34
#include <iostream>
5+
#include <random>
46

57

68

79

810

9-
class AllocatorFixture : public benchmark::Fixture
11+
class MyTLSFAllocatorFixture : public benchmark::Fixture
1012
{
1113
public:
12-
TlsfAllocator* tlsf;
13-
const size_t POOL_SIZE = 1024 * 1024 * 1;// 1 MiB pool size
14-
void SetUp(const ::benchmark::State& state)
14+
TlsfAllocator* m_tlsf;
15+
const size_t POOL_SIZE = 1024 * 1024 *50;// 50 MiB pool size
16+
void SetUp(const ::benchmark::State& state)
1517
{
16-
tlsf = new TlsfAllocator(POOL_SIZE);
18+
m_tlsf = new TlsfAllocator(POOL_SIZE);
1719
}
18-
void TearDown(const ::benchmark::State& state)
20+
void TearDown(const ::benchmark::State& state)
1921
{
20-
delete tlsf;
22+
delete m_tlsf;
2123
}
2224
};
2325

2426

25-
static void BM_System_Malloc(benchmark::State& state)
27+
28+
29+
30+
31+
32+
/******* Mixed Allocation *********/
33+
34+
35+
BENCHMARK_DEFINE_F(MyTLSFAllocatorFixture, MyTlsf_AllocDeallocCycle_MixedSize) (benchmark::State& state)
2636
{
27-
const size_t ALLOCATION_SIZE = state.range(0);
37+
const size_t NUM_ALLOCATIONS = state.range(0);
38+
const size_t MAX_ALLOC_SIZE = 1024 * 128; // Max 128 KiB per block
39+
40+
std::mt19937 gen(28);
41+
std::uniform_int_distribution<> distrib(20, MAX_ALLOC_SIZE);//fom 20 bytes to 128 KiB
42+
std::vector<void*> allocated_pointers;
43+
44+
allocated_pointers.reserve(NUM_ALLOCATIONS);
45+
2846
for (auto _ : state)
2947
{
30-
void* p = std::malloc(ALLOCATION_SIZE);
48+
// 1. ALLOCATION PHASE (TIME MEASURED)
49+
for (size_t i = 0; i < NUM_ALLOCATIONS; ++i)
50+
{
51+
size_t size = distrib(gen);
52+
void* p;
53+
3154

32-
benchmark::DoNotOptimize(p);
33-
std::free(p);
55+
p = m_tlsf->allocate(size);
56+
57+
if (p == nullptr)
58+
{
59+
// Skips this benchmark run iteration gracefully
60+
state.SkipWithError("TLSF Allocation failed (Pool Exhaustion).");
61+
return;
62+
}
63+
benchmark::DoNotOptimize(p);
64+
65+
66+
allocated_pointers.push_back(p);
67+
}
68+
69+
70+
71+
72+
for (void* p : allocated_pointers)
73+
{
74+
m_tlsf->deallocate(p);
75+
}
76+
allocated_pointers.clear();
77+
78+
3479
}
35-
}
3680

37-
BENCHMARK(BM_System_Malloc)->Arg( 1024 * 40 );//40 KiB
3881

3982

83+
}
4084

41-
BENCHMARK_DEFINE_F(AllocatorFixture, BM_Tlsf_AllocateAndFree) (benchmark::State& state)
85+
BENCHMARK_REGISTER_F(MyTLSFAllocatorFixture, MyTlsf_AllocDeallocCycle_MixedSize)
86+
->Args({ 10 })
87+
->Args({ 20 })
88+
->Args({ 30 })
89+
->Args({ 40 })
90+
->Args({ 50 })
91+
->Args({ 60 })
92+
->Args({ 70 })
93+
->Args({ 80 })
94+
->Args({ 90 })
95+
->Args({ 100 })
96+
->Args({ 110 })
97+
->Args({ 120 })
98+
->Args({ 130 })
99+
->Args({ 140 })
100+
->Args({ 150 })
101+
->Args({ 160 })
102+
->Args({ 170 })
103+
->Args({ 180 })
104+
->Args({ 190 })
105+
->Args({ 200 });
106+
107+
108+
109+
110+
static void SystemMalloc_AllocDeallocCycle_MixedSize(benchmark::State& state)
42111
{
43-
const size_t ALLOCATION_SIZE = state.range(0);
112+
const size_t NUM_ALLOCATIONS = state.range(0);
113+
const size_t MAX_ALLOC_SIZE = 1024 * 128; // Max 128 KiB per block
114+
115+
116+
std::vector<void*> pointers;
117+
pointers.reserve(NUM_ALLOCATIONS);
118+
119+
// Random number generation setup (fixed seed for stable testing)
120+
std::mt19937 gen(28);
121+
std::uniform_int_distribution<> distrib(20, MAX_ALLOC_SIZE);// from 20 bytes to 128 KiB
122+
123+
44124
for (auto _ : state)
45125
{
46-
void* p = tlsf->allocate(ALLOCATION_SIZE);
47-
benchmark::DoNotOptimize(p);
48-
tlsf->deallocate(p);
126+
// 1. ALLOCATION PHASE (TIME MEASURED)
127+
for (size_t i = 0; i < NUM_ALLOCATIONS; ++i)
128+
{
129+
size_t size = distrib(gen);
130+
void* p;
131+
132+
133+
p = std::malloc(size); // <<<--- MALLOC CALL
134+
135+
if (p == nullptr)
136+
{
137+
state.SkipWithError("System Malloc failed (OOM).");
138+
return;
139+
}
140+
benchmark::DoNotOptimize(p);
141+
pointers.push_back(p);
142+
}
143+
144+
145+
146+
147+
for (void* p : pointers)
148+
{
149+
std::free(p); // <<<--- FREE CALL
150+
}
151+
pointers.clear();
152+
153+
154+
49155
}
156+
157+
158+
159+
50160
}
51-
BENCHMARK_REGISTER_F(AllocatorFixture, BM_Tlsf_AllocateAndFree)->Arg(1024 * 40);// 40 KiB
52161

162+
BENCHMARK(SystemMalloc_AllocDeallocCycle_MixedSize)
163+
->Args({ 10 })
164+
->Args({ 20 })
165+
->Args({ 30 })
166+
->Args({ 40 })
167+
->Args({ 50 })
168+
->Args({ 60 })
169+
->Args({ 70 })
170+
->Args({ 80 })
171+
->Args({ 90 })
172+
->Args({ 100 })
173+
->Args({ 110 })
174+
->Args({ 120 })
175+
->Args({ 130 })
176+
->Args({ 140 })
177+
->Args({ 150 })
178+
->Args({ 160 })
179+
->Args({ 170 })
180+
->Args({ 180 })
181+
->Args({ 190 })
182+
->Args({ 200 });
53183

54184
//BENCHMARK_MAIN();
55185

56186

57187

188+
189+
190+
191+
192+
58193
int main(int argc, char** argv)
59194
{
60195
// 1. Initialize and run the benchmarks
61196
benchmark::Initialize(&argc, argv);
62197
if (benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
63-
198+
64199
bool success = benchmark::RunSpecifiedBenchmarks();
65200

66201
std::cout << "\n\n--- Benchmarks Finished ---\nPress ENTER to exit...";
67202

68203

69204

70205
// Wait for the user to press ENTER
71-
//std::cin.get();
206+
std::cin.get();
72207

73208
return success ? 0 : 1;
74209
}

demo/demo_app.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)