Skip to content

Commit

Permalink
refactor choosing benchmark operation
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Oct 14, 2024
1 parent d1c65b3 commit 7113c5b
Showing 1 changed file with 69 additions and 76 deletions.
145 changes: 69 additions & 76 deletions bench/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class any_impl
bool with_file_io_ = false;

public:
using operation = auto (any_impl::*)
(file_item const& fi, std::size_t repeat) const
-> clock_type::duration;

any_impl(
string_view base_name,
bool is_boost,
Expand Down Expand Up @@ -148,15 +152,21 @@ class any_impl

virtual
clock_type::duration
parse(string_view s, std::size_t repeat) const = 0;
parse_string(file_item const& fi, std::size_t repeat) const = 0;

virtual
clock_type::duration
parse(file_item const& fi, std::size_t repeat) const = 0;
parse_file(file_item const& fi, std::size_t repeat) const = 0;

virtual
clock_type::duration
serialize(string_view s, std::size_t repeat) const = 0;
serialize_string(file_item const& fi, std::size_t repeat) const = 0;

clock_type::duration
skip(file_item const& , std::size_t ) const
{
return clock_type::duration::zero();
}

string_view
name() const noexcept
Expand Down Expand Up @@ -259,22 +269,26 @@ bench(
{
trial.clear();
std::size_t repeat = 1;
auto const f = [&]
{
if(verb == "Parse")
{
if( vi[j]->with_file_io() )
return vi[j]->parse(vf[i], repeat);
else
return vi[j]->parse(vf[i].text, repeat);
}

any_impl::operation op = nullptr;
if(verb == "Parse")
{
if( vi[j]->with_file_io() )
op = &any_impl::parse_file;
else
op = &any_impl::parse_string;
}
else
{
BOOST_ASSERT( verb == "Serialize" );
if( vi[j]->with_file_io() )
return clock_type::duration::zero();
op = &any_impl::skip;
else
return vi[j]->serialize(vf[i].text, repeat);
};
op = &any_impl::serialize_string;
}

auto const f = [&]{ return (vi[j].get()->*op)(vf[i], repeat); };

// helps with the caching, which reduces noise; also, we determine
// if this configuration should be skipped altogether
auto const elapsed = f();
Expand Down Expand Up @@ -360,9 +374,7 @@ class boost_impl : public any_impl
{}

clock_type::duration
parse(
string_view s,
std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
parser p( {}, get_parse_options() );
Expand All @@ -374,15 +386,15 @@ class boost_impl : public any_impl
sp = &mr;
p.reset( std::move(sp) );

p.write( s.data(), s.size() );
p.write( fi.text.data(), fi.text.size() );
auto jv = p.release();
(void)jv;
}
return clock_type::now() - start;
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
stream_parser p( {}, get_parse_options() );
Expand Down Expand Up @@ -419,15 +431,13 @@ class boost_impl : public any_impl
}

clock_type::duration
serialize(
string_view s,
std::size_t repeat) const override
serialize_string(file_item const& fi, std::size_t repeat) const override
{
monotonic_resource mr;
storage_ptr sp;
if( is_pool_ )
sp = &mr;
auto jv = json::parse( s, std::move(sp) );
auto jv = json::parse( fi.text, std::move(sp) );

auto const start = clock_type::now();
serializer sr;
Expand Down Expand Up @@ -535,25 +545,23 @@ class boost_null_impl : public any_impl
{}

clock_type::duration
parse(
string_view s,
std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
null_parser p( get_parse_options() );
while(repeat--)
{
p.reset();
system::error_code ec;
p.write(s.data(), s.size(), ec);
p.write(fi.text.data(), fi.text.size(), ec);
if( ec.failed() )
throw system::system_error( ec );
}
return clock_type::now() - start;
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
null_parser p( get_parse_options() );
Expand Down Expand Up @@ -591,8 +599,7 @@ class boost_null_impl : public any_impl
}

clock_type::duration
serialize(
string_view, std::size_t) const override
serialize_string(file_item const& fi, std::size_t) const override
{
return clock_type::duration(0);
}
Expand All @@ -612,9 +619,7 @@ class boost_simple_impl : public any_impl
{}

clock_type::duration
parse(
string_view s,
std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
while(repeat--)
Expand All @@ -624,14 +629,15 @@ class boost_simple_impl : public any_impl
if( is_pool_ )
sp = &mr;

auto jv = json::parse( s, std::move(sp), get_parse_options() );
auto jv = json::parse(
fi.text, std::move(sp), get_parse_options() );
(void)jv;
}
return clock_type::now() - start;
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
while(repeat--)
Expand All @@ -650,15 +656,13 @@ class boost_simple_impl : public any_impl
}

clock_type::duration
serialize(
string_view s,
std::size_t repeat) const override
serialize_string(file_item const& fi, std::size_t repeat) const override
{
monotonic_resource mr;
storage_ptr sp;
if( is_pool_ )
sp = &mr;
auto jv = json::parse( s, std::move(sp) );
auto jv = json::parse( fi.text, std::move(sp) );

auto const start = clock_type::now();
std::string out;
Expand All @@ -680,9 +684,9 @@ class boost_operator_impl : public any_impl
{}

clock_type::duration
parse(string_view s, std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
std::istringstream is(s);
std::istringstream is(fi.text);
is.exceptions(std::ios::failbit);

auto const start = clock_type::now();
Expand All @@ -701,7 +705,7 @@ class boost_operator_impl : public any_impl
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
while(repeat--)
Expand All @@ -721,16 +725,14 @@ class boost_operator_impl : public any_impl
}

clock_type::duration
serialize(
string_view s,
std::size_t repeat) const override
serialize_string(file_item const& fi, std::size_t repeat) const override
{
monotonic_resource mr;
storage_ptr sp;
if( is_pool_ )
sp = &mr;

auto jv = json::parse( s, std::move(sp) );
auto jv = json::parse( fi.text, std::move(sp) );

auto const start = clock_type::now();
std::string out;
Expand All @@ -755,23 +757,21 @@ struct rapidjson_crt_impl : public any_impl
{}

clock_type::duration
parse(
string_view s, std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
using namespace rapidjson;
auto const start = clock_type::now();
while(repeat--)
{
CrtAllocator alloc;
GenericDocument<
UTF8<>, CrtAllocator> d(&alloc);
d.Parse(s.data(), s.size());
GenericDocument<UTF8<>, CrtAllocator> d(&alloc);
d.Parse( fi.text.data(), fi.text.size() );
}
return clock_type::now() - start;
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
using namespace rapidjson;

Expand All @@ -785,8 +785,7 @@ struct rapidjson_crt_impl : public any_impl
std::size_t const sz = fread(s, 1, fi.text.size(), f);

CrtAllocator alloc;
GenericDocument<
UTF8<>, CrtAllocator> d(&alloc);
GenericDocument<UTF8<>, CrtAllocator> d(&alloc);
d.Parse(s, sz);

fclose(f);
Expand All @@ -795,21 +794,19 @@ struct rapidjson_crt_impl : public any_impl
}

clock_type::duration
serialize(string_view s, std::size_t repeat) const override
serialize_string(file_item const& fi, std::size_t repeat) const override
{
using namespace rapidjson;
CrtAllocator alloc;
GenericDocument<
UTF8<>, CrtAllocator> d(&alloc);
d.Parse(s.data(), s.size());
GenericDocument<UTF8<>, CrtAllocator> d(&alloc);
d.Parse( fi.text.data(), fi.text.size() );

auto const start = clock_type::now();
rapidjson::StringBuffer st;
while(repeat--)
{
st.Clear();
rapidjson::Writer<
rapidjson::StringBuffer> wr(st);
rapidjson::Writer<rapidjson::StringBuffer> wr(st);
d.Accept(wr);
}
return clock_type::now() - start;
Expand All @@ -823,20 +820,19 @@ struct rapidjson_memory_impl : public any_impl
{}

clock_type::duration
parse(
string_view s, std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
while(repeat--)
{
rapidjson::Document d;
d.Parse(s.data(), s.size());
d.Parse(fi.text.data(), fi.text.size());
}
return clock_type::now() - start;
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
using namespace rapidjson;

Expand All @@ -858,18 +854,17 @@ struct rapidjson_memory_impl : public any_impl
}

clock_type::duration
serialize(string_view s, std::size_t repeat) const override
serialize_string(file_item const& fi, std::size_t repeat) const override
{
rapidjson::Document d;
d.Parse(s.data(), s.size());
d.Parse( fi.text.data(), fi.text.size() );

auto const start = clock_type::now();
rapidjson::StringBuffer st;
while(repeat--)
{
st.Clear();
rapidjson::Writer<
rapidjson::StringBuffer> wr(st);
rapidjson::Writer<rapidjson::StringBuffer> wr(st);
d.Accept(wr);
}
return clock_type::now() - start;
Expand All @@ -887,19 +882,18 @@ struct nlohmann_impl : public any_impl
{}

clock_type::duration
parse(string_view s, std::size_t repeat) const override
parse_string(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
while(repeat--)
{
auto jv = nlohmann::json::parse(
s.begin(), s.end());
auto jv = nlohmann::json::parse( fi.text.begin(), fi.text.end() );
}
return clock_type::now() - start;
}

clock_type::duration
parse(file_item const& fi, std::size_t repeat) const override
parse_file(file_item const& fi, std::size_t repeat) const override
{
auto const start = clock_type::now();
char* s = new char[ fi.text.size() ];
Expand All @@ -918,10 +912,9 @@ struct nlohmann_impl : public any_impl
}

clock_type::duration
serialize(string_view s, std::size_t repeat) const override
serialize_string(file_item const& fi, std::size_t repeat) const override
{
auto jv = nlohmann::json::parse(
s.begin(), s.end());
auto jv = nlohmann::json::parse( fi.text.begin(), fi.text.end() );

auto const start = clock_type::now();
while(repeat--)
Expand Down

0 comments on commit 7113c5b

Please sign in to comment.