From 8280c9eee52dd9fa3501305dfd4d1cab1564a73b Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期六, 20 十二月 2025 15:01:20 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/common/config/MethodTimingAspect.java |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/common/config/MethodTimingAspect.java b/src/main/java/com/zy/common/config/MethodTimingAspect.java
new file mode 100644
index 0000000..a0cbbf4
--- /dev/null
+++ b/src/main/java/com/zy/common/config/MethodTimingAspect.java
@@ -0,0 +1,46 @@
+package com.zy.common.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Aspect
+@Slf4j
+@Order(Ordered.LOWEST_PRECEDENCE)
+@ConditionalOnProperty(prefix = "perf.methodTiming", name = "enabled", havingValue = "true")
+public class MethodTimingAspect {
+
+    @Value("${perf.methodTiming.thresholdMs:50}")
+    private long thresholdMs;
+
+    @Value("${perf.methodTiming.sampleRate:1.0}")
+    private double sampleRate;
+
+    @Around("execution(public * com.zy.core..*(..))")
+    public Object timeCorePublicMethods(ProceedingJoinPoint joinPoint) throws Throwable {
+        if (sampleRate < 1.0d && ThreadLocalRandom.current().nextDouble() > sampleRate) {
+            return joinPoint.proceed();
+        }
+
+        long startNs = System.nanoTime();
+        try {
+            return joinPoint.proceed();
+        } finally {
+            long costMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
+            if (costMs >= thresholdMs) {
+                log.warn("SLOW {} took {} ms (thread={})", joinPoint.getSignature().toShortString(), costMs, Thread.currentThread().getName());
+            }
+        }
+    }
+}
+

--
Gitblit v1.9.1