From 27cb27a38328adc3c27f06e46944be3514f1baeb Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Wed, 24 Jan 2024 02:33:35 +0300 Subject: [PATCH] JSON reporter: store library version and schema version in `context` --- src/CMakeLists.txt | 7 +++++++ src/json_reporter.cc | 12 ++++++++++++ test/reporter_output_test.cc | 3 +++ 3 files changed, 22 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index daf82fb131..4e5432d790 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,13 @@ target_include_directories(benchmark PUBLIC $ ) +set_property( + SOURCE json_reporter.cc + APPEND + PROPERTY COMPILE_DEFINITIONS + BENCHMARK_VERSION="${VERSION}" +) + # libpfm, if available if (PFM_FOUND) target_link_libraries(benchmark PRIVATE PFM::libpfm) diff --git a/src/json_reporter.cc b/src/json_reporter.cc index 6559dfd5e6..bb0b9a1bfc 100644 --- a/src/json_reporter.cc +++ b/src/json_reporter.cc @@ -167,12 +167,24 @@ bool JSONReporter::ReportContext(const Context& context) { } out << "],\n"; +#if defined(BENCHMARK_VERSION) + const char library_version[] = BENCHMARK_VERSION; +#else + const char library_version[] = "hello, bazel!"; +#endif + out << indent << FormatKV("library_version", library_version); + out << ",\n"; + #if defined(NDEBUG) const char build_type[] = "release"; #else const char build_type[] = "debug"; #endif out << indent << FormatKV("library_build_type", build_type); + out << ",\n"; + + // NOTE: our json schema is not strictly tied to the library version! + out << indent << FormatKV("json_schema_version", int64_t(1)); std::map* global_context = internal::GetGlobalContext(); diff --git a/test/reporter_output_test.cc b/test/reporter_output_test.cc index 657a9a1079..ea5381d20b 100644 --- a/test/reporter_output_test.cc +++ b/test/reporter_output_test.cc @@ -55,6 +55,9 @@ static int AddContextCases() { {{"Load Average: (%float, ){0,2}%float$", MR_Next}}); } AddCases(TC_JSONOut, {{"\"load_avg\": \\[(%float,?){0,3}],$", MR_Next}}); + AddCases(TC_JSONOut, {{"\"library_version\": \".*\",$", MR_Next}}); + AddCases(TC_JSONOut, {{"\"library_build_type\": \".*\",$", MR_Next}}); + AddCases(TC_JSONOut, {{"\"json_schema_version\": 1$", MR_Next}}); return 0; } int dummy_register = AddContextCases();