Skip to content

Instantly share code, notes, and snippets.

@ern
Created October 5, 2017 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ern/34e44b9933a3ff6f31088a91a578ae9f to your computer and use it in GitHub Desktop.
Save ern/34e44b9933a3ff6f31088a91a578ae9f to your computer and use it in GitHub Desktop.
Simulate a slow resource
private void traceSimulateDelay(long delay) {
// log the caller
String thisClassName = this.getClass().getName();
StackTraceElement[] ste = Thread.currentThread().getStackTrace();
StringBuilder callee = new StringBuilder("PERFORMANCE[-{}] called by ");
for (int i = 2; i <= ste.length; i++) {
// 0 is Thread.getStackTrace()
// 1 is this method
String className = ste[i].getClassName();
String shortClassName = className.substring(className.lastIndexOf(".") + 1);
callee.append(shortClassName).append(".").append(ste[i].getMethodName()).append(":").append(ste[i].getLineNumber());
if (thisClassName.equals(className)) {
callee.append(">");
} else {
break;
}
}
M_log.trace(callee.toString(), delay);
// simulate a long query
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment