Skip to content

Commit

Permalink
chore: add benchmarch for json5
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 26, 2024
1 parent 3c26f9b commit ad54795
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
#include <string>

#include "json.hpp"
#include "json5.hpp"

template <typename accel, typename tag_type>
void do_benchmark(const std::string& content, const tag_type& tag)
template <typename parser>
void do_benchmark(const std::string& content, const std::string& tag)
{
std::vector<double> iteration_time_ms;
iteration_time_ms.reserve(10000);
auto start = std::chrono::steady_clock::now();
bool parsed = false;

while (true) {
while (true)
{
auto t0 = std::chrono::steady_clock::now();
auto opt = json::parser<std::string, std::string, accel>::parse(content);
auto opt = parser::parse(content);
parsed = opt.has_value();

auto t1 = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -49,7 +50,8 @@ void do_benchmark(const std::string& content, const tag_type& tag)
median = iteration_time_ms[loop_times / 2];
}

std::cout << tag << "," << std::boolalpha << parsed << "," << loop_times << "," << mean << "," << median << "," << stdev << std::endl;
std::cout << tag << ", " << std::boolalpha << parsed << ", " << loop_times << ", " << mean << ", " << median << ", "
<< stdev << std::endl;
}

int main(int argc, char** argv)
Expand All @@ -59,7 +61,7 @@ int main(int argc, char** argv)
return -1;
}

std::cout << "file,parsed,acceleration,sample_count,mean,median,stdev" << std::endl;
std::cout << "file, parser, ret, sample_count, mean, median, stdev" << std::endl;

for (int i = 1; i < argc; ++i) {
std::filesystem::path path(argv[i]);
Expand All @@ -77,16 +79,23 @@ int main(int argc, char** argv)

using namespace json::_packed_bytes;

do_benchmark<packed_bytes_trait_none>(content, path.filename().string() + ",none");
do_benchmark<packed_bytes_trait_uint32>(content, path.filename().string() + ",bits32");
do_benchmark<packed_bytes_trait_uint64>(content, path.filename().string() + ",bits64");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait_none>>(
content, path.filename().string() + ", none");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait_uint32>>(
content, path.filename().string() + ", bits32");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait_uint64>>(
content, path.filename().string() + ", bits64");

if constexpr (packed_bytes_trait<16>::available) {
do_benchmark<packed_bytes_trait<16>>(content, path.filename().string() + ",simd128");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait<16>>>(
content, path.filename().string() + ", simd128");
}
if constexpr (packed_bytes_trait<32>::available) {
do_benchmark<packed_bytes_trait<32>>(content, path.filename().string() + ",simd256");
do_benchmark<json::parser<std::string, std::string, packed_bytes_trait<32>>>(
content, path.filename().string() + ", simd256");
}

do_benchmark<json::parser5<std::string>>(content, path.filename().string() + ", json5");
}

return 0;
Expand Down

0 comments on commit ad54795

Please sign in to comment.