Class PerfLog
java.lang.Object
cloud.opencode.base.log.perf.PerfLog
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 Summary
Modifier and TypeMethodDescriptionstatic longGets the global slow operation threshold.static SlowOperationConfigGets the slow operation configuration.static booleanisSlow(long elapsedMs) Checks if an operation is slow based on global threshold.static voidLogs performance information.static voidsetGlobalThreshold(long thresholdMs) Sets the global slow operation threshold.static voidSets the slow operation configuration.static StopWatchStarts a new StopWatch.static voidExecutes a task and logs the elapsed time.static <T> TExecutes a task and logs the elapsed time, returning the result.static voidtimedWithThreshold(String operation, long thresholdMs, Runnable runnable) Executes a task with threshold warning.static <T> TtimedWithThreshold(String operation, long thresholdMs, Callable<T> callable) Executes a task with threshold warning, returning the result.static voidLogs slow operation warning.
-
Method Details
-
start
-
timed
-
timed
-
timedWithThreshold
-
timedWithThreshold
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
Logs performance information. 记录性能信息。- Parameters:
operation- the operation name - 操作名称elapsed- the elapsed time in milliseconds - 毫秒耗时
-
warnSlow
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
Sets the slow operation configuration. 设置慢操作配置。- Parameters:
config- the configuration - 配置
-
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
-