During software development and testing, we often encounter the need to record and replay network requests for debugging, testing and analysis. To simulate real user requests, we usually use various traffic recording and playback tools to record and replay network requests.
Among them.jvm-sandbox-repeater
It is a powerful traffic recording and playback tool that can help us easily record and playback network requests.
1. Introduction
jvm-sandbox-repeater
is a program based on theJVM-Sandbox
The use of Java to implement the traffic recording and playback tool , or it can be understood that it is a plug-in based on the Java Virtual Machine , can run directly in the JVM , without any modification of the target application .
It automatically intercepts and logs network requests and responses from Java applications at runtime. It utilizes theJVM Sandbox
s bytecode enhancement technology for non-intrusive traffic recording and playback of Java applications. This means that by using thejvm-sandbox-repeater
We can easily record and playback traffic without modifying the source code.
2. Functional characteristics
The main functional features of jvm-sandbox-repeater include the following:
-
Non-intrusive recording: jvm-sandbox-repeater With JVM-Sandbox bytecode enhancement technology, Java applications are recorded at runtime, and network requests are automatically intercepted and logged at runtime without any modification to the source code.
-
Supports multiple protocols: jvm-sandbox-repeater supports multiple network protocols, including HTTP/HTTPS, gRPC, Dubbo, TCP and many other protocols to intercept and record.
-
Flexible Configuration: jvm-sandbox-repeater provides a rich set of configuration options to customize the recording and playback behavior as needed.
-
high performance: jvm-sandbox-repeater is based on the JVM-Sandbox technology, which has low performance loss and less impact on the application.
-
High Precision Recording: The tool is able to accurately record detailed information about requests and responses, including request headers, request bodies, response status codes, response bodies, etc., to ensure the accuracy of playback.
-
Flexible playback strategySupport multiple playback strategies, such as chronological playback, concurrent playback, etc., to meet different testing needs.
-
visualization interfaceProvides an easy-to-use visualization interface for recording, management and playback of traffic, lowering the threshold of use.
-
high scalability: Based on the JVM Sandbox platform , easy to integrate and extend with other tools .
3、Application Scenario
Use at workjvm-sandbox-repeater
tools, mainly by borrowing three of these basic types of competencies:
-
Traffic Recording
: jvm-sandbox-repeater captures network requests from target applications in real time and records them. Through the recording function to obtain the actual network request data of the target application for subsequent analysis and debugging. -
traffic replay
: jvm-sandbox-repeater supports playback of recorded network requests. With the playback feature, network requests from the target application are simulated to verify their correctness and stability. -
Requests for modifications
: jvm-sandbox-repeater allows developers to modify recorded network requests. The modification feature allows adjusting the parameters and header information of the network request to simulate different scenarios and test cases.
With the help of these three basic types of capabilities, the following scenarios are commonly applied in test development work:
- interface testing: By recording real interface traffic in the production environment and playing it back in the test environment, you can simulate a variety of complex scenarios and verify the stability and performance of the interface.
- performance testing: Performance testing using recorded traffic data provides a truer picture of system performance and helps identify and resolve performance bottlenecks.
- Compatibility Test: System compatibility can be verified by playing back recorded traffic in different versions of Java applications or in different environments.
4. Principle 1: How does the JVM-Sandbox technology work?
jvm-sandbox-repeater
is a program based on theJVM-Sandbox
The implementation of the traffic playback tool, but it is expected that many readers, what is JVM-Sandbox, and how it works is not quite clear, here to expand.
JVM-Sandbox simply put , it is a Java application sandbox , designed to provide an isolated , secure operating environment for Java applications . Sandbox (Sandbox) technology's main purpose is to protect system security and stability , while providing a better application development and testing environment . By isolating the environment in which a program runs, Sandbox technology prevents the program from accessing resources and data that should not be accessed, thus reducing security risks.
The working principle of JVM-Sandbox can be summarized in the following key steps:
1. Class loading: In the JVM sandbox, all classes are loaded by the Java Virtual Machine's class loader. The class loader is responsible for loading Java classes from the file system, network, or other sources and converting them into executable code.
2、Byte code verification: During the loading process, the JVM validates the bytecode to ensure that it conforms to the Java language specification. The purpose of this step is to detect potential security vulnerabilities such as type conversion errors, out-of-bounds access to arrays, and so on.
3. Security inspections: After bytecode verification, the JVM performs further security checks to restrict the behavior of the application to prevent harm to the system. This includes restricting the application's access rights and resource usage, such as file system access, network access, system calls, and so on.
4. Sandbox implementation: Once the bytecode verification and security checks have been passed, the JVM places the application into a controlled sandbox environment for execution. This sandbox environment restricts the application's access to system resources and ensures that it can only be executed within a limited scope.
In addition.JVM-Sandbox
Virtualization techniques are also used to create isolated environments and methods that are mocked (simulated) are added to the isolated environments through bytecode enhancements. When these methods are called, the JVM-Sandbox looks for them through a reflection mechanism and forwards them to the virtual machine in the sandbox for execution using a proxy object. When execution is complete, the results are returned to the main program.
5、Principle 2: Bytecode Enhancement Technology and what is it?
bytecode enhancement technologyis a technique for modifying Java bytecode to enhance its functionality. It is mainly realized by reading and modifying the bytecode file after the Java bytecode has been generated to enhance elements such as classes, methods, fields, and so on. This technique is equivalent to modifying the binary file of an application with the main purpose of reducing redundant code, improving performance, etc.
The main steps of bytecode enhancement include:
- Modify byte code: Get the original byte code in memory and then modify its byte[] array with some tools (e.g. ASM, Javassist) to get a new byte array.
- Make the modified bytecode effective: This can be achieved in two ways: one is to customize the ClassLoader to load the modified bytecode; the other is to intercept and return the modified bytecode when the JVM loads the user's Class, or to replace the original bytecode with a method at runtime.
Bytecode enhancement technology has a wide range of applications, for example, it can be used in APM (Application Performance Management) tools to monitor and manage application software performance and availability to ensure the normal operation of software applications; it can also be used in Java problem location tools such as BTrace
6. Installation, use
1. Downloading or introducing dependencies
First, you need to download the latest version of the plugin jar file from the official jvm-sandbox-repeater website or GitHub repository.
Or just add the jvm-sandbox-repeater dependency to your project's files:
<dependency>
<groupId>-sandbox-repeater</groupId>
<artifactId>jvm-sandbox-repeater</artifactId>
<version>latest version</version>
</dependency>
2. Configuring the Sandbox
Add the jvm-sandbox-repeater plugin to the JVM by adding the downloaded jar file of the jvm-sandbox-repeater plugin to the class path of the target application or adding it to the JVM via the Javaagent parameter.
Enable JVM-Sandbox and jvm-sandbox-repeater by adding the following configuration to the project's startup parameters:
java -javaagent:path/to/= -jar
One of them is the configuration file of jvm-sandbox-repeater, which configures the parameters of jvm-sandbox-repeater, such as the path to save the recording file, the playback speed, etc. You can modify them according to the actual situation.
3. Preparation of the configuration file
Create a file named to configure jvm-sandbox-repeater.The following is a simple example:
repeater:
enabled: true
recordDir: /path/to/record/dir
replayDir: /path/to/replay/dir
filter:
classes:
- .*
methods:
-
In this example, we enable jvm-sandbox-repeater and specify the directory for recording and playback. We also define a filter to record and playback only the getUserInfo method of the UserService class under the package.
4. Recording and playback
While the target application is running, jvm-sandbox-repeater captures network requests in real time and records them to a specified recording folder. When you need to play back the requests, you can use the command line tools or APIs provided by jvm-sandbox-repeater to do so.
# Record request
java -jar record -c
# Playback request
java -jar replay -c
Alternatively, you can use the API provided by jvm-sandbox-repeater in your code for recording and playback. Here is a simple example:
import ;
import ;
import ;
public class MyApp {
public static void main(String[] args) {
// initialization Repeater
Repeater repeater = new Repeater();
();
// Start recording request
Recorder recorder = ();
("/api/user", "GET");
// Stop Recording Request
();
// Starting a playback request
Replayer replayer = ();
("/api/user", "GET");
// Stop Playback Request
();
}
}
With these steps, you can use jvm-sandbox-repeater to record and playback traffic for Java applications.
5. Viewing and analyzing results
During or after playback, you can view the status, results, and performance data of the playback through the visual interface provided by jvm-sandbox-repeater to analyze the results.
7. Summary
Overall.jvm-sandbox-repeater
is a powerful, easy-to-use traffic recording and playback tool that helps developers or testers easily record and playback network requests. It can be used to record and playback network requests byjvm-sandbox-repeater
If you are looking for a good traffic recording and playback tool, you can use it to improve the efficiency and quality of software development and testing by debugging, testing and analyzing more efficiently. If you are looking for an excellent traffic recording and playback tool, try thejvm-sandbox-repeater
I'm sure it will bring you unexpected surprises!
jvm-sandbox-repeater
The source code of the project:
git clone /alibaba/