File: json_perf_common.hpp

package info (click to toggle)
glaze 7.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,036 kB
  • sloc: cpp: 142,035; sh: 109; ansic: 26; makefile: 12
file content (95 lines) | stat: -rw-r--r-- 3,302 bytes parent folder | download | duplicates (3)
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
// Common utilities for JSON performance tests
#pragma once

#include <iostream>
#include <optional>
#include <string_view>

namespace glz::perf
{
   // We scale all speeds by the minified JSON byte length, so that libraries which do not efficiently write JSON do not
   // get an unfair advantage. We want to know how fast the libraries will serialize/deserialize with respect to one
   // another.
   inline size_t minified_byte_length{};

   struct results
   {
      std::string_view name{};
      std::string_view url{};
      size_t iterations{};

      std::optional<size_t> json_byte_length{};
      std::optional<double> json_read{};
      std::optional<double> json_write{};
      std::optional<double> json_roundtrip{};

      std::optional<size_t> binary_byte_length{};
      std::optional<double> beve_write{};
      std::optional<double> beve_read{};
      std::optional<double> beve_roundtrip{};

      void print(bool use_minified = true)
      {
         if (json_roundtrip) {
            std::cout << name << " json roundtrip: " << *json_roundtrip << " s\n";
         }

         if (json_byte_length) {
            std::cout << name << " json byte length: " << *json_byte_length << '\n';
         }

         if (json_write) {
            if (json_byte_length) {
               const auto byte_length = use_minified ? minified_byte_length : *json_byte_length;
               const auto MBs = iterations * byte_length / (*json_write * 1048576);
               std::cout << name << " json write: " << *json_write << " s, " << MBs << " MB/s\n";
            }
            else {
               std::cout << name << " json write: " << *json_write << " s\n";
            }
         }

         if (json_read) {
            if (json_byte_length) {
               const auto byte_length = use_minified ? minified_byte_length : *json_byte_length;
               const auto MBs = iterations * byte_length / (*json_read * 1048576);
               std::cout << name << " json read: " << *json_read << " s, " << MBs << " MB/s\n";
            }
            else {
               std::cout << name << " json read: " << *json_read << " s\n";
            }
         }

         if (beve_roundtrip) {
            std::cout << '\n';
            std::cout << name << " beve roundtrip: " << *beve_roundtrip << " s\n";
         }

         if (binary_byte_length) {
            std::cout << name << " beve byte length: " << *binary_byte_length << '\n';
         }

         if (beve_write) {
            if (binary_byte_length) {
               const auto MBs = iterations * *binary_byte_length / (*beve_write * 1048576);
               std::cout << name << " beve write: " << *beve_write << " s, " << MBs << " MB/s\n";
            }
            else {
               std::cout << name << " beve write: " << *beve_write << " s\n";
            }
         }

         if (beve_read) {
            if (binary_byte_length) {
               const auto MBs = iterations * *binary_byte_length / (*beve_read * 1048576);
               std::cout << name << " beve read: " << *beve_read << " s, " << MBs << " MB/s\n";
            }
            else {
               std::cout << name << " beve read: " << *beve_read << " s\n";
            }
         }

         std::cout << "\n---\n" << std::endl;
      }
   };
}