File: benchmark.hpp

package info (click to toggle)
foonathan-memory 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: cpp: 12,014; xml: 139; sh: 49; makefile: 22
file content (179 lines) | stat: -rw-r--r-- 4,994 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2015-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

#ifndef FOONATHAN_MEMORY_TEST_BENCHMARK_HPP_INCLUDED
#define FOONATHAN_MEMORY_TEST_BENCHMARK_HPP_INCLUDED

// Benchmarking functions and allocator scenarios

#include <algorithm>
#include <chrono>
#include <random>
#include <vector>

#include "foonathan/memory/allocator_traits.hpp"

using unit = std::chrono::nanoseconds;

template <typename F, typename... Args>
std::size_t measure(F func, Args&&... args)
{
    auto start = std::chrono::system_clock::now();
    func(std::forward<Args>(args)...);
    auto duration = std::chrono::duration_cast<unit>(std::chrono::system_clock::now() - start);
    return std::size_t(duration.count());
}

std::size_t sample_size = 1024u;

template <typename F, typename Alloc, typename... Args>
std::size_t benchmark(F measure_func, Alloc make_alloc, Args&&... args)
{
    auto min_time = std::size_t(-1);
    for (std::size_t i = 0u; i != sample_size; ++i)
    {
        auto alloc = make_alloc();
        auto time  = measure_func(alloc, std::forward<Args>(args)...);
        if (time < min_time)
            min_time = time;
    }
    return min_time;
}

struct single
{
    std::size_t count;

    single(std::size_t c) : count(c)
    {
    }

    template <class RawAllocator>
    std::size_t operator()(RawAllocator& alloc, std::size_t size)
    {
        using namespace foonathan::memory;
        return measure([&]() {
            for (std::size_t i = 0u; i != count; ++i)
            {
                volatile auto ptr = allocator_traits<RawAllocator>::allocate_node(alloc, size, 1);
                allocator_traits<RawAllocator>::deallocate_node(alloc, ptr, size, 1);
            }
        });
    }

    template <class RawAllocator>
    std::size_t operator()(RawAllocator& alloc, std::size_t array_size, std::size_t node_size)
    {
        return measure([&]() {
            for (std::size_t i = 0u; i != count; ++i)
            {
                auto ptr =
                    allocator_traits<RawAllocator>::allocate_array(alloc, array_size, node_size, 1);
                allocator_traits<RawAllocator>::deallocate_array(alloc, ptr, array_size, node_size,
                                                                 1);
            }
        });
    }

    static const char* name()
    {
        return "single";
    }
};

struct basic_bulk
{
    using order_func = void (*)(std::vector<void*>&);

    order_func  func;
    std::size_t count;

    basic_bulk(order_func f, std::size_t c) : func(f), count(c)
    {
    }

    template <class RawAllocator>
    std::size_t operator()(RawAllocator& alloc, std::size_t node_size)
    {
        using namespace foonathan::memory;

        std::vector<void*> ptrs;
        ptrs.reserve(count);

        auto alloc_t = measure([&]() {
            for (std::size_t i = 0u; i != count; ++i)
                ptrs.push_back(allocator_traits<RawAllocator>::allocate_node(alloc, node_size, 1));
        });
        func(ptrs);
        auto dealloc_t = measure([&]() {
            for (auto ptr : ptrs)
                allocator_traits<RawAllocator>::deallocate_node(alloc, ptr, node_size, 1);
        });
        return alloc_t + dealloc_t;
    }

    template <class RawAllocator>
    std::size_t operator()(RawAllocator& alloc, std::size_t array_size, std::size_t node_size)
    {
        using namespace foonathan::memory;

        std::vector<void*> ptrs;
        ptrs.reserve(count);

        auto alloc_t = measure([&]() {
            for (std::size_t i = 0u; i != count; ++i)
                ptrs.push_back(allocator_traits<RawAllocator>::allocate_array(alloc, array_size,
                                                                              node_size, 1));
        });
        func(ptrs);
        auto dealloc_t = measure([&]() {
            for (auto ptr : ptrs)
                allocator_traits<RawAllocator>::deallocate_array(alloc, ptr, array_size, node_size,
                                                                 1);
        });
        return alloc_t + dealloc_t;
    }
};

struct bulk : basic_bulk
{
    bulk(std::size_t c) : basic_bulk([](std::vector<void*>&) {}, c)
    {
    }

    static const char* name()
    {
        return "bulk";
    }
};

struct bulk_reversed : basic_bulk
{
    bulk_reversed(std::size_t c)
    : basic_bulk([](std::vector<void*>& ptrs) { std::reverse(ptrs.begin(), ptrs.end()); }, c)
    {
    }

    static const char* name()
    {
        return "bulk_reversed";
    }
};

struct butterfly : basic_bulk
{
    butterfly(std::size_t c)
    : basic_bulk([](std::vector<void*>&
                        ptrs) { std::shuffle(ptrs.begin(), ptrs.end(), std::mt19937{}); },
                 c)
    {
    }

    static const char* name()
    {
        return "butterfly\n";
    }
};

#endif // FOONATHAN_MEMORY_TEST_BENCHMARK_HPP_INCLUDED