Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -26,8 +26,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/taobao/profile/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/taobao/profile/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ public static void end4Mysql(){
return ;
}

long useTime = endTime - (Long) frameData[1];
if(!isNeedRecord(useTime)){
return;
}

RecordSlowQuery record = new RecordSlowQuery();
Map<String, String> map = new HashMap<String, String>();

Expand All @@ -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();
Expand All @@ -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) {
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/taobao/profile/test/ProfilerTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}