]> git.lizzy.rs Git - dragonfireclient.git/blob - src/benchmark/benchmark_serialize.cpp
Add benchmarks for json string serialize/deserialize (#12258)
[dragonfireclient.git] / src / benchmark / benchmark_serialize.cpp
1 /*
2 Minetest
3 Copyright (C) 2022 Minetest Authors
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "benchmark_setup.h"
21 #include "util/serialize.h"
22 #include <sstream>
23 #include <ios>
24
25 // Builds a string of exactly `length` characters by repeating `s` (rest cut off)
26 static std::string makeRepeatTo(const std::string &s, size_t length)
27 {
28         std::string v;
29         v.reserve(length + s.size());
30         for (size_t i = 0; i < length; i += s.size()) {
31                 v += s;
32         }
33         v.resize(length);
34         return v;
35 }
36
37 #define BENCH3(_label, _chars, _length, _lengthlabel) \
38         BENCHMARK_ADVANCED("serializeJsonStringIfNeeded_" _lengthlabel "_" _label)(Catch::Benchmark::Chronometer meter) { \
39                 std::string s = makeRepeatTo(_chars, _length); \
40                 meter.measure([&] { return serializeJsonStringIfNeeded(s); }); \
41         }; \
42         BENCHMARK_ADVANCED("deSerializeJsonStringIfNeeded_" _lengthlabel "_" _label)(Catch::Benchmark::Chronometer meter) { \
43                 std::string s = makeRepeatTo(_chars, _length); \
44                 std::string serialized = serializeJsonStringIfNeeded(s); \
45                 std::istringstream is(serialized, std::ios::binary); \
46                 meter.measure([&] { \
47                         is.clear(); \
48                         is.seekg(0, std::ios::beg); \
49                         return deSerializeJsonStringIfNeeded(is); \
50                 }); \
51         };
52
53 /* Both with and without a space character (' ') */
54 #define BENCH2(_label, _chars, _length, _lengthlabel) \
55         BENCH3(_label, _chars, _length, _lengthlabel) \
56         BENCH3(_label "_with_space", " " _chars, _length, _lengthlabel) \
57
58 /* Iterate over input lengths */
59 #define BENCH1(_label, _chars) \
60         BENCH2(_label, _chars, 10, "small") \
61         BENCH2(_label, _chars, 10000, "large")
62
63 /* Iterate over character sets */
64 #define BENCH_ALL() \
65         BENCH1("alpha", "abcdefghijklmnopqrstuvwxyz") \
66         BENCH1("escaped", "\"\\/\b\f\n\r\t") \
67         BENCH1("nonascii", "\xf0\xff")
68
69 TEST_CASE("benchmark_serialize") {
70         BENCH_ALL()
71 }