3 Copyright (C) 2022 Minetest Authors
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.
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.
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.
20 #include "benchmark_setup.h"
21 #include "util/serialize.h"
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)
29 v.reserve(length + s.size());
30 for (size_t i = 0; i < length; i += s.size()) {
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); }); \
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); \
48 is.seekg(0, std::ios::beg); \
49 return deSerializeJsonStringIfNeeded(is); \
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) \
58 /* Iterate over input lengths */
59 #define BENCH1(_label, _chars) \
60 BENCH2(_label, _chars, 10, "small") \
61 BENCH2(_label, _chars, 10000, "large")
63 /* Iterate over character sets */
65 BENCH1("alpha", "abcdefghijklmnopqrstuvwxyz") \
66 BENCH1("escaped", "\"\\/\b\f\n\r\t") \
67 BENCH1("nonascii", "\xf0\xff")
69 TEST_CASE("benchmark_serialize") {