diff --git a/wpilibc/src/main/native/cpp/ScopedTracer.cpp b/wpilibc/src/main/native/cpp/ScopedTracer.cpp new file mode 100644 index 0000000000..2024a65d52 --- /dev/null +++ b/wpilibc/src/main/native/cpp/ScopedTracer.cpp @@ -0,0 +1,22 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2020 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "frc/ScopedTracer.h" + +#include + +using namespace frc; + +ScopedTracer::ScopedTracer(wpi::Twine name, wpi::raw_ostream& os) + : m_name(name.str()), m_os(os) { + m_tracer.ResetTimer(); +} + +ScopedTracer::~ScopedTracer() { + m_tracer.AddEpoch(m_name); + m_tracer.PrintEpochs(m_os); +} diff --git a/wpilibc/src/main/native/include/frc/ScopedTracer.h b/wpilibc/src/main/native/include/frc/ScopedTracer.h new file mode 100644 index 0000000000..8634c1edd0 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/ScopedTracer.h @@ -0,0 +1,48 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2020 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include +#include + +#include "frc/Tracer.h" + +namespace wpi { +class raw_ostream; +} // namespace wpi + +namespace frc { +/** + * A class for keeping track of how much time it takes for different + * parts of code to execute. This class uses RAII, meaning you simply + * need to create an instance at the top of the block you are timing. After the + * block finishes execution (i.e. when the ScopedTracer instance gets + * destroyed), the epoch is printed to the provided raw_ostream. + */ +class ScopedTracer { + public: + /** + * Constructs a ScopedTracer instance. + * + * @param name The name of the epoch. + * @param os A reference to the raw_ostream to print data to. + */ + ScopedTracer(wpi::Twine name, wpi::raw_ostream& os); + ~ScopedTracer(); + + ScopedTracer(const ScopedTracer&) = delete; + ScopedTracer& operator=(const ScopedTracer&) = delete; + + private: + Tracer m_tracer; + std::string m_name; + wpi::raw_ostream& m_os; +}; +} // namespace frc diff --git a/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp b/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp new file mode 100644 index 0000000000..da222de610 --- /dev/null +++ b/wpilibc/src/test/native/cpp/ScopedTracerTest.cpp @@ -0,0 +1,33 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2020 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include +#include + +#include +#include +#include + +#include "frc/ScopedTracer.h" +#include "gtest/gtest.h" + +wpi::SmallString<128> buf; +wpi::raw_svector_ostream os(buf); + +#ifdef __APPLE__ +TEST(ScopedTracerTest, DISABLED_Timing) { +#else +TEST(ScopedTracerTest, Timing) { +#endif + { + frc::ScopedTracer tracer("timing_test", os); + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + } + + wpi::StringRef out = os.str(); + EXPECT_TRUE(out.startswith(" timing_test: 1.5")); +}