Save an integer id in graph profiler objects to distinguish between different profiler instances during benchmarking.

PiperOrigin-RevId: 498409363
This commit is contained in:
MediaPipe Team 2022-12-29 10:16:10 -08:00 committed by Copybara-Service
parent aaa16eca1f
commit 60c6b155f6
3 changed files with 36 additions and 0 deletions

View File

@ -194,6 +194,7 @@ void GraphProfiler::Initialize(
"Calculator \"$0\" has already been added.", node_name);
}
profile_builder_ = std::make_unique<GraphProfileBuilder>(this);
graph_id_ = ++next_instance_id_;
is_initialized_ = true;
}

View File

@ -237,6 +237,9 @@ class GraphProfiler : public std::enable_shared_from_this<ProfilingContext> {
return validated_graph_;
}
// Gets a numerical identifier for this GraphProfiler object.
uint64_t GetGraphId() { return graph_id_; }
private:
// This can be used to add packet info for the input streams to the graph.
// It treats the stream defined by |stream_name| as a stream produced by a
@ -357,6 +360,12 @@ class GraphProfiler : public std::enable_shared_from_this<ProfilingContext> {
class GraphProfileBuilder;
std::unique_ptr<GraphProfileBuilder> profile_builder_;
// The globally incrementing identifier for all graphs in a process.
static inline std::atomic_int next_instance_id_ = 0;
// A unique identifier for this object. Only unique within a process.
uint64_t graph_id_;
// For testing.
friend GraphProfilerTestPeer;
};

View File

@ -442,6 +442,32 @@ TEST_F(GraphProfilerTestPeer, InitializeMultipleTimes) {
"Cannot initialize .* multiple times.");
}
// Tests that graph identifiers are not reused, even after destruction.
TEST_F(GraphProfilerTestPeer, InitializeMultipleProfilers) {
auto raw_graph_config = R"(
profiler_config {
enable_profiler: true
}
input_stream: "input_stream"
node {
calculator: "DummyTestCalculator"
input_stream: "input_stream"
})";
const int n_iterations = 100;
absl::flat_hash_set<int> seen_ids;
for (int i = 0; i < n_iterations; ++i) {
std::shared_ptr<ProfilingContext> profiler =
std::make_shared<ProfilingContext>();
auto graph_config = CreateGraphConfig(raw_graph_config);
mediapipe::ValidatedGraphConfig validated_graph;
QCHECK_OK(validated_graph.Initialize(graph_config));
profiler->Initialize(validated_graph);
int id = profiler->GetGraphId();
ASSERT_THAT(seen_ids, testing::Not(testing::Contains(id)));
seen_ids.insert(id);
}
}
// Tests that Pause(), Resume(), and Reset() works.
TEST_F(GraphProfilerTestPeer, PauseResumeReset) {
InitializeProfilerWithGraphConfig(R"(