File: bench_buffers.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (241 lines) | stat: -rw-r--r-- 6,678 bytes parent folder | download | duplicates (10)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

#include <boost/beast/core/buffers_range.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/core/read_size.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/asio/streambuf.hpp>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <iomanip>
#include <utility>
#include <vector>

namespace boost {
namespace beast {

class buffers_test : public beast::unit_test::suite
{
public:
    using size_type = std::uint64_t;

    class timer
    {
        using clock_type =
            std::chrono::system_clock;

        clock_type::time_point when_;

    public:
        using duration =
            clock_type::duration;

        timer()
            : when_(clock_type::now())
        {
        }

        duration
        elapsed() const
        {
            return clock_type::now() - when_;
        }
    };

    inline
    size_type
    throughput(std::chrono::duration<
        double> const& elapsed, size_type items)
    {
        using namespace std::chrono;
        return static_cast<size_type>(
            1 / (elapsed/items).count());
    }

    template<class MutableBufferSequence>
    static
    std::size_t
    fill(MutableBufferSequence const& buffers)
    {
        std::size_t n = 0;
        for(auto b : beast::buffers_range_ref(buffers))
        {
            std::fill(
                static_cast<char*>(b.data()),
                static_cast<char*>(b.data()) +
                    b.size(), '\0');
            n += b.size();
        }
        return n;
    }

    template<class DynamicBuffer>
    size_type
    do_prepares(std::size_t repeat,
        std::size_t count, std::size_t size)
    {
        timer t;
        size_type total = 0;
        for(auto i = repeat; i--;)
        {
            DynamicBuffer b;
            for(auto j = count; j--;)
            {
                auto const n = fill(b.prepare(size));
                b.commit(n);
                total += n;
            }
        }
        return throughput(t.elapsed(), total);
    }

    template<class DynamicBuffer>
    size_type
    do_hints(std::size_t repeat,
        std::size_t count, std::size_t size)
    {
        timer t;
        size_type total = 0;
        for(auto i = repeat; i--;)
        {
            DynamicBuffer b;
            for(auto j = count; j--;)
            {
                for(auto remain = size; remain;)
                {
                    auto const n = fill(b.prepare(
                        read_size(b, remain)));
                    b.commit(n);
                    remain -= n;
                    total += n;
                }
            }
        }
        return throughput(t.elapsed(), total);
    }

    template<class DynamicBuffer>
    size_type
    do_random(std::size_t repeat,
        std::size_t count, std::size_t size)
    {
        timer t;
        size_type total = 0;
        for(auto i = repeat; i--;)
        {
            DynamicBuffer b;
            for(auto j = count; j--;)
            {
                auto const n = fill(b.prepare(
                    1 + (rand()%(2*size))));
                b.commit(n);
                total += n;
            }
        }
        return throughput(t.elapsed(), total);
    }

    static
    inline
    void
    do_trials_1(bool)
    {
    }

    template<class F0, class... FN>
    void
    do_trials_1(bool print, F0&& f, FN... fn)
    {
        timer t;
        using namespace std::chrono;
        static size_type constexpr den = 1024 * 1024;
        if(print)
        {
            log << std::right << std::setw(10) <<
                ((f() + (den / 2)) / den) << " MB/s";
            log.flush();
        }
        else
        {
            f();
        }
        do_trials_1(print, fn...);
    }

    template<class F0, class... FN>
    void
    do_trials(string_view name,
        std::size_t trials, F0&& f0, FN... fn)
    {
        using namespace std::chrono;
        // warm-up
        do_trials_1(false, f0, fn...);
        do_trials_1(false, f0, fn...);
        while(trials--)
        {
            timer t;
            log << std::left << std::setw(24) << name << ":";
            log.flush();
            do_trials_1(true, f0, fn...);
            log << "   " <<
                duration_cast<milliseconds>(t.elapsed()).count() << "ms";
            log << std::endl;
        }
    }

    void
    run() override
    {
        static std::size_t constexpr trials = 1;
        static std::size_t constexpr repeat = 250;
        std::vector<std::pair<std::size_t, std::size_t>> params;
        params.emplace_back(1024, 1024);
        params.emplace_back(512, 4096);
        params.emplace_back(256, 32768);
        log << std::endl;
        for(auto const& param : params)
        {
            auto const count = param.first;
            auto const size = param.second;
            auto const s = std::string("count=") + std::to_string(count) +
                ", size=" + std::to_string(size);
            log << std::left << std::setw(24) << s << " " <<
                std::right << std::setw(15) << "prepare" <<
                std::right << std::setw(15) << "with hint" <<
                std::right << std::setw(15) << "random" <<
                std::endl;
            do_trials("multi_buffer", trials,
                 [&](){ return do_prepares<multi_buffer>(repeat, count, size); }
                ,[&](){ return do_hints   <multi_buffer>(repeat, count, size); }
                ,[&](){ return do_random  <multi_buffer>(repeat, count, size); }
            );
            do_trials("flat_buffer", trials,
                 [&](){ return do_prepares<flat_buffer>(repeat, count, size); }
                ,[&](){ return do_hints   <flat_buffer>(repeat, count, size); }
                ,[&](){ return do_random  <flat_buffer>(repeat, count, size); }
            );
            do_trials("net::streambuf", trials,
                 [&](){ return do_prepares<net::streambuf>(repeat, count, size); }
                ,[&](){ return do_hints   <net::streambuf>(repeat, count, size); }
                ,[&](){ return do_random  <net::streambuf>(repeat, count, size); }
            );
            log << std::endl;
        }
        pass();
    }
};

BEAST_DEFINE_TESTSUITE(beast,benchmarks,buffers);

} // beast
} // boost