From dc6e76f7b7b6b9f1dc1a0f39d4503d05867ec916 Mon Sep 17 00:00:00 2001 From: MQY <3463526515@qq.com> Date: Tue, 22 Jul 2025 09:16:42 +0000 Subject: [PATCH] feat(batched): Add functionality to upload benchmark test results - Add the upload field in common.h to control whether to upload benchmark test results - Implement the benchmark result saving and upload functionality in batched.cpp - Added the --upload command-line parameter to enable the upload feature - Add upload logic in the main function, including generating timestamp filenames, saving benchmark results, etc --- common/common.h | 1 + examples/batched/batched.cpp | 46 ++++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/common/common.h b/common/common.h index 11427c51f6934..eb38e5f3fb95b 100644 --- a/common/common.h +++ b/common/common.h @@ -447,6 +447,7 @@ struct common_params { // batched-bench params bool batched_bench_output_jsonl = false; + bool upload = false; // whether to upload benchmark results // common params std::string out_file; // output filename for all example programs diff --git a/examples/batched/batched.cpp b/examples/batched/batched.cpp index 1a5de5928a526..4222e56208edf 100644 --- a/examples/batched/batched.cpp +++ b/examples/batched/batched.cpp @@ -5,24 +5,37 @@ #include #include +#include +#include #include #include +#include static void print_usage(int, char ** argv) { LOG("\nexample usage:\n"); LOG("\n %s -m model.gguf -p \"Hello my name is\" -n 32 -np 4\n", argv[0]); LOG("\n"); + LOG(" --upload enable to submit benchmark results to public repository\n"); + LOG("\n"); } int main(int argc, char ** argv) { - common_params params; +common_params params; - params.prompt = "Hello my name is"; - params.n_predict = 32; +params.prompt = "Hello my name is"; +params.n_predict = 32; +params.upload = false; - if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) { - return 1; +if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) { + return 1; +} + +// Parse --upload flag after common_params_parse +for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--upload") == 0) { + params.upload = true; } +} common_init(); @@ -242,5 +255,28 @@ int main(int argc, char ** argv) { llama_backend_free(); + if (params.upload) { + // Get current time for filename + time_t now = time(nullptr); + char timestamp[20]; + strftime(timestamp, sizeof(timestamp), "%Y%m%d-%H%M%S", localtime(&now)); + std::string filename = "llama-bench-" + std::string(timestamp) + ".json"; + FILE * f = fopen(filename.c_str(), "w"); + if (f) { + fprintf(f, "{\n"); + fprintf(f, " \"model\": \"%s\",\n", params.model.path.c_str()); + fprintf(f, " \"n_predict\": %d,\n", params.n_predict); + fprintf(f, " \"n_parallel\": %d,\n", params.n_parallel); + fprintf(f, " \"tokens_per_second\": %f,\n", n_decode / ((t_main_end - t_main_start) / 1000000.0f)); + fprintf(f, " \"system_info\": \"%s\"\n", common_params_get_system_info(params).c_str()); + fprintf(f, "}\n"); + fclose(f); + LOG_INF("Benchmark results saved to %s\n", filename.c_str()); + LOG_INF("To upload, please visit https://github.com/ggerganov/llamacpp-bench and follow the instructions.\n"); + } else { + LOG_ERR("Failed to open %s for writing\n", filename.c_str()); + } + } + return 0; }