Add web performance tracing to the MEDIAPIPE_PROFILING repertoire

This records the MEDIAPIPE_PROFILING tracing annotations to the browser's trace using the user timing API. See https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API

To enable, build with --define MEDIAPIPE_WEB_PROFILING=1 --define DRISHTI_PROFILING=1

PiperOrigin-RevId: 502422030
This commit is contained in:
Camillo Lugaresi 2023-01-16 12:57:44 -08:00 committed by Copybara-Service
parent ffd8486d0d
commit c1f5920ecf
2 changed files with 88 additions and 0 deletions

View File

@ -127,6 +127,7 @@ cc_library(
"//mediapipe/framework/port:status",
"//mediapipe/framework/port:advanced_proto_lite",
"//mediapipe/framework/tool:name_util",
":web_performance_profiling",
] + select({
"//conditions:default": [],
}) + select({
@ -275,6 +276,25 @@ cc_test(
],
)
config_setting(
name = "mediapipe_web_profiling_enabled",
values = {
"define": "MEDIAPIPE_WEB_PROFILING=1",
},
visibility = ["//visibility:private"],
)
cc_library(
name = "web_performance_profiling",
hdrs = ["web_performance_profiling.h"],
defines = select({
":mediapipe_web_profiling_enabled": ["MEDIAPIPE_WEB_PROFILING_ENABLED"],
"//conditions:default": [],
}),
visibility = ["//mediapipe:__subpackages__"],
deps = ["@com_google_absl//absl/strings"],
)
cc_library(
name = "profiler_resource_util",
srcs = ["profiler_resource_util_common.cc"] + select({

View File

@ -0,0 +1,68 @@
#ifndef MEDIAPIPE_FRAMEWORK_PROFILER_WEB_PERFORMANCE_PROFILING_H_
#define MEDIAPIPE_FRAMEWORK_PROFILER_WEB_PERFORMANCE_PROFILING_H_
#if MEDIAPIPE_WEB_PROFILING_ENABLED && __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#include "absl/strings/str_cat.h"
// This records MediaPipe profiling events in the browser's performance trace.
// To use, build with:
// --define MEDIAPIPE_PROFILING=1 --define MEDIAPIPE_WEB_PROFILING=1
namespace mediapipe {
class WepPerformanceTraceScope {
public:
explicit WepPerformanceTraceScope(TraceEvent::EventType event_type,
const char* event_type_str,
CalculatorContext* cc)
: event_type_str_(event_type_str), cc_(cc) {
const auto& calculator_name = cc->NodeName();
std::string start_name =
absl::StrCat(calculator_name, "::", event_type_str_, "_start");
std::string timestamp_str = cc->InputTimestamp().DebugString();
EM_ASM(
{
const startName = UTF8ToString($0);
const timestamp = UTF8ToString($1);
performance.mark(startName, {mp_timestamp : timestamp});
},
start_name.c_str(), timestamp_str.c_str());
}
~WepPerformanceTraceScope() {
const auto& calculator_name = cc_->NodeName();
std::string start_name =
absl::StrCat(calculator_name, "::", event_type_str_, "_start");
std::string end_name =
absl::StrCat(calculator_name, "::", event_type_str_, "_end");
std::string measure_name =
absl::StrCat(calculator_name, "::", event_type_str_);
EM_ASM(
{
const startName = UTF8ToString($0);
const endName = UTF8ToString($1);
const measureName = UTF8ToString($2);
performance.mark(endName);
performance.measure(measureName, startName, endName);
},
start_name.c_str(), end_name.c_str(), measure_name.c_str());
}
private:
const char* event_type_str_;
CalculatorContext* cc_;
};
} // namespace mediapipe
#define MEDIAPIPE_WEB_PERFORMANCE_SCOPE(event_type, calculator_context) \
mediapipe::WepPerformanceTraceScope web_trace_scope( \
mediapipe::TraceEvent::event_type, #event_type, calculator_context)
#else
#define MEDIAPIPE_WEB_PERFORMANCE_SCOPE(event_type, calculator_context)
#endif // MEDIAPIPE_WEB_PROFILING_ENABLED && __EMSCRIPTEN__
#endif // MEDIAPIPE_FRAMEWORK_PROFILER_WEB_PERFORMANCE_PROFILING_H_