Commit 3a29305f authored by Dominik Charousset's avatar Dominik Charousset

Add convenience type for measuring times

parent b7f4a3f6
...@@ -313,6 +313,7 @@ caf_add_test_suites(caf-core-test ...@@ -313,6 +313,7 @@ caf_add_test_suites(caf-core-test
telemetry.histogram telemetry.histogram
telemetry.label telemetry.label
telemetry.metric_registry telemetry.metric_registry
telemetry.timer
thread_hook thread_hook
tracing_data tracing_data
type_id_list type_id_list
......
...@@ -241,6 +241,7 @@ class label_view; ...@@ -241,6 +241,7 @@ class label_view;
class metric; class metric;
class metric_family; class metric_family;
class metric_registry; class metric_registry;
class timer;
enum class metric_type : uint8_t; enum class metric_type : uint8_t;
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <chrono>
#include "caf/telemetry/histogram.hpp"
namespace caf::telemetry {
/// Convenience helper for measuring durations such as latency using a histogram
/// with second resolution. The measurement starts when creating the objects and
/// finishes when the timer goes out of scope.
class timer {
public:
using clock_type = std::chrono::steady_clock;
explicit timer(dbl_histogram* h) : h_(h) {
if (h)
start_ = clock_type::now();
}
~timer() {
using dbl_sec = std::chrono::duration<double>;
if (h_) {
auto end = clock_type::now();
h_->observe(std::chrono::duration_cast<dbl_sec>(end - start_).count());
}
}
auto histogram_ptr() const noexcept {
return h_;
}
auto started() const noexcept {
return start_;
}
private:
dbl_histogram* h_;
clock_type::time_point start_;
};
} // namespace caf::telemetry
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE telemetry.timer
#include "caf/telemetry/timer.hpp"
#include "caf/test/dsl.hpp"
using namespace caf;
using namespace caf::telemetry;
CAF_TEST(timers observe how much time passes in a scope) {
dbl_histogram h1{1, 2, 4, 8};
CAF_MESSAGE("timers call observe() with the measured time");
{
timer t{&h1};
CAF_CHECK_EQUAL(t.histogram_ptr(), &h1);
CAF_CHECK_GREATER(t.started().time_since_epoch().count(), 0);
}
CAF_CHECK_GREATER(h1.sum(), 0.0);
CAF_MESSAGE("timers constructed with a nullptr have no effect");
{
timer t{nullptr};
CAF_CHECK_EQUAL(t.histogram_ptr(), nullptr);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment