Skip to content

Instantly share code, notes, and snippets.

@nuboat
Created October 7, 2020 11:18
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 nuboat/41ec967aa146e5e208da3eb1884109e5 to your computer and use it in GitHub Desktop.
Save nuboat/41ec967aa146e5e208da3eb1884109e5 to your computer and use it in GitHub Desktop.
Java Profiler
/*
* 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 {
}
/*
* 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