Files
pytorch/test/cpp/monitor/test_events.cpp
T
Tristan Rice eb374de3f5 Back out "Revert D32606547: torch/monitor: add C++ events and handlers" (#69923)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69923

Original commit changeset: fbaf2cc06ad4

Original Phabricator Diff: D32606547 (https://github.com/pytorch/pytorch/commit/e61fc1c03b64e61ca4f5bbe278db7ee2cf35e8ff)

This is the same thing as the original diff but just using a normal std::mutex instead of std::shared_timed_mutex which is not available on OSX 10.11. The performance difference should be negligible and easy to change down the line if it does become a bottleneck.

Old failing build: https://github.com/pytorch/pytorch/runs/4495465412?check_suite_focus=true

Pull Request resolved: https://github.com/pytorch/pytorch/pull/68783

Test Plan:
buck test //caffe2/test/cpp/monitor:monitor

will add ciflow tags to ensure mac builds are fine

Reviewed By: aivanou

Differential Revision: D33102715

fbshipit-source-id: 3816ff01c578d8e844d303d881a63cf5c3817bdb
2021-12-15 22:51:43 -08:00

40 lines
887 B
C++

#include <gtest/gtest.h>
#include <torch/csrc/monitor/events.h>
using namespace torch::monitor;
struct AggregatingEventHandler : public EventHandler {
std::vector<Event> events;
void handle(const Event& e) override {
events.emplace_back(e);
}
};
TEST(EventsTest, EventHandler) {
Event e;
e.type = "test";
e.message = "test message";
e.timestamp = std::chrono::system_clock::now();
e.metadata["string"] = "asdf";
e.metadata["double"] = 1234.5678;
e.metadata["int"] = 1234L;
e.metadata["bool"] = true;
// log to nothing
logEvent(e);
auto handler = std::make_shared<AggregatingEventHandler>();
registerEventHandler(handler);
logEvent(e);
ASSERT_EQ(handler->events.size(), 1);
ASSERT_EQ(e, handler->events.at(0));
unregisterEventHandler(handler);
logEvent(e);
// handler unregister, didn't log
ASSERT_EQ(handler->events.size(), 1);
}