add profile support

This commit is contained in:
wenshao
2012-09-21 21:50:48 +08:00
parent 78751d169d
commit 906f2a4840
2 changed files with 101 additions and 0 deletions
@@ -0,0 +1,42 @@
package com.alibaba.druid.support.profile;
public class ProfileEntry {
private final Key key;
private final String type;
public ProfileEntry(Key key, String type){
super();
this.key = key;
this.type = type;
}
public Key getKey() {
return key;
}
public String getType() {
return type;
}
public static class Key {
private String parent;
private String name;
public Key(String parentName, String name){
super();
this.parent = parentName;
this.name = name;
}
public String getParent() {
return parent;
}
public String getName() {
return name;
}
}
}
@@ -0,0 +1,59 @@
package com.alibaba.druid.support.profile;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.druid.support.profile.ProfileEntry.Key;
public class Profiler {
public static ThreadLocal<Boolean> enableLocal = new ThreadLocal<Boolean>();
private static ThreadLocal<Map<ProfileEntry.Key, ProfileEntry>> entriesLocal = new ThreadLocal<Map<Key, ProfileEntry>>();
private final static ThreadLocal<ProfileEntry> currentLocal = new ThreadLocal<ProfileEntry>();
public static void setThreadLocalEnable() {
enableLocal.set(true);
}
public static void setThreadLocalDisable() {
enableLocal.remove();
}
public static boolean isEnable() {
return Boolean.TRUE == enableLocal.get();
}
public static void enter(String name, String type, long startNano) {
ProfileEntry parent = currentLocal.get();
String parentName = null;
if (parent != null) {
parentName = parent.getKey().getName();
}
ProfileEntry.Key key = new ProfileEntry.Key(parentName, name);
Map<ProfileEntry.Key, ProfileEntry> entries = entriesLocal.get();
if (entries == null) {
entries = new HashMap<ProfileEntry.Key, ProfileEntry>();
entriesLocal.set(entries);
}
ProfileEntry entry = entries.get(key);
if (entry == null) {
entry = new ProfileEntry(key, type);
entries.put(key, entry);
}
currentLocal.set(entry);
}
public static ProfileEntry current() {
return currentLocal.get();
}
public static void release(long nanoSpan) {
}
}