Files
beast/test/http/parser_bench.cpp
T

272 lines
6.5 KiB
C++
Raw Normal View History

2016-04-25 05:27:34 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-04-25 05:27:34 -04:00
//
// 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)
//
#include "nodejs_parser.hpp"
#include "message_fuzz.hpp"
2016-05-07 14:57:15 -04:00
2016-04-25 05:27:34 -04:00
#include <beast/http.hpp>
2016-11-20 07:32:41 -05:00
#include <beast/core/consuming_buffers.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/flat_buffer.hpp>
#include <beast/core/multi_buffer.hpp>
2016-05-06 19:14:17 -04:00
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
2016-04-25 05:27:34 -04:00
#include <chrono>
#include <iostream>
#include <vector>
namespace beast {
namespace http {
2016-05-06 19:14:17 -04:00
class parser_bench_test : public beast::unit_test::suite
2016-04-25 05:27:34 -04:00
{
public:
static std::size_t constexpr N = 2000;
//using corpus = std::vector<multi_buffer>;
using corpus = std::vector<flat_buffer>;
2016-04-25 05:27:34 -04:00
corpus creq_;
corpus cres_;
std::size_t size_ = 0;
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
2016-04-25 05:27:34 -04:00
corpus
2016-04-27 07:02:51 -04:00
build_corpus(std::size_t n, std::true_type)
2016-04-25 05:27:34 -04:00
{
corpus v;
v.resize(n);
2016-04-25 05:27:34 -04:00
message_fuzz mg;
2016-04-27 07:02:51 -04:00
for(std::size_t i = 0; i < n; ++i)
2016-04-25 05:27:34 -04:00
{
mg.request(v[i]);
size_ += v[i].size();
BEAST_EXPECT(v[i].size() > 0);
2016-04-25 05:27:34 -04:00
}
return v;
}
corpus
2016-04-27 07:02:51 -04:00
build_corpus(std::size_t n, std::false_type)
2016-04-25 05:27:34 -04:00
{
corpus v;
v.resize(n);
2016-04-25 05:27:34 -04:00
message_fuzz mg;
2016-04-27 07:02:51 -04:00
for(std::size_t i = 0; i < n; ++i)
2016-04-25 05:27:34 -04:00
{
mg.response(v[i]);
size_ += v[i].size();
BEAST_EXPECT(v[i].size() > 0);
2016-04-25 05:27:34 -04:00
}
return v;
}
2016-11-20 07:32:41 -05:00
template<class ConstBufferSequence,
bool isRequest, class Derived>
2016-11-20 07:32:41 -05:00
static
std::size_t
feed(ConstBufferSequence const& buffers,
basic_parser<isRequest, Derived>& parser,
2016-11-20 07:32:41 -05:00
error_code& ec)
{
using boost::asio::buffer_size;
beast::consuming_buffers<
ConstBufferSequence> cb{buffers};
std::size_t used = 0;
for(;;)
{
auto const n =
parser.put(cb, ec);
2016-11-20 07:32:41 -05:00
if(ec)
return 0;
if(n == 0)
break;
cb.consume(n);
used += n;
if(parser.is_done())
2016-11-20 07:32:41 -05:00
break;
if(buffer_size(cb) == 0)
break;
}
return used;
}
2016-04-25 05:27:34 -04:00
template<class Parser>
void
2016-11-20 07:32:41 -05:00
testParser1(std::size_t repeat, corpus const& v)
2016-04-25 05:27:34 -04:00
{
while(repeat--)
for(auto const& b : v)
2016-04-25 05:27:34 -04:00
{
Parser p;
error_code ec;
p.write(b.data(), ec);
2016-08-29 13:28:08 -04:00
if(! BEAST_EXPECTS(! ec, ec.message()))
log << to_string(b.data()) << std::endl;
2016-04-25 05:27:34 -04:00
}
}
2016-11-20 07:32:41 -05:00
template<class Parser>
void
testParser2(std::size_t repeat, corpus const& v)
{
while(repeat--)
for(auto const& b : v)
2016-11-20 07:32:41 -05:00
{
Parser p;
error_code ec;
feed(b.data(), p, ec);
2016-11-20 07:32:41 -05:00
if(! BEAST_EXPECTS(! ec, ec.message()))
log << to_string(b.data()) << std::endl;
2016-11-20 07:32:41 -05:00
}
}
2016-04-25 05:27:34 -04:00
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;
2016-05-10 07:09:57 -04:00
log << name << std::endl;
2016-04-25 05:27:34 -04:00
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 << ": " <<
2016-05-10 07:09:57 -04:00
duration_cast<milliseconds>(elapsed).count() << " ms" << std::endl;
2016-04-25 05:27:34 -04:00
}
}
template<bool isRequest>
2016-11-20 07:32:41 -05:00
struct null_parser :
basic_parser<isRequest, null_parser<isRequest>>
2016-04-25 05:27:34 -04:00
{
};
2016-11-20 07:32:41 -05:00
template<bool isRequest, class Body, class Fields>
struct bench_parser : basic_parser<
isRequest, bench_parser<isRequest, Body, Fields>>
2016-11-20 07:32:41 -05:00
{
using mutable_buffers_type =
boost::asio::mutable_buffers_1;
void
2017-06-04 10:52:28 -07:00
on_request(string_view,
string_view,
2016-11-20 07:32:41 -05:00
int, error_code&)
{
}
void
on_response(int,
2017-06-04 10:52:28 -07:00
string_view,
2016-11-20 07:32:41 -05:00
int, error_code&)
{
}
void
2017-06-04 10:52:28 -07:00
on_field(string_view,
string_view,
2016-11-20 07:32:41 -05:00
error_code&)
{
}
void
on_header(error_code& ec)
{
}
void
on_body(boost::optional<std::uint64_t> const&,
error_code&)
2016-11-20 07:32:41 -05:00
{
}
void
2017-06-04 10:52:28 -07:00
on_data(string_view,
2016-11-20 07:32:41 -05:00
error_code& ec)
{
}
void
on_chunk(std::uint64_t,
2017-06-04 10:52:28 -07:00
string_view,
2016-11-20 07:32:41 -05:00
error_code&)
{
}
void
on_complete(error_code&)
{
}
};
2016-04-25 05:27:34 -04:00
void
testSpeed()
{
static std::size_t constexpr Trials = 3;
2016-11-20 07:32:41 -05:00
static std::size_t constexpr Repeat = 500;
2016-04-25 05:27:34 -04:00
creq_ = build_corpus(N/2, std::true_type{});
cres_ = build_corpus(N/2, std::false_type{});
2016-04-25 05:27:34 -04:00
log << "sizeof(request parser) == " <<
2016-11-20 07:32:41 -05:00
sizeof(null_parser<true>) << '\n';
2016-04-25 05:27:34 -04:00
log << "sizeof(response parser) == " <<
2016-11-20 07:32:41 -05:00
sizeof(null_parser<false>)<< '\n';
2016-04-25 05:27:34 -04:00
testcase << "Parser speed test, " <<
((Repeat * size_ + 512) / 1024) << "KB in " <<
(Repeat * (creq_.size() + cres_.size())) << " messages";
2016-11-20 07:32:41 -05:00
timedTest(Trials, "http::basic_parser",
2016-04-25 05:27:34 -04:00
[&]
{
2016-11-20 07:32:41 -05:00
testParser2<bench_parser<
true, dynamic_body, fields> >(
2016-04-25 05:27:34 -04:00
Repeat, creq_);
2016-11-20 07:32:41 -05:00
testParser2<bench_parser<
false, dynamic_body, fields>>(
2016-04-25 05:27:34 -04:00
Repeat, cres_);
});
timedTest(Trials, "nodejs_parser",
[&]
{
testParser1<nodejs_parser<
true, dynamic_body, fields>>(
Repeat, creq_);
testParser1<nodejs_parser<
false, dynamic_body, fields>>(
Repeat, cres_);
});
2016-04-25 05:27:34 -04:00
pass();
}
void run() override
{
pass();
testSpeed();
}
};
BEAST_DEFINE_TESTSUITE(parser_bench,http,beast);
} // http
} // beast