Backend Development 5 min read

Measuring Java Code Performance with Intrusive Methods and Dynamic Proxies

This article explains how to perform code‑level performance measurement in Java services, compares the traditional intrusive approach with a cleaner solution using JDK dynamic proxies, and demonstrates how to decouple timing logic from business code.

Art of Distributed System Architecture Design
Art of Distributed System Architecture Design
Art of Distributed System Architecture Design
Measuring Java Code Performance with Intrusive Methods and Dynamic Proxies

When a service goes live, architects and engineers need to monitor its runtime status, such as normal operation and performance compliance. While system‑level metrics like CPU, disk I/O, and response time are common, this article focuses on code‑level performance measurement.

Developers encountering performance problems typically instrument each target method by starting a monitor before the call, calculating the elapsed time after the call, and logging the result. This is known as an intrusive performance measurement method, which the article discusses first.

1. Intrusive Performance Measurement Method

In typical business development, a service interface (e.g., UserService ) is defined and concrete classes implement it. The article shows a UserService interface and its implementation via images.

The corresponding implementation class is illustrated below.

Figure 1

The PerformanceMonitor utility class definition is shown in the next figure.

Figure 2

To measure the execution time of doSomething , developers must insert PerformanceMonitor calls inside the method. When many methods need measurement, this leads to repetitive and cumbersome code that mixes monitoring logic with business logic, making maintenance difficult.

2. Java Dynamic Proxy Performance Measurement Method

Dynamic proxies, introduced after JDK 1.3, allow creation of proxy instances for interfaces at runtime. The key classes are java.lang.reflect.Proxy and InvocationHandler . By implementing InvocationHandler , one can embed performance measurement without altering the target methods.

First, the intrusive monitoring code is removed from doSomething , as shown in the following image.

Figure 3

After removal, the method contains only business logic, making it clean and readable.

The performance measurement is then implemented in a custom PerformanceHandler class that implements InvocationHandler . Its key part (highlighted in the third step of the figure) invokes the original business method and records the execution time.

The proxy instance is created via Proxy.newProxyInstance , supplying the class loader, the interface array, and the PerformanceHandler . Testing shows that this dynamic‑proxy approach yields the same measurement results as the intrusive method while keeping monitoring code separate from business logic.

JavaInstrumentationbackend-developmentDynamic ProxyPerformance Measurement
Art of Distributed System Architecture Design
Written by

Art of Distributed System Architecture Design

Introductions to large-scale distributed system architectures; insights and knowledge sharing on large-scale internet system architecture; front-end web architecture overviews; practical tips and experiences with PHP, JavaScript, Erlang, C/C++ and other languages in large-scale internet system development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.