Improve formatting of compiler output

This commit is contained in:
hns 2009-09-28 13:07:59 +00:00
parent 3cef3759a8
commit 6af9357ba1
2 changed files with 32 additions and 3 deletions

View file

@ -8,6 +8,8 @@ import org.mozilla.javascript.Scriptable;
import java.util.*;
import helma.util.StringUtils;
public class Profiler implements Debugger {
HashMap frames = new HashMap();
@ -64,10 +66,16 @@ public class Profiler implements Debugger {
return ((ProfilerFrame)o2).runtime - ((ProfilerFrame)o1).runtime;
}
});
int length = Math.min(100, f.length);
int prefixLength = Integer.MAX_VALUE;
for (int i = 0; i < length - 1; i++) {
prefixLength = Math.min(prefixLength,
StringUtils.getCommonPrefix(f[i].name, f[i+1].name).length());
}
StringBuffer buffer = new StringBuffer(" total average calls path\n");
buffer.append("==================================================================\n");
for (int i = 0; i < Math.min(100, f.length); i++) {
buffer.append(f[i].renderLine(0));
for (int i = 0; i < length; i++) {
buffer.append(f[i].renderLine(prefixLength));
}
return buffer.toString();
}
@ -134,7 +142,7 @@ public class Profiler implements Debugger {
Integer.valueOf(invocations),
name.substring(prefixLength)
};
formatter.format("%1$7d ms %2$5d ms %3$6d %4$s%n", args);
formatter.format("%1$7d ms %2$5d ms %3$6d %4$s%n", args);
return formatter.toString();
}
}

View file

@ -84,4 +84,25 @@ public class StringUtils {
return (String[]) list.toArray(new String[list.size()]);
}
/**
* Get the largest common prefix of Strings s1 and s2
* @param s1 a string
* @param s2 another string
* @return the largest prefix shared by both strings
*/
public static String getCommonPrefix(String s1, String s2) {
if (s1.indexOf(s2) == 0) {
return s2;
} else if (s2.indexOf(s1) == 0) {
return s1;
}
int length = Math.min(s1.length(), s2.length());
for (int i = 0; i < length; i++) {
if (s1.charAt(i) != s2.charAt(i)) {
return s1.substring(0, i);
}
}
return s1.substring(0, length);
}
}