Measure method execution time using java agent and byte-buddy

295 views
Skip to first unread message

shehanper...@gmail.com

unread,
Dec 14, 2017, 1:37:02 AM12/14/17
to Byte Buddy
Hi,
I want to measure execution time of a program without changing the my program. I think java agent and byte buddy can be use for this problem. But i don't have a idea about how to do it.

can you please help

Rafael Winterhalter

unread,
Dec 14, 2017, 2:35:42 PM12/14/17
to shehanper...@gmail.com, Byte Buddy
Hei,
yes, that is perfectly possible. You can have a look at this SO answer to begin with: https://stackoverflow.com/questions/38849768/profiling-using-byte-buddy-or-some-library
Best regards, Rafael


--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

she...@wso2.com

unread,
Dec 16, 2017, 9:24:17 PM12/16/17
to Byte Buddy
On Friday, December 15, 2017 at 1:05:42 AM UTC+5:30, Rafael Winterhalter wrote:
> Hei,
> yes, that is perfectly possible. You can have a look at this SO answer to begin with: https://stackoverflow.com/questions/38849768/profiling-using-byte-buddy-or-some-library
>
> Best regards, Rafael
>
>
>
> 2017-12-14 7:37 GMT+01:00 <shehanper...@gmail.com>:
> Hi,
>
> I want to measure execution time of a program without changing the my program. I think java agent and byte buddy can be use for this problem. But i don't have a idea about how to do it.
>
>
>
> can you please help
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.

>
> For more options, visit https://groups.google.com/d/optout.

I followed this and some other your answers in SO . I used following code for java agent .

public static void premain(String arguments, Instrumentation instrumentation) {
System.out.println("Premain");
new AgentBuilder.Default()
.type(ElementMatchers.nameEndsWith("Timed"))
.transform((builder, type, classLoader, module) ->
builder.method(ElementMatchers.any())
.intercept(MethodDelegation.to(MonitorInterceptor.class))
).installOn(instrumentation);

}

This is my MonitorInspector

public class MonitorInterceptor {

@RuntimeType
public static Object intercept(@Origin Method method,
@SuperCall Callable<?> callable) throws Exception {
long start = System.currentTimeMillis();
try {
return callable.call();
} finally {
System.out.println(method + " took " + (System.currentTimeMillis() - start));
}
}
}

But this not gives the printout of time it only provide following output .

Premain
Main Class

Can you give me an idea what is the problem ,that no giving the output from MonitorInspector ?


shehanper...@gmail.com

unread,
Dec 17, 2017, 12:36:04 AM12/17/17
to Byte Buddy

Rafael Winterhalter

unread,
Dec 17, 2017, 5:24:56 PM12/17/17
to shehanper...@gmail.com, Byte Buddy
I added an extended answer to your SO question.

2017-12-17 6:36 GMT+01:00 <shehanper...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages