diff --git a/pom.xml b/pom.xml index 925e666..995462e 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ junit junit - 4.11 + 4.13.2 test @@ -26,8 +26,8 @@ maven-compiler-plugin 3.0 - 1.6 - 1.6 + 1.8 + 1.8 UTF-8 diff --git a/src/main/java/com/taobao/profile/Manager.java b/src/main/java/com/taobao/profile/Manager.java index 4644152..720a39f 100644 --- a/src/main/java/com/taobao/profile/Manager.java +++ b/src/main/java/com/taobao/profile/Manager.java @@ -150,6 +150,10 @@ public static boolean isNeedNanoTime() { return NEED_NANO_TIME; } + public static void setNeedNanoTime(boolean value) { + NEED_NANO_TIME = value; + } + /** * @return the ignoreGetSetMethod */ diff --git a/src/main/java/com/taobao/profile/Profiler.java b/src/main/java/com/taobao/profile/Profiler.java index 0ad4780..426bdcb 100644 --- a/src/main/java/com/taobao/profile/Profiler.java +++ b/src/main/java/com/taobao/profile/Profiler.java @@ -320,6 +320,11 @@ public static void end4Mysql(){ return ; } + long useTime = endTime - (Long) frameData[1]; + if(!isNeedRecord(useTime)){ + return; + } + RecordSlowQuery record = new RecordSlowQuery(); Map map = new HashMap(); @@ -328,7 +333,7 @@ public static void end4Mysql(){ map.put("db", (String) frameData[4]); map.put("sql", (String) frameData[5]); record.setRequestDesc(map); - record.setUseTime(endTime - (Long) frameData[1]); + record.setUseTime(useTime); record.setType("MYSQL"); StringBuilder sb = new StringBuilder(); @@ -337,9 +342,6 @@ public static void end4Mysql(){ sb.append(frameData[3].toString()); sb.append((String) frameData[4]); - if(!isNeedRecord(record.getUseTime())){ - return; - } map.put("nanoTime", Manager.isNeedNanoTime() + ""); synchronized (thrData) { diff --git a/src/test/java/com/taobao/profile/test/ProfilerTest.java b/src/test/java/com/taobao/profile/test/ProfilerTest.java new file mode 100644 index 0000000..52e5780 --- /dev/null +++ b/src/test/java/com/taobao/profile/test/ProfilerTest.java @@ -0,0 +1,41 @@ +package com.taobao.profile.test; + +import com.taobao.profile.Manager; +import com.taobao.profile.Profiler; +import com.taobao.profile.dependence_query.SlowQueryData; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ProfilerTest { + + @Before + public void setUp() { + Manager.instance().setProfileFlag(true); + Manager.setRecordTime(10); + Manager.setNeedNanoTime(false); + Profiler.clearData(); + } + + @Test + public void testEnd4Mysql_shouldNotRecordWhenTimeIsBelowThreshold() { + long threadId = Thread.currentThread().getId(); + Profiler.start4Mysql("host", 1234, "db", "sql"); + Profiler.end4Mysql(); + int threadIndex = (int) threadId; + SlowQueryData thrData = Profiler.slowQueryProfile[threadIndex]; + assertEquals(0, thrData.profileData.size()); + } + + @Test + public void testEnd4Mysql_shouldRecordWhenTimeIsAboveThreshold() throws InterruptedException { + long threadId = Thread.currentThread().getId(); + Profiler.start4Mysql("host", 1234, "db", "sql"); + Thread.sleep(11); + Profiler.end4Mysql(); + int threadIndex = (int) threadId; + SlowQueryData thrData = Profiler.slowQueryProfile[threadIndex]; + assertEquals(1, thrData.profileData.size()); + } +}