Skip to content

Commit

Permalink
JSON reporter: store library version and schema version in context
Browse files Browse the repository at this point in the history
  • Loading branch information
LebedevRI committed Jan 23, 2024
1 parent 74d03ac commit 27cb27a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ target_include_directories(benchmark PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)

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)
Expand Down
12 changes: 12 additions & 0 deletions src/json_reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string>* global_context =
internal::GetGlobalContext();
Expand Down
3 changes: 3 additions & 0 deletions test/reporter_output_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 27cb27a

Please sign in to comment.