How to Diagnose Slow OpenAPI Responses with TProfiler Java Agent
This article walks through diagnosing intermittent OpenAPI latency by using Alibaba's open‑source TProfiler Java agent to collect method‑level timing without modifying code, covering setup, command‑line usage, log analysis, and practical performance‑improvement recommendations.
Problem
OpenAPI service latency varies from tens of milliseconds to several seconds. The issue cannot be reproduced in a fast test environment, making traditional per‑method logging impractical.
Solution: TProfiler Java Agent
Alibaba’s open‑source TProfiler Java agent records method execution times without modifying source code. Adding the agent at JVM startup with a properties file incurs minimal performance overhead.
Setup
Clone the repository and build the jar:
git clone https://github.com/crossoverJie/TProfiler
mvn assembly:assemblyThe compiled jar is located at TProfiler/pkg/TProfiler/lib/tprofiler-1.0.1.jar.
Create a profile.properties file that configures the profiling port and other options (example shown in the image).
Start the target application with the agent and the properties file:
-javaagent:/TProfiler/lib/tprofiler-1.0.1.jar -Dprofile.properties=/TProfiler/profile.propertiesDemo Application
A simple HTTP endpoint built with cicada invokes two time‑consuming methods. While the service runs, TProfiler writes raw samples to tprofile.log.
Flushing Samples
Because tmethod.log is initially empty, run the client to flush the latest samples:
java -cp /TProfiler/tprofiler.jar com.taobao.profile.client.TProfilerClient 127.0.0.1 50000 flushmethodThe command contacts the profiling service, parses tprofile.log, and writes aggregated method data to tmethod.log.
Computing Average Latencies
Run the analysis tool to generate per‑method statistics:
java -cp /TProfiler/tprofiler.jar com.taobao.profile.analysis.ProfilerLogAnalysis tprofiler.log tmethod.log topmethod.log topobject.logThe resulting topmethod.log lists, for each method, the request count, average latency, and total time. Example entry: request count 4, average 205 ms, total 818 ms.
Inspecting Individual Invocations
Locate the method ID in tmethod.log (e.g., method selectDB has ID 2) and grep that ID in tprofiler.log to view each execution’s timestamp and duration:
grep 2 tprofiler.logFindings and Recommendations
The slowest methods are all database‑related, indicating high DB load.
Mitigation strategies include hot‑cold data separation, sharding, and converting some write‑to‑DB operations to asynchronous processing.
For deeper visibility, consider adopting a distributed tracing system such as Pinpoint.
Key Advantages of TProfiler
TProfiler offers lightweight, zero‑code‑change profiling that can be extended with custom dashboards, making it suitable for rapid incident investigation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
dbaplus Community
Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
