Created
October 7, 2020 11:18
-
-
Save nuboat/41ec967aa146e5e208da3eb1884109e5 to your computer and use it in GitHub Desktop.
Java Profiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 Be ID Corporation Co., Ltd. <https://www.beid.io> | |
*/ | |
package handlers; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
/** | |
* <p> | |
* There is no support for runtime annotation in Scala, so far | |
* Java interfaces need to be used. | |
* <p> | |
* @author Peerapat A on Mar 18, 2017 | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface Profiler { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 Be ID Corporation Co., Ltd. <https://www.beid.io> | |
*/ | |
package handlers | |
import java.util.concurrent.TimeUnit | |
import com.google.common.base.Stopwatch | |
import com.typesafe.scalalogging.LazyLogging | |
import org.aopalliance.intercept.{MethodInterceptor, MethodInvocation} | |
/** | |
* @author Peerapat A on Mar 18, 2017 | |
*/ | |
private class ProfilerInterceptor extends MethodInterceptor | |
with LazyLogging { | |
override def invoke(invocation: MethodInvocation): AnyRef = { | |
val watch = Stopwatch.createStarted() | |
val className = invocation.getMethod.getDeclaringClass.getSimpleName | |
val methodName = invocation.getMethod.getName | |
try { | |
invocation.proceed() | |
} finally { | |
val elapsed = watch.stop.elapsed(TimeUnit.MILLISECONDS) | |
logger.info(s"$className.$methodName, execute in $elapsed ms.") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment