File: bench_parser.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (346 lines) | stat: -rw-r--r-- 8,322 bytes parent folder | download | duplicates (12)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// 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 "nodejs_parser.hpp"

#include "test/beast/http/message_fuzz.hpp"

#include <boost/beast/http.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/buffers_suffix.hpp>
#include <boost/beast/core/buffers_to_string.hpp>
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <chrono>
#include <iostream>
#include <vector>

namespace boost {
namespace beast {
namespace http {

class parser_test : public beast::unit_test::suite
{
public:
    static std::size_t constexpr N = 2000;

    //using corpus = std::vector<multi_buffer>;
    using corpus = std::vector<flat_buffer>;

    corpus creq_;
    corpus cres_;
    std::size_t size_ = 0;

    corpus
    build_corpus(std::size_t n, std::true_type)
    {
        corpus v;
        v.resize(n);
        message_fuzz mg;
        for(std::size_t i = 0; i < n; ++i)
        {
            mg.request(v[i]);
            size_ += v[i].size();
            BEAST_EXPECT(v[i].size() > 0);
        }
        return v;
    }

    corpus
    build_corpus(std::size_t n, std::false_type)
    {
        corpus v;
        v.resize(n);
        message_fuzz mg;
        for(std::size_t i = 0; i < n; ++i)
        {
            mg.response(v[i]);
            size_ += v[i].size();
            BEAST_EXPECT(v[i].size() > 0);
        }
        return v;
    }

    template<class ConstBufferSequence,
        bool isRequest>
    static
    std::size_t
    feed(ConstBufferSequence const& buffers,
        basic_parser<isRequest>& parser,
            error_code& ec)
    {
        beast::buffers_suffix<
            ConstBufferSequence> cb{buffers};
        std::size_t used = 0;
        for(;;)
        {
            auto const n =
                parser.put(cb, ec);
            if(ec)
                return 0;
            if(n == 0)
                break;
            cb.consume(n);
            used += n;
            if(parser.is_done())
                break;
            if(buffer_bytes(cb) == 0)
                break;
        }
        return used;
    }

    template<class Parser>
    void
    testParser1(std::size_t repeat, corpus const& v)
    {
        while(repeat--)
            for(auto const& b : v)
            {
                Parser p;
                error_code ec;
                p.write(b.data(), ec);
                if(! BEAST_EXPECTS(! ec, ec.message()))
                    log << buffers_to_string(b.data()) << std::endl;
            }
    }

    template<class Parser>
    void
    testParser2(std::size_t repeat, corpus const& v)
    {
        while(repeat--)
            for(auto const& b : v)
            {
                Parser p;
                p.header_limit((std::numeric_limits<std::uint32_t>::max)());
                error_code ec;
                feed(b.data(), p, ec);
                if(! BEAST_EXPECTS(! ec, ec.message()))
                    log << buffers_to_string(b.data()) << std::endl;
            }
    }

    template<class Function>
    void
    timedTest(std::size_t repeat, std::string const& name, Function&& f)
    {
        using namespace std::chrono;
        using clock_type = std::chrono::high_resolution_clock;
        log << name << std::endl;
        for(std::size_t trial = 1; trial <= repeat; ++trial)
        {
            auto const t0 = clock_type::now();
            f();
            auto const elapsed = clock_type::now() - t0;
            log <<
                "Trial " << trial << ": " <<
                duration_cast<milliseconds>(elapsed).count() << " ms" << std::endl;
        }
    }

    template<bool isRequest>
    struct null_parser :
        basic_parser<isRequest>
    {
        void
        on_request_impl(
            verb, string_view, string_view,
            int, error_code&) override
        {
        }

        void
        on_response_impl(
            int, string_view, int,
            error_code&) override
        {
        }

        void
        on_field_impl(
            field, string_view, string_view,
            error_code&) override
        {
        }

        void
        on_header_impl(error_code&) override
        {
        }

        void
        on_body_init_impl(
            boost::optional<std::uint64_t> const&,
            error_code&) override
        {
        }

        std::size_t
        on_body_impl(
            string_view,
            error_code&) override
        {
            return 0;
        }

        void
        on_chunk_header_impl(
            std::uint64_t,
            string_view,
            error_code&) override
        {
        }

        std::size_t
        on_chunk_body_impl(
            std::uint64_t,
            string_view,
            error_code&) override
        {
            return 0;
        }

        void
        on_finish_impl(error_code&) override
        {
        }
    };

    template<bool isRequest, class Body, class Fields>
    struct bench_parser : basic_parser<isRequest>
    {
        using mutable_buffers_type =
            net::mutable_buffer;

        void
        on_request_impl(verb, string_view,
            string_view, int, error_code&) override
        {
        }

        void
        on_response_impl(int,
            string_view, int, error_code&) override
        {
        }

        void
        on_field_impl(field,
            string_view, string_view, error_code&) override
        {
        }

        void
        on_header_impl(error_code&) override
        {
        }

        void
        on_body_init_impl(
            boost::optional<std::uint64_t> const&,
            error_code&) override
        {
        }

        std::size_t
        on_body_impl(
            string_view s, error_code&) override
        {
            return s.size();
        }

        void
        on_chunk_header_impl(std::uint64_t,
            string_view, error_code&) override
        {
        }

        std::size_t
        on_chunk_body_impl(std::uint64_t,
            string_view s, error_code&) override
        {
            return s.size();
        }

        void
        on_finish_impl(error_code&) override
        {
        }
    };

    void
    testSpeed()
    {
        static std::size_t constexpr Trials = 5;
        static std::size_t constexpr Repeat = 500;

        creq_ = build_corpus(N/2, std::true_type{});
        cres_ = build_corpus(N/2, std::false_type{});

        log << "sizeof(request parser)  == " <<
            sizeof(null_parser<true>) << '\n';

        log << "sizeof(response parser) == " <<
            sizeof(null_parser<false>)<< '\n';

        testcase << "Parser speed test, " <<
            ((Repeat * size_ + 512) / 1024) << "KB in " <<
                (Repeat * (creq_.size() + cres_.size())) << " messages";

#if 0
        timedTest(Trials, "http::parser",
            [&]
            {
                testParser2<request_parser<dynamic_body>>(Repeat, creq_);
                testParser2<response_parser<dynamic_body>>(Repeat, cres_);
            });
#endif
#if 1
        timedTest(Trials, "http::basic_parser",
            [&]
            {
                testParser2<bench_parser<
                    true, dynamic_body, fields> >(
                        Repeat, creq_);
                testParser2<bench_parser<
                    false, dynamic_body, fields>>(
                        Repeat, cres_);
            });
#if 1
        timedTest(Trials, "nodejs_parser",
            [&]
            {
                testParser1<nodejs_parser<
                    true, dynamic_body, fields>>(
                        Repeat, creq_);
                testParser1<nodejs_parser<
                    false, dynamic_body, fields>>(
                        Repeat, cres_);
            });
#endif
#endif
        pass();
    }

    void run() override
    {
        pass();
        testSpeed();
    }
};

BEAST_DEFINE_TESTSUITE(beast,benchmarks,parser);

} // http
} // beast
} // boost