Building a Code Analysis Platform to Boost Microservice Backend Efficiency

This article details how a microservice‑based backend team designed and implemented a code‑analysis platform that creates a knowledge base, enables precise test selection and application slimming, and ultimately accelerates demand iteration while reducing dead code and unnecessary test runs.

Architect
Architect
Architect
Building a Code Analysis Platform to Boost Microservice Backend Efficiency

Introduction

In a microservice environment, frequent demand iterations cause a rapid increase in the number of services, source lines of code and automated test cases, making it difficult for product managers, developers and testers to understand historical logic and maintain iteration efficiency.

At Ctrip Travel BG each backend developer maintains >4 services and ~200,000 LOC, with >2,000 releases per month; core services account for >80% of releases.

A code‑analysis platform was built to identify dead code (≈33% of production code) and to filter out ~68% of regression tests that are unrelated to the current change.

Code Analysis Planning

The platform targets backend Java applications and provides developers with tools to safely remove dead code and to execute only the tests that cover the changed methods.

Analysis Principles

Static Analysis

Parse source code to extract every method declaration and its call relationships. The recommended tool is java-callgraph2, which correctly handles Java constructs such as threads, lambdas and streams.

Semi‑Dynamic Analysis

Combine byte‑code instrumentation with test‑case replay to capture the method chain traversed during test execution and map it onto the static call graph. The open‑source arex-agent-java agent provides a non‑intrusive way to instrument the JVM during test runs.

Dynamic Analysis

Collect runtime method execution counts from the JVM. Two approaches exist:

Intrusive instrumentation (similar to semi‑dynamic) – accurate but adds overhead and is not recommended for production.

JVM method counters via JIT – non‑intrusive, but requires a brief STW (Stop‑The‑World) pause when attaching to the process.

The latter is preferred for production traffic sampling.

Platformization Steps

1. Build the Knowledge Base

Application entry acquisition : Identify external APIs, scheduled jobs and message consumers via annotation scanning or service‑registry queries.

Source code retrieval : Use container images (post‑deployment, pre‑exposure) as the static‑analysis input.

Static analysis & storage : Generate method call graphs and store them in a graph database (e.g., Nebula‑Graph) while keeping basic statistics in a relational database.

2. Enrich with Test Cases

Replay automated test cases, collect the internal methods and external APIs exercised by each test, and link this information to the knowledge base. The AREX agent enables non‑intrusive instrumentation during test execution.

3. Add Production Traffic Information

Gather method‑level traffic data from production instances using JVM counters. Follow three principles:

Minimize impact on services (e.g., pull instances out of the cluster only when safe).

Collect meaningful data (avoid short‑lived or idle instances).

Perform periodic sampling to keep the knowledge base up‑to‑date.

Application Scenarios

Precise Testing

Compare code changes against the knowledge base to automatically recommend only the test cases that touch the modified methods. In practice this achieved 99.2% of the coverage of a full‑run baseline while reducing unrelated test executions by 68%.

Application Slimming

Analyze method reachability, usage frequency and external dependencies to locate dead code. A pilot covering 10% of services (>100 services) safely removed millions of lines of code, reducing Docker image build time from tens of minutes to a few minutes for large services.

Summary

The platform demonstrates how systematic static, semi‑dynamic and dynamic analyses can be combined into a comprehensive knowledge base that drives precise testing and safe application slimming, thereby markedly improving demand‑iteration efficiency in a microservice architecture.

References

java-callgraph2: https://github.com/Adrninistrator/java-callgraph2

arex-agent-java: https://github.com/arextest/arex-agent-java

OpenJDK Serviceability: https://openjdk.org/groups/hotspot/docs/Serviceability.html

JavaParser: https://javaparser.org/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendMicroservicescode analysisstatic analysisDynamic analysistest optimizationapplication slimming
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

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.