Class PerfLog

java.lang.Object
cloud.opencode.base.log.perf.PerfLog

public final class PerfLog extends Object
PerfLog - Performance Logging Utility PerfLog - 性能日志工具

PerfLog provides convenient methods for measuring and logging operation performance. It integrates with StopWatch and SlowOperationConfig.

PerfLog 提供测量和记录操作性能的便捷方法。它与 StopWatch 和 SlowOperationConfig 集成。

Example | 示例:

// Start a StopWatch
StopWatch watch = PerfLog.start("queryUsers");
List<User> users = userDao.findAll();
watch.stopAndLog();

// Timed execution
PerfLog.timed("processOrder", () -> orderService.process(order));

// With threshold warning
PerfLog.timedWithThreshold("slowOp", 1000, () -> heavyOperation());

// Configure global threshold
PerfLog.setGlobalThreshold(500);

Features | 主要功能:

  • StopWatch-based performance timing - 基于 StopWatch 的性能计时
  • Timed execution with automatic logging - 自动记录的计时执行
  • Configurable slow operation threshold and warning - 可配置的慢操作阈值和警告
  • Integration with PERFORMANCE marker - 与 PERFORMANCE 标记集成

Security | 安全性:

  • Thread-safe: Yes (AtomicReference for config) - 线程安全: 是(AtomicReference 用于配置)
  • Null-safe: No (operation name must not be null) - 空值安全: 否(操作名称不能为 null)
Since:
JDK 25, opencode-base-log V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • start

      public static StopWatch start(String operation)
      Starts a new StopWatch. 启动一个新的 StopWatch。
      Parameters:
      operation - the operation name - 操作名称
      Returns:
      the started StopWatch - 已启动的 StopWatch
    • timed

      public static void timed(String operation, Runnable runnable)
      Executes a task and logs the elapsed time. 执行任务并记录耗时。
      Parameters:
      operation - the operation name - 操作名称
      runnable - the task - 任务
    • timed

      public static <T> T timed(String operation, Callable<T> callable)
      Executes a task and logs the elapsed time, returning the result. 执行任务并记录耗时,返回结果。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      operation - the operation name - 操作名称
      callable - the task - 任务
      Returns:
      the result - 结果
    • timedWithThreshold

      public static void timedWithThreshold(String operation, long thresholdMs, Runnable runnable)
      Executes a task with threshold warning. 执行带阈值警告的任务。
      Parameters:
      operation - the operation name - 操作名称
      thresholdMs - the threshold in milliseconds - 毫秒阈值
      runnable - the task - 任务
    • timedWithThreshold

      public static <T> T timedWithThreshold(String operation, long thresholdMs, Callable<T> callable)
      Executes a task with threshold warning, returning the result. 执行带阈值警告的任务,返回结果。
      Type Parameters:
      T - the result type - 结果类型
      Parameters:
      operation - the operation name - 操作名称
      thresholdMs - the threshold in milliseconds - 毫秒阈值
      callable - the task - 任务
      Returns:
      the result - 结果
    • log

      public static void log(String operation, long elapsed)
      Logs performance information. 记录性能信息。
      Parameters:
      operation - the operation name - 操作名称
      elapsed - the elapsed time in milliseconds - 毫秒耗时
    • warnSlow

      public static void warnSlow(String operation, long elapsed, long thresholdMs)
      Logs slow operation warning. 记录慢操作警告。
      Parameters:
      operation - the operation name - 操作名称
      elapsed - the elapsed time in milliseconds - 毫秒耗时
      thresholdMs - the threshold in milliseconds - 毫秒阈值
    • setGlobalThreshold

      public static void setGlobalThreshold(long thresholdMs)
      Sets the global slow operation threshold. 设置全局慢操作阈值。
      Parameters:
      thresholdMs - the threshold in milliseconds - 毫秒阈值
    • getGlobalThreshold

      public static long getGlobalThreshold()
      Gets the global slow operation threshold. 获取全局慢操作阈值。
      Returns:
      the threshold in milliseconds - 毫秒阈值
    • setSlowOperationConfig

      public static void setSlowOperationConfig(SlowOperationConfig config)
      Sets the slow operation configuration. 设置慢操作配置。
      Parameters:
      config - the configuration - 配置
    • getSlowOperationConfig

      public static SlowOperationConfig getSlowOperationConfig()
      Gets the slow operation configuration. 获取慢操作配置。
      Returns:
      the configuration - 配置
    • isSlow

      public static boolean isSlow(long elapsedMs)
      Checks if an operation is slow based on global threshold. 根据全局阈值检查操作是否慢。
      Parameters:
      elapsedMs - the elapsed time in milliseconds - 毫秒耗时
      Returns:
      true if slow - 如果慢返回 true