Skip to content

Commit 7113c5b

Browse files
committed
refactor choosing benchmark operation
1 parent d1c65b3 commit 7113c5b

File tree

1 file changed

+69
-76
lines changed

1 file changed

+69
-76
lines changed

bench/bench.cpp

Lines changed: 69 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ class any_impl
9898
bool with_file_io_ = false;
9999

100100
public:
101+
using operation = auto (any_impl::*)
102+
(file_item const& fi, std::size_t repeat) const
103+
-> clock_type::duration;
104+
101105
any_impl(
102106
string_view base_name,
103107
bool is_boost,
@@ -148,15 +152,21 @@ class any_impl
148152

149153
virtual
150154
clock_type::duration
151-
parse(string_view s, std::size_t repeat) const = 0;
155+
parse_string(file_item const& fi, std::size_t repeat) const = 0;
152156

153157
virtual
154158
clock_type::duration
155-
parse(file_item const& fi, std::size_t repeat) const = 0;
159+
parse_file(file_item const& fi, std::size_t repeat) const = 0;
156160

157161
virtual
158162
clock_type::duration
159-
serialize(string_view s, std::size_t repeat) const = 0;
163+
serialize_string(file_item const& fi, std::size_t repeat) const = 0;
164+
165+
clock_type::duration
166+
skip(file_item const& , std::size_t ) const
167+
{
168+
return clock_type::duration::zero();
169+
}
160170

161171
string_view
162172
name() const noexcept
@@ -259,22 +269,26 @@ bench(
259269
{
260270
trial.clear();
261271
std::size_t repeat = 1;
262-
auto const f = [&]
263-
{
264-
if(verb == "Parse")
265-
{
266-
if( vi[j]->with_file_io() )
267-
return vi[j]->parse(vf[i], repeat);
268-
else
269-
return vi[j]->parse(vf[i].text, repeat);
270-
}
271272

273+
any_impl::operation op = nullptr;
274+
if(verb == "Parse")
275+
{
276+
if( vi[j]->with_file_io() )
277+
op = &any_impl::parse_file;
278+
else
279+
op = &any_impl::parse_string;
280+
}
281+
else
282+
{
272283
BOOST_ASSERT( verb == "Serialize" );
273284
if( vi[j]->with_file_io() )
274-
return clock_type::duration::zero();
285+
op = &any_impl::skip;
275286
else
276-
return vi[j]->serialize(vf[i].text, repeat);
277-
};
287+
op = &any_impl::serialize_string;
288+
}
289+
290+
auto const f = [&]{ return (vi[j].get()->*op)(vf[i], repeat); };
291+
278292
// helps with the caching, which reduces noise; also, we determine
279293
// if this configuration should be skipped altogether
280294
auto const elapsed = f();
@@ -360,9 +374,7 @@ class boost_impl : public any_impl
360374
{}
361375

362376
clock_type::duration
363-
parse(
364-
string_view s,
365-
std::size_t repeat) const override
377+
parse_string(file_item const& fi, std::size_t repeat) const override
366378
{
367379
auto const start = clock_type::now();
368380
parser p( {}, get_parse_options() );
@@ -374,15 +386,15 @@ class boost_impl : public any_impl
374386
sp = &mr;
375387
p.reset( std::move(sp) );
376388

377-
p.write( s.data(), s.size() );
389+
p.write( fi.text.data(), fi.text.size() );
378390
auto jv = p.release();
379391
(void)jv;
380392
}
381393
return clock_type::now() - start;
382394
}
383395

384396
clock_type::duration
385-
parse(file_item const& fi, std::size_t repeat) const override
397+
parse_file(file_item const& fi, std::size_t repeat) const override
386398
{
387399
auto const start = clock_type::now();
388400
stream_parser p( {}, get_parse_options() );
@@ -419,15 +431,13 @@ class boost_impl : public any_impl
419431
}
420432

421433
clock_type::duration
422-
serialize(
423-
string_view s,
424-
std::size_t repeat) const override
434+
serialize_string(file_item const& fi, std::size_t repeat) const override
425435
{
426436
monotonic_resource mr;
427437
storage_ptr sp;
428438
if( is_pool_ )
429439
sp = &mr;
430-
auto jv = json::parse( s, std::move(sp) );
440+
auto jv = json::parse( fi.text, std::move(sp) );
431441

432442
auto const start = clock_type::now();
433443
serializer sr;
@@ -535,25 +545,23 @@ class boost_null_impl : public any_impl
535545
{}
536546

537547
clock_type::duration
538-
parse(
539-
string_view s,
540-
std::size_t repeat) const override
548+
parse_string(file_item const& fi, std::size_t repeat) const override
541549
{
542550
auto const start = clock_type::now();
543551
null_parser p( get_parse_options() );
544552
while(repeat--)
545553
{
546554
p.reset();
547555
system::error_code ec;
548-
p.write(s.data(), s.size(), ec);
556+
p.write(fi.text.data(), fi.text.size(), ec);
549557
if( ec.failed() )
550558
throw system::system_error( ec );
551559
}
552560
return clock_type::now() - start;
553561
}
554562

555563
clock_type::duration
556-
parse(file_item const& fi, std::size_t repeat) const override
564+
parse_file(file_item const& fi, std::size_t repeat) const override
557565
{
558566
auto const start = clock_type::now();
559567
null_parser p( get_parse_options() );
@@ -591,8 +599,7 @@ class boost_null_impl : public any_impl
591599
}
592600

593601
clock_type::duration
594-
serialize(
595-
string_view, std::size_t) const override
602+
serialize_string(file_item const& fi, std::size_t) const override
596603
{
597604
return clock_type::duration(0);
598605
}
@@ -612,9 +619,7 @@ class boost_simple_impl : public any_impl
612619
{}
613620

614621
clock_type::duration
615-
parse(
616-
string_view s,
617-
std::size_t repeat) const override
622+
parse_string(file_item const& fi, std::size_t repeat) const override
618623
{
619624
auto const start = clock_type::now();
620625
while(repeat--)
@@ -624,14 +629,15 @@ class boost_simple_impl : public any_impl
624629
if( is_pool_ )
625630
sp = &mr;
626631

627-
auto jv = json::parse( s, std::move(sp), get_parse_options() );
632+
auto jv = json::parse(
633+
fi.text, std::move(sp), get_parse_options() );
628634
(void)jv;
629635
}
630636
return clock_type::now() - start;
631637
}
632638

633639
clock_type::duration
634-
parse(file_item const& fi, std::size_t repeat) const override
640+
parse_file(file_item const& fi, std::size_t repeat) const override
635641
{
636642
auto const start = clock_type::now();
637643
while(repeat--)
@@ -650,15 +656,13 @@ class boost_simple_impl : public any_impl
650656
}
651657

652658
clock_type::duration
653-
serialize(
654-
string_view s,
655-
std::size_t repeat) const override
659+
serialize_string(file_item const& fi, std::size_t repeat) const override
656660
{
657661
monotonic_resource mr;
658662
storage_ptr sp;
659663
if( is_pool_ )
660664
sp = &mr;
661-
auto jv = json::parse( s, std::move(sp) );
665+
auto jv = json::parse( fi.text, std::move(sp) );
662666

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

682686
clock_type::duration
683-
parse(string_view s, std::size_t repeat) const override
687+
parse_string(file_item const& fi, std::size_t repeat) const override
684688
{
685-
std::istringstream is(s);
689+
std::istringstream is(fi.text);
686690
is.exceptions(std::ios::failbit);
687691

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

703707
clock_type::duration
704-
parse(file_item const& fi, std::size_t repeat) const override
708+
parse_file(file_item const& fi, std::size_t repeat) const override
705709
{
706710
auto const start = clock_type::now();
707711
while(repeat--)
@@ -721,16 +725,14 @@ class boost_operator_impl : public any_impl
721725
}
722726

723727
clock_type::duration
724-
serialize(
725-
string_view s,
726-
std::size_t repeat) const override
728+
serialize_string(file_item const& fi, std::size_t repeat) const override
727729
{
728730
monotonic_resource mr;
729731
storage_ptr sp;
730732
if( is_pool_ )
731733
sp = &mr;
732734

733-
auto jv = json::parse( s, std::move(sp) );
735+
auto jv = json::parse( fi.text, std::move(sp) );
734736

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

757759
clock_type::duration
758-
parse(
759-
string_view s, std::size_t repeat) const override
760+
parse_string(file_item const& fi, std::size_t repeat) const override
760761
{
761762
using namespace rapidjson;
762763
auto const start = clock_type::now();
763764
while(repeat--)
764765
{
765766
CrtAllocator alloc;
766-
GenericDocument<
767-
UTF8<>, CrtAllocator> d(&alloc);
768-
d.Parse(s.data(), s.size());
767+
GenericDocument<UTF8<>, CrtAllocator> d(&alloc);
768+
d.Parse( fi.text.data(), fi.text.size() );
769769
}
770770
return clock_type::now() - start;
771771
}
772772

773773
clock_type::duration
774-
parse(file_item const& fi, std::size_t repeat) const override
774+
parse_file(file_item const& fi, std::size_t repeat) const override
775775
{
776776
using namespace rapidjson;
777777

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

787787
CrtAllocator alloc;
788-
GenericDocument<
789-
UTF8<>, CrtAllocator> d(&alloc);
788+
GenericDocument<UTF8<>, CrtAllocator> d(&alloc);
790789
d.Parse(s, sz);
791790

792791
fclose(f);
@@ -795,21 +794,19 @@ struct rapidjson_crt_impl : public any_impl
795794
}
796795

797796
clock_type::duration
798-
serialize(string_view s, std::size_t repeat) const override
797+
serialize_string(file_item const& fi, std::size_t repeat) const override
799798
{
800799
using namespace rapidjson;
801800
CrtAllocator alloc;
802-
GenericDocument<
803-
UTF8<>, CrtAllocator> d(&alloc);
804-
d.Parse(s.data(), s.size());
801+
GenericDocument<UTF8<>, CrtAllocator> d(&alloc);
802+
d.Parse( fi.text.data(), fi.text.size() );
805803

806804
auto const start = clock_type::now();
807805
rapidjson::StringBuffer st;
808806
while(repeat--)
809807
{
810808
st.Clear();
811-
rapidjson::Writer<
812-
rapidjson::StringBuffer> wr(st);
809+
rapidjson::Writer<rapidjson::StringBuffer> wr(st);
813810
d.Accept(wr);
814811
}
815812
return clock_type::now() - start;
@@ -823,20 +820,19 @@ struct rapidjson_memory_impl : public any_impl
823820
{}
824821

825822
clock_type::duration
826-
parse(
827-
string_view s, std::size_t repeat) const override
823+
parse_string(file_item const& fi, std::size_t repeat) const override
828824
{
829825
auto const start = clock_type::now();
830826
while(repeat--)
831827
{
832828
rapidjson::Document d;
833-
d.Parse(s.data(), s.size());
829+
d.Parse(fi.text.data(), fi.text.size());
834830
}
835831
return clock_type::now() - start;
836832
}
837833

838834
clock_type::duration
839-
parse(file_item const& fi, std::size_t repeat) const override
835+
parse_file(file_item const& fi, std::size_t repeat) const override
840836
{
841837
using namespace rapidjson;
842838

@@ -858,18 +854,17 @@ struct rapidjson_memory_impl : public any_impl
858854
}
859855

860856
clock_type::duration
861-
serialize(string_view s, std::size_t repeat) const override
857+
serialize_string(file_item const& fi, std::size_t repeat) const override
862858
{
863859
rapidjson::Document d;
864-
d.Parse(s.data(), s.size());
860+
d.Parse( fi.text.data(), fi.text.size() );
865861

866862
auto const start = clock_type::now();
867863
rapidjson::StringBuffer st;
868864
while(repeat--)
869865
{
870866
st.Clear();
871-
rapidjson::Writer<
872-
rapidjson::StringBuffer> wr(st);
867+
rapidjson::Writer<rapidjson::StringBuffer> wr(st);
873868
d.Accept(wr);
874869
}
875870
return clock_type::now() - start;
@@ -887,19 +882,18 @@ struct nlohmann_impl : public any_impl
887882
{}
888883

889884
clock_type::duration
890-
parse(string_view s, std::size_t repeat) const override
885+
parse_string(file_item const& fi, std::size_t repeat) const override
891886
{
892887
auto const start = clock_type::now();
893888
while(repeat--)
894889
{
895-
auto jv = nlohmann::json::parse(
896-
s.begin(), s.end());
890+
auto jv = nlohmann::json::parse( fi.text.begin(), fi.text.end() );
897891
}
898892
return clock_type::now() - start;
899893
}
900894

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

920914
clock_type::duration
921-
serialize(string_view s, std::size_t repeat) const override
915+
serialize_string(file_item const& fi, std::size_t repeat) const override
922916
{
923-
auto jv = nlohmann::json::parse(
924-
s.begin(), s.end());
917+
auto jv = nlohmann::json::parse( fi.text.begin(), fi.text.end() );
925918

926919
auto const start = clock_type::now();
927920
while(repeat--)

0 commit comments

Comments
 (0)