From 314a6b54228c93d4a6fe1ba306712bb8872a9ba3 Mon Sep 17 00:00:00 2001 From: slaren Date: Thu, 17 Aug 2023 00:12:40 +0200 Subject: [PATCH] fix json formatting --- examples/llama-bench/llama-bench.cpp | 49 ++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/examples/llama-bench/llama-bench.cpp b/examples/llama-bench/llama-bench.cpp index e815969ad..b963ae29b 100755 --- a/examples/llama-bench/llama-bench.cpp +++ b/examples/llama-bench/llama-bench.cpp @@ -584,30 +584,51 @@ struct csv_printer : public printer { }; struct json_printer : public printer { + bool first = true; + void print_fields(const std::vector & fields, const std::vector & values) { assert(fields.size() == values.size()); for (size_t i = 0; i < fields.size(); i++) { - fprintf(fout, " \"%s\": \"%s\",\n", fields.at(i).c_str(), values.at(i).c_str()); + fprintf(fout, " \"%s\": \"%s\"", fields.at(i).c_str(), values.at(i).c_str()); + if (i < fields.size() - 1) { + fprintf(fout, ",\n"); + } else { + fprintf(fout, "\n"); + } } } + virtual void print_header(const cmd_params & params) { + fprintf(fout, "[\n"); + (void) params; + } + + virtual void print_footer() { + fprintf(fout, "\n]\n"); + } + virtual void print_test(const test & t) { - fprintf(fout, "{\n"); - fprintf(fout, " \"model\": {\n"); + if (first) { + first = false; + } else { + fprintf(fout, ",\n"); + } + fprintf(fout, " {\n"); + fprintf(fout, " \"model\": {\n"); print_fields(model_params::get_fields(), t.mparams.get_values()); - fprintf(fout, " },\n"); - fprintf(fout, " \"benchmark\": {\n"); + fprintf(fout, " },\n"); + fprintf(fout, " \"benchmark\": {\n"); print_fields(bench_params::get_fields(), t.bparams.get_values()); - fprintf(fout, " },\n"); - fprintf(fout, " \"backend\": {\n"); + fprintf(fout, " },\n"); + fprintf(fout, " \"backend\": {\n"); print_fields(backend_params::get_fields(), t.bkparams.get_values()); - fprintf(fout, " },\n"); - fprintf(fout, " \"samples\": {\n"); - fprintf(fout, " \"ns\": [ %s ],\n", join(t.tsamples.t_ns, ", ").c_str()); - fprintf(fout, " \"avg\": %" PRIu64 ",\n", t.tsamples.avg()); - fprintf(fout, " \"stddev\": %" PRIu64 "\n", t.tsamples.stdev()); - fprintf(fout, " }\n"); - fprintf(fout, "}\n"); + fprintf(fout, " },\n"); + fprintf(fout, " \"samples\": {\n"); + fprintf(fout, " \"ns\": [ %s ],\n", join(t.tsamples.t_ns, ", ").c_str()); + fprintf(fout, " \"avg\": %" PRIu64 ",\n", t.tsamples.avg()); + fprintf(fout, " \"stddev\": %" PRIu64 "\n", t.tsamples.stdev()); + fprintf(fout, " }\n"); + fprintf(fout, " }"); } };