File size: 3,441 Bytes
1dd0e3b
 
 
aa33e74
1dd0e3b
 
8ad3629
1dd0e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <benchmark/benchmark.h>
#include <vector>
#include "object_pool.h"
#include <cstring>

// 测试结构体
struct TestStruct {
    int id;
    double value;
    char name[32];

    TestStruct(int id = 0, double value = 0.0, const char* name = "")
        : id(id), value(value) {
        strcpy(this->name, name);
    }
};

// 测试 ObjectPool 分配
static void BM_ObjectPool_Allocate(benchmark::State& state) {
    hhb::core::ObjectPool<TestStruct> pool;
    std::vector<TestStruct*> objects;
    objects.reserve(state.range(0));

    for (auto _ : state) {
        for (int i = 0; i < state.range(0); ++i) {
            TestStruct* obj = pool.allocate();
            new (obj) TestStruct(i, i * 1.0, "test");
            objects.push_back(obj);
        }

        // 释放所有对象
        for (TestStruct* obj : objects) {
            obj->~TestStruct();
            pool.deallocate(obj);
        }
        objects.clear();
    }
}

// 测试 std::vector 分配
static void BM_Vector_Allocate(benchmark::State& state) {
    std::vector<TestStruct> objects;

    for (auto _ : state) {
        objects.reserve(state.range(0));
        for (int i = 0; i < state.range(0); ++i) {
            objects.emplace_back(i, i * 1.0, "test");
        }
        objects.clear();
    }
}

// 测试内存碎片率(通过分配和释放模式)
static void BM_ObjectPool_Fragmentation(benchmark::State& state) {
    hhb::core::ObjectPool<TestStruct> pool;
    std::vector<TestStruct*> objects;
    objects.reserve(state.range(0));

    for (auto _ : state) {
        // 分配一半对象
        for (int i = 0; i < state.range(0) / 2; ++i) {
            TestStruct* obj = pool.allocate();
            new (obj) TestStruct(i, i * 1.0, "test");
            objects.push_back(obj);
        }

        // 释放一半对象
        for (int i = 0; i < objects.size() / 2; ++i) {
            objects[i]->~TestStruct();
            pool.deallocate(objects[i]);
        }

        // 重新分配对象
        for (int i = 0; i < state.range(0) / 2; ++i) {
            TestStruct* obj = pool.allocate();
            new (obj) TestStruct(i + state.range(0) / 2, (i + state.range(0) / 2) * 1.0, "test");
            objects.push_back(obj);
        }

        // 释放所有对象
        for (TestStruct* obj : objects) {
            obj->~TestStruct();
            pool.deallocate(obj);
        }
        objects.clear();
    }
}

static void BM_Vector_Fragmentation(benchmark::State& state) {
    std::vector<TestStruct> objects;

    for (auto _ : state) {
        // 分配一半对象
        objects.reserve(state.range(0));
        for (int i = 0; i < state.range(0) / 2; ++i) {
            objects.emplace_back(i, i * 1.0, "test");
        }

        // 释放一半对象
        objects.erase(objects.begin(), objects.begin() + objects.size() / 2);

        // 重新分配对象
        for (int i = 0; i < state.range(0) / 2; ++i) {
            objects.emplace_back(i + state.range(0) / 2, (i + state.range(0) / 2) * 1.0, "test");
        }

        objects.clear();
    }
}

// 注册测试
BENCHMARK(BM_ObjectPool_Allocate)->RangeMultiplier(10)->Range(1000, 10000000);
BENCHMARK(BM_Vector_Allocate)->RangeMultiplier(10)->Range(1000, 10000000);
BENCHMARK(BM_ObjectPool_Fragmentation)->RangeMultiplier(10)->Range(1000, 10000000);
BENCHMARK(BM_Vector_Fragmentation)->RangeMultiplier(10)->Range(1000, 10000000);

BENCHMARK_MAIN();