11#include " ../../src/from_json.hpp"
22#include " twitter_data.hpp"
33#include " nlohmann_twitter_data.hpp"
4- #include " event_counter.h"
54#include < cassert>
65#include < cstdlib>
76#include < ctime>
1211#include < simdjson.h>
1312#include < fstream>
1413#include < nlohmann/json.hpp>
15- #include < rfl.hpp>
16- #include < rfl/json.hpp>
17-
18- event_collector collector;
19-
20- template <class function_type >
21- event_aggregate bench (const function_type &function, size_t min_repeat = 10 ,
22- size_t min_time_ns = 1000000000 ,
23- size_t max_repeat = 100000 ) {
24- event_aggregate aggregate{};
25- size_t N = min_repeat;
26- if (N == 0 ) {
27- N = 1 ;
28- }
29- for (size_t i = 0 ; i < N; i++) {
30- std::atomic_thread_fence (std::memory_order_acquire);
31- collector.start ();
32- function ();
33- std::atomic_thread_fence (std::memory_order_release);
34- event_count allocate_count = collector.end ();
35- aggregate << allocate_count;
36- if ((i + 1 == N) && (aggregate.total_elapsed_ns () < min_time_ns) &&
37- (N < max_repeat)) {
38- N *= 10 ;
39- }
40- }
41- return aggregate;
42- }
43-
44- // Source of the 2 functions below:
45- // https://github.com/simdutf/simdutf/blob/master/benchmarks/base64/benchmark_base64.cpp
46- void pretty_print (size_t strings, size_t bytes, std::string name, event_aggregate agg) {
47- printf (" %-60s : " , name.c_str ());
48- printf (" %5.2f MB/s " , bytes * 1000 / agg.elapsed_ns ());
49- printf (" %5.2f Ms/s " , strings * 1000 / agg.elapsed_ns ());
50- if (collector.has_events ()) {
51- printf (" %5.2f GHz " , agg.cycles () / agg.elapsed_ns ());
52- printf (" %5.2f c/b " , agg.cycles () / bytes);
53- printf (" %5.2f i/b " , agg.instructions () / bytes);
54- printf (" %5.2f i/c " , agg.instructions () / agg.cycles ());
55- }
56- printf (" \n " );
57- }
14+ #include " benchmark_helper.hpp"
15+ #if SIMDJSON_BENCH_CPP_REFLECT
16+ #include " benchmark_reflect_serialization_twitter.hpp"
17+ #endif
5818
5919template <class T >
6020void bench_fast_simpler (T &data) {
@@ -92,21 +52,6 @@ void bench_nlohmann(TwitterData &data) {
9252 );
9353}
9454
95- void bench_reflect_cpp (TwitterData& data) {
96- std::string output = rfl::json::write (data);
97- size_t output_volume = output.size ();
98- printf (" # output volume: %zu bytes\n " , output_volume);
99-
100- volatile size_t measured_volume = 0 ;
101- pretty_print (
102- 1 , output_volume, " bench_reflect_cpp" ,
103- bench ([&data, &measured_volume, &output_volume] () {
104- std::string output = rfl::json::write (data);
105- measured_volume = output.size ();
106- if (measured_volume != output_volume) { printf (" mismatch\n " ); }
107- })
108- );
109- }
11055
11156size_t WriteCallback (void * contents, size_t size, size_t nmemb, void * userp) {
11257 ((std::string*)userp)->append ((char *)contents, size * nmemb);
@@ -233,7 +178,8 @@ int main()
233178 experimental_json_builder::from_json (json_value, my_struct);
234179 bench_fast_simpler (my_struct);
235180 bench_nlohmann (my_struct);
181+ #if SIMDJSON_BENCH_CPP_REFLECT
236182 bench_reflect_cpp (my_struct);
237-
183+ # endif
238184 return EXIT_SUCCESS;
239185}
0 commit comments