evindj commented on code in PR #589:
URL: https://github.com/apache/iceberg-cpp/pull/589#discussion_r3321146810


##########
src/iceberg/metrics/timer.h:
##########
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <atomic>
+#include <chrono>
+#include <cstdint>
+#include <string_view>
+#include <type_traits>
+
+#include "iceberg/iceberg_export.h"
+
+namespace iceberg {
+
+/// \brief Abstract timer for measuring operation durations.
+///
+/// Use Start() to obtain a Timed RAII
+/// guard that records the elapsed duration when it goes out of scope.
+class ICEBERG_EXPORT Timer {
+ public:
+  /// \brief RAII guard that records elapsed time into the owning Timer on 
destruction.
+  class ICEBERG_EXPORT Timed {
+   public:
+    explicit Timed(Timer& timer);
+    ~Timed();
+
+    Timed(const Timed&) = delete;
+    Timed& operator=(const Timed&) = delete;
+    Timed(Timed&& other) noexcept;
+    Timed& operator=(Timed&& other) noexcept;
+
+    /// \brief Explicitly stop timing and record the duration.
+    ///
+    /// Subsequent calls (including the destructor) are no-ops.
+    void Stop();
+
+   private:
+    Timer* timer_;
+    std::chrono::steady_clock::time_point start_;
+    bool stopped_ = false;
+  };
+
+  virtual ~Timer() = default;
+
+  /// \brief Number of timing recordings made so far.
+  virtual int64_t Count() const = 0;
+
+  /// \brief Total accumulated duration across all recordings.
+  virtual std::chrono::nanoseconds TotalDuration() const = 0;
+
+  /// \brief Record a nanosecond duration directly.
+  ///
+  /// Use the template overload below to record
+  /// any std::chrono duration type with automatic unit conversion.
+  virtual void Record(std::chrono::nanoseconds duration) = 0;
+
+  /// \brief Record a duration of any chrono type, converting to nanoseconds.
+  template <typename Rep, typename Period>
+  void Record(std::chrono::duration<Rep, Period> duration) {
+    Record(std::chrono::duration_cast<std::chrono::nanoseconds>(duration));
+  }
+
+  /// \brief Return the time unit used by this timer (always "nanoseconds").
+  virtual std::string_view Unit() const { return "nanoseconds"; }
+
+  /// \brief Return true if this timer is a no-op.
+  virtual bool IsNoop() const { return false; }
+
+  /// \brief Start timing and return a RAII Timed guard.
+  ///
+  /// The elapsed duration is recorded into this timer when the Timed guard is
+  /// destroyed or Stop() is called.
+  Timed Start();
+
+  /// \brief Execute a callable, record its wall-clock duration, and return 
its result.
+  template <typename Callable>
+  auto Time(Callable&& fn) {
+    auto timed = Start();
+    if constexpr (std::is_void_v<std::invoke_result_t<Callable>>) {
+      std::forward<Callable>(fn)();
+      timed.Stop();
+    } else {
+      auto result = std::forward<Callable>(fn)();
+      timed.Stop();
+      return result;
+    }
+  }

Review Comment:
   For some reason the compiler wants ```void``` to be treated differently. It 
is possible that I am missing something 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to