Automated Testing Method Based on Traffic Replication and Playback Strategy and Code Interception Technology
By rebuilding the context of the production environment in the test environment, using traffic replication and code interception technologies, the problems of high risk of instrumentation and insufficient path coverage in the existing technology are solved, and safe, comprehensive and independent automated testing is achieved.
Patent Information
- Application Number
- CN202510611517.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-13
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2045-05-13
AI Technical Summary
The existing traffic replication and playback strategies have high requirements and high risks in production environments, and cannot cover end-to-end paths, affecting system stability and comprehensive testing.
By establishing a snapshot of the production environment, using traffic replication and code intercepting technologies to rebuild the context in the test environment, intercept write request traffic, ensure the consistency between the test environment and the production environment, and avoid production environment instrumentation.
It realizes end-to-end testing safely and reliably in the test environment, covering all paths, adapting to system changes, and no business code changes and operation and maintenance operations are required.
Smart Images

Figure CN120123253B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of write interface automation testing technology, and specifically relates to an automated testing method based on a traffic replication and playback strategy and code interception technology. Background Art
[0002] The traffic replication and playback strategy is a popular automated testing method in the prior art. It does not require testers to write test cases or test code related to business logic.
[0003] The currently common traffic replication and playback strategy is to instrument in the production environment and record traffic and process data, and then integrate the traffic and data into an automated testing framework. When playing back the traffic, the data obtained by instrumentation is used for corresponding restoration at the program locations where data needs to be obtained or processed.
[0004] This strategy still faces two problems when in use:
[0005] One is that it is necessary to instrument in the production environment, which has very high requirements for system stability and hardware resources, and the maintenance cost is very high and the risk is great. If there is a slight mistake, it will have a serious impact on production.
[0006] The second is that the traffic playback cannot cover the end-to-end path. Its instrumentation method will intercept calls to middleware such as associated services and databases, and the testing is not comprehensive enough.
[0007] Therefore, there is an urgent need for a new solution to solve the defects and deficiencies existing in the above prior art. Summary of the Invention
[0008] In order to solve the defects and deficiencies existing in the prior art, the present invention provides an automated testing method based on a traffic replication and playback strategy and code interception technology.
[0009] The specific solution provided by the present invention is as follows:
[0010] An automated testing method based on a traffic replication and playback strategy and code interception technology, characterized by comprising the following steps:
[0011] S1: Establish a snapshot of the production environment;
[0012] S2: Traffic replication;
[0013] S3: Simulate and reconstruct the playback environment context; including the following steps:
[0014] S3.1: Select corresponding interception code to intercept status data, and store, transfer and call context information;
[0015] S3.2: Reconstruct the user session context, ensure that the primary key written to the database is consistent with the production result, and generate the sequence number field in the data;
[0016] S3.3: Obtain the consistent data during context reconstruction through business logic and request time;
[0017] S3.4: Make nested calls to the program code;
[0018] S4: Compare and verify the write operations to the database, print the inconsistent parts to the report for analysis;
[0019] S5: Optimize the traffic replication and playback strategy.
[0020] As a further preferred embodiment of the present invention, in the step S1,
[0021] Set the starting point of the production environment snapshot at the low peak period of the traffic;
[0022] The production environment snapshot at least includes a database snapshot, a program package image, and a system configuration; among them, the database snapshot adopts a dump technology and a binary log synchronization technology. If data changes are found during the database dump, the binary log record is used to apply the changes to the dump result; the program package image and the system configuration are obtained through the operation and maintenance system;
[0023] And when loading the data snapshot to the test environment, set the test environment as an environment with access restrictions, and perform desensitization operations on user privacy data.
[0024] As a further preferred embodiment of the present invention, in the step S2, keep the start time of traffic replication consistent with the start time of establishing the production environment snapshot.
[0025] As a further preferred embodiment of the present invention, in the step S3.1, when selecting the corresponding interception code to intercept the status data:
[0026] Inject the interception code into the running process through dynamic injection;
[0027] And code injection functions are respectively set at three points of preprocessing, end of processing, and exception on the interception framework;
[0028] At the preprocessing point, modify the parameter input of the program method, establish a thread context, or intercept the continued execution of the code and directly return the required result; at the end of processing point, modify the return result; if a thread context is established at the preprocessing point, clean up the corresponding thread context at the end of processing and exception points.
[0029] As a further preferred embodiment of the present invention, in step S3.1, when storing, transmitting, and invoking context information:
[0030] The context information is stored in the space of the execution thread and is made visible only within the thread. And a method is set to obtain the context information of the thread through code execution, and content can be added to or queried from it.
[0031] When creating a child thread, the request information is passed to the child thread, and the child thread creates its own context object according to the passed request information.
[0032] At the same time, when making a service call, the corresponding request information is written into the header of the call message.
[0033] In addition, when synchronizing data through the middleware, the write method is intercepted, the corresponding request message is written into the extended information, and in the consumption code, the original request information in the extended information is read by intercepting the consumption method and the context is reconstructed.
[0034] As a further preferred embodiment of the present invention, in step S3.2, when reconstructing the user session context and keeping the primary key written into the database consistent with the production result, and at the same time generating the serial number field in the data:
[0035] When reconstructing the user session context, in the user login request, the user session is successfully created by intercepting the login verification, and a successful result is simulated and returned to the caller.
[0036] When keeping the primary key written into the database consistent with the production result, for the auto-increment primary key, by intercepting and injecting the primary key field and primary key value consistent with the production during writing, the write result is kept consistent with the production result.
[0037] When generating the serial number field in the data, the serial number field in the data is generated based on the context information, and the context information at least includes the system time and the current increment serial number.
[0038] As a further preferred embodiment of the present invention, in step S3.3, when obtaining the consistency data during context reconstruction through business logic and request time:
[0039] The business logic is used to help determine the uniqueness of the business data. For the case where the unique field has changed, combined with the request time, the specific data is further determined.
[0040] Find the unique data write statement in the binary log based on the table name, business data uniqueness field, and time point, obtain the primary key or sequence number field value from it, and return it to the interception point program for database writing, and inject it into the data write statement to keep the writing result in the test environment consistent with the production result.
[0041] As a further preferred embodiment of the present invention, in step S3.4, when nested calls are made to the program code, the intercepted processing context of the called program code needs to be isolated, and the context of nested calls is managed in a stack manner.
[0042] As a further preferred embodiment of the present invention, in step S4, when comparing and verifying the write operation of the database, it is carried out in the order of time sequence, table, and primary key in turn.
[0043] As a further preferred embodiment of the present invention, in step S5, when optimizing the traffic replication playback strategy, the traffic playback result is used as the baseline result, and the running result of the test code is compared with this baseline result:
[0044] When the test result is consistent with the baseline result, it is determined that no optimization is required, and the interception optimization process ends;
[0045] When the test result is inconsistent with the baseline result, first judge the types of inconsistent data:
[0046] When the inconsistent data is non-status data, it is determined that the code needs to be analyzed;
[0047] When the inconsistent data is status data, it is determined that interception optimization is required;
[0048] The interception optimization method at least includes adding new interception points and adding new interception logics.
[0049] Compared with the prior art, the technical effects that the present invention can achieve include:
[0050] 1) The present invention provides an automated testing method based on the traffic replication playback strategy and code interception technology, which can complete the playback of production environment traffic in the test environment, establish a snapshot of the production environment, and use the snapshot to restore the test environment in the test environment; at the same time, no instrumentation is required in the production environment, only the write request traffic of the service interface needs to be intercepted and recorded at the network layer, and then the code interception technology is used to ensure the consistency of the program running context and production, ensure that the running process is consistent with the production, and produce consistent results.
[0051] 2) The present invention provides an automated testing method based on a traffic replication and playback strategy and code interception technology, which will not perform any processing or intervention on the production environment during implementation and can be safely carried out in the test environment.
[0052] 3) The present invention provides an automated testing method based on a traffic replication and playback strategy and code interception technology, which does not affect production, can achieve a safe and repeatable automated testing process, can perform end-to-end testing coverage on the system under test, and can handle test scenarios of service reconstruction, technology framework upgrade, and middleware replacement, with a wide range of applications; and it does not require changes to business code or operation and maintenance operations and can be independently deployed. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 The flowchart of the steps of the present invention is shown. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0054] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0055] In the description of the present invention, it should be noted that the orientation or positional relationship indicated by the terms "upper", "lower", "inner", "outer", "front end", "rear end", "both ends", "one end", "the other end", etc. is based on the orientation or positional relationship shown in the drawings, and is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and thus should not be construed as a limitation of the present invention. In addition, the terms "first" and "second" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance.
[0056] In the description of the present invention, it should be noted that unless otherwise clearly specified and limited, the terms "installed", "provided with", "connected", etc. should be understood in a broad sense. For example, "connected" can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be directly connected or indirectly connected through an intermediate medium, and it can be the communication inside two elements. For those of ordinary skill in the art, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0057] [First Embodiment]
[0058] As Figure 1The first embodiment provided by the present invention is shown below. This embodiment provides an automated testing method based on a traffic replication and playback strategy and code interception technology, including the following steps:
[0059] S1: Establish a production environment snapshot; In this step, before implementing the traffic replication and playback strategy, it is first necessary to determine a starting point. Usually, the starting point of the production environment snapshot is set during the low peak period of traffic, such as at night, when there are very few user requests in production, which is suitable for establishing a snapshot at this time;
[0060] The production environment snapshot includes at least a database snapshot, a program package image, and system configuration; Among them, for the database snapshot, dump technology and binary log binlog synchronization technology are used. If data changes are found during the database dump, the binary log binlog record is used to apply the change to the dump result; while the program package image and system configuration are obtained through the operation and maintenance system;
[0061] When loading the data snapshot into the test environment, it should be noted that the test environment needs to be set as an access-restricted environment, and at the same time, desensitization operations are performed on user privacy data to further prevent the leakage of production data.
[0062] S2: Traffic replication; Since traffic replication technology and its related open source, commercial, or self-developed technologies are all existing technologies in this field, the implementation solution of the technology itself is not within the scope of discussion in this embodiment. In the method provided in this embodiment, it is necessary to keep the start time of traffic replication consistent with the start time of establishing the production environment snapshot, so as to ensure that when the replicated traffic is played back in the test environment and database operations are generated, the data and status of the database are the same as those of the production environment at that time, so as to achieve consistency between the playback write process and the production write process. That is, the start time of traffic replication should be the time when the database Dump is completed. The database dump has a duration, and the database state corresponding to the end of the database dump is the database state that needs to be reproduced during traffic playback. Traffic replication starts after this time, and traffic playback starts from this state.
[0063] S3: Simulate and reconstruct the playback environment context;
[0064] The context information of the process running is dynamically changing. It includes both its own state, such as memory and cache, and external states, such as the state of the database and the response data of third-party services.
[0065] The method for implementing the above state context traffic replication is through simulation to avoid performing real context replication. Specifically, it includes: after identifying the main state data, at the point where state data is obtained during traffic playback, return the specified data for it through code interception, and this state data is consistent with the production data. The simulation method refers to the process of intercepting calls, obtaining the production data status, and returning data.
[0066] Specifically, it includes the following steps:
[0067] S3.1: Select the corresponding interception code to intercept the state data, and store, transfer, and call the context information; in this embodiment, the interception means mainly targets calls involving context state consistency, as well as interception and corresponding processing at the call point during thread and service calls.
[0068] In this embodiment, when selecting the corresponding interception code to intercept the state data:
[0069] Establish the interception according to the characteristics and support of the language by selecting the corresponding interception technology. For example, for the Java language, interception technologies such as ASM, Byte Buddy, or jvm sandbox can be selected for corresponding interception. When intercepting the code, inject the interception code into the running process through dynamic injection to avoid changes to the business code and configuration changes in operation and maintenance.
[0070] The code injection functions at three points, namely, before preprocessing, afterreturning after processing, and afterthrowing for exceptions, are respectively set on the interception framework;
[0071] At the before preprocessing point, modify the parameter input of the program method, establish the thread context, or intercept the continued execution of the code and directly return the required result; modify the return result at the end of processing; if the thread context is established at the preprocessing point, clean up the corresponding thread context at the end of processing and the exception point.
[0072] For example: an interception point can be set at the business request entry position. This interception point is mainly responsible for determining whether the request is a traffic playback request. If the determination result is yes, establish a ThreadContext thread context object, which will run through the entire request lifecycle and transfer the request information to each running link, and call the tryRemove method to request to remove the context object at the end of the run. The corresponding interception code can also be automatically generated by the system time.
[0073] And when storing, transferring, and calling the context information:
[0074] Store the context information in the space of the execution thread, making it only visible within the thread, and set up the ability to obtain the context information of the thread through code execution, and be able to add content to it or query content. In this way, separate the creation and use of the context, so that the context information related to the main method and the overall logic can also be perceived and obtained in sub-methods or local logics;
[0075] When creating a child thread, the child thread does not directly obtain the context of the parent thread. By passing the request information to the child thread, the child thread creates its own context object based on the passed request information; and when intercepting the points of creating and destroying the child thread, just find a suitable point according to the implementation of the program. For example, for those using the Mapped Diagnostic Context (MDC), the setContextMap method can be intercepted to inject the request information, so that the child thread can also obtain the request information and establish its own context object; for threads managed in a thread pool, the afterExecute point can be intercepted to clear the context object.
[0076] At the same time, when making a service call, write the corresponding request information into the header of the call message;
[0077] In addition, when synchronizing data through middleware, intercept the write method, write the corresponding request message into the extended information, and in the consumption code, read the original request information in the extended information and reconstruct the context by intercepting the consumption method. Among them, the write method and the consumption method correspond to both sides of the producer-consumer pattern. The write method is used by the producer to write the message data into the middleware when generating data, and the consumption method is used by the consumer to obtain the message data from the middleware when consuming data.
[0078] S3.2: Reconstruct the user session context, keep the primary key written into the database consistent with the production result, and generate the sequence number field in the data; in this step,
[0079] When reconstructing the user session context, in the user login request, intercept the login verification to successfully create the user session. For those using third-party authentication such as mobile phone verification codes and WeChat platforms, intercept them all, prevent any possible calls to the third party, and simulate a successful result and return it to the caller;
[0080] When keeping the primary key written into the database consistent with the production result, for the auto-increment primary key, since the context of the primary key is determined by the database, intercept it during writing and inject the primary key field and primary key value consistent with the production in the Structured Query Language (SQL) to make the write result consistent with the production result;
[0081] There are various strategies for generating the serial number field in business data, and they are based on the context information at the time of generation. Therefore, when generating the serial number field in the data, the serial number field in the data can be generated based on the context information. The context information at least includes system time, the current incremental serial number, etc. Obtain the actual serial number generated at that time in production according to the business primary key, and then directly return the result serial number to the calling method by means of code interception when generating the serial number.
[0082] S3.3: Obtain the consistent data when reconstructing the context through business logic and the request time; When obtaining the consistent data when reconstructing the context through business logic and the request time:
[0083] Use business logic to help determine the uniqueness of business data, that is, which fields in the business data can determine the uniqueness of this piece of data. For the case where there are changes in the uniqueness fields, combined with the request time, further determine the specific data;
[0084] Find the unique data write statement SQL in the binary log binlog through the table name, business data uniqueness fields, and time point, obtain its primary key or serial number field value from it, and return it to the interception point program for database writing, and inject it into the data write statement SQL, so as to keep the results written in the test environment consistent with the production results. The primary key or serial number field value is the state data that needs to be kept consistent.
[0085] A typical content example of a binary log binlog is as follows:
[0086] # datetime=2024-12-24_08:15:20
[0087] INSERT INTO `funds`
[0088] (`id`, `bill_str`, `cid`, `biz_id`, `biz_str`, `biz_type`, `created_at`, `updated_at`, `approved_by`) VALUES
[0089] (1044823, 'THJS241224000001', 10000181, 80944, 'TD241224000001', 20, '2024-12-24 08:15:21', '2024-12-24 08:15:20', 1);
[0090] S3.4: Make nested calls to the program code; when making nested calls to the program code, the intercepted processing context of the called program code needs to be isolated to prevent contamination of other calls; one business process corresponds to one call, and there is a main method. The main method will call other sub-methods to form nested calls. The context needs to be isolated between nested calls and between nested calls and their callers. Because the context used by each call may overwrite the context of other calls, if they are not isolated from each other, the context between them will be confused, so isolation is needed. Of course, isolation is also required between this transfer process and other call processes.
[0091] And manage the context of nested calls in a stack manner, so that the local context of the call can be obtained only by that call, and its life cycle is consistent with that call, that is, when the call ends, the local context is cleared at the same time.
[0092] S4: Perform comparison and verification on the write operations of the database, in the order of time sequence, table, and primary key in turn, and print the inconsistent parts into the report for analysis;
[0093] S5: Optimize the traffic replication and playback strategy. When optimizing the traffic replication and playback strategy:
[0094] Take the traffic playback result as the baseline result, and compare the running result of the test code with this baseline result. The comparison process is to compare the data writing process generated by playback with the data writing process in production. Specifically, the data writing operations of the test and production are compared in the order of time sequence, table, and primary key in turn, and the comparison object is all data fields that need to be concerned in all actual business scenarios.
[0095] When the test result is consistent with the baseline result, it is determined that no optimization is required, and the interception optimization process ends;
[0096] When the test result is inconsistent with the baseline result, first judge the types of inconsistent data:
[0097] When the inconsistent data is non-status data, it is determined that the code needs to be analyzed; non-status data should be stable and unchanging values no matter when it runs. The inconsistent data is non-status data, which means that there may be problems with the business logic and the code needs to be analyzed rather than the interception optimization logic;
[0098] When the inconsistent data is status data, it is determined that interception optimization is required;
[0099] The interception optimization method at least includes adding new interception points and adding new interception logic to optimize the interception to make the consistency of the context more accurate until the status data reaches consistency with production during playback.
[0100] It is obvious to those skilled in the art that the present invention is not limited to the details of the above-described exemplary embodiments, and that the present invention can be implemented in other specific forms without departing from the spirit or essential characteristics of the present invention. Therefore, in any aspect, the embodiments should be regarded as exemplary and non-restrictive. The scope of the present invention is defined by the appended claims rather than the above description. Therefore, all changes falling within the meaning and scope of the equivalent elements of the claims are intended to be embraced within the present invention. Any reference signs in the claims should not be construed as limiting the claims concerned.
Claims
1. An automated testing method based on a traffic replication and playback strategy and code interception technology, characterized in that: It includes the following steps: S1: Create a snapshot of the production environment; S2: Traffic replication; S3: Simulate and reconstruct the context of the playback environment; including the following steps: S3.1: Select the corresponding interception code to intercept the status data, and store, transfer, and call the context information; S3.2: Reconstruct the user session context, and keep the primary key written to the database consistent with the production result, and at the same time generate the serial number field in the data; S3.3: Obtain the consistent data when reconstructing the context through business logic and request time; S3.4: Make nested calls to the program code; S4: Compare and verify the write operations of the database, and print the inconsistent parts to the report for analysis; S5: Optimize the traffic replication playback strategy; In step S3.1, when selecting the corresponding interception code to intercept the status data: Inject the interception code into the running process through dynamic injection; And code injection functions are respectively set at three points: preprocessing, end of processing, and exception on the interception framework; At the preprocessing point, modify the parameter input of the program method, establish a thread context, or intercept the continued execution of the code and directly return the required result; at the end of processing point, modify the return result; if a thread context is established at the preprocessing point, then clean up the corresponding thread context at the end of processing and exception points; In step S3.1, when storing, transferring, and calling the context information: Store the context information in the space of the execution thread, and make it only visible within the thread, and set that the context information of the thread can be obtained through the code running method, and content can be added to or queried from it; When creating a child thread, pass the request information to the child thread, and the child thread creates its own context object according to the passed request information; At the same time, write the corresponding request information to the header of the call message when making a service call; In addition, when synchronizing data through the middleware, intercept the write method, write the corresponding request message to the extended information, and read the original request information in the extended information and reconstruct the context by intercepting the consumption method in the consumption code.
2. The automated testing method based on the traffic replication and playback strategy and the code interception technology according to claim 1, wherein: In step S1, Set the starting point of the production environment snapshot at the low peak period of traffic; The production environment snapshot includes at least a database snapshot, a program package image, and system configuration; among them, the database snapshot uses the dump technology and binary log synchronization technology. If data changes are found during the database dump, the binary log record is used to apply the changes to the dump result; the program package image and system configuration are obtained through the operation and maintenance system; And when loading the data snapshot to the test environment, set the test environment as an environment with access restrictions, and at the same time perform desensitization operations on user privacy data.
3. The automated testing method based on the traffic replication and playback strategy and the code interception technology according to claim 1, characterized in that: In step S2, keep the start time of traffic replication consistent with the start time of creating the production environment snapshot.
4. The automated testing method based on the traffic replication playback strategy and code interception technology according to claim 1, characterized in that: In step S3.2, when reconstructing the user session context, and keeping the primary key written to the database consistent with the production result, and at the same time generating the serial number field in the data: When reconstructing the user session context, in the user login request, the user session is successfully created by intercepting the login verification, and a successful result is simulated and returned to the caller; When keeping the primary key written to the database consistent with the production result, for the auto-increment primary key, the write result is made consistent with the production result by intercepting during writing and injecting the primary key field and primary key value consistent with the production; When generating the sequence field in the data, the sequence field in the data is generated based on the context information, and the context information at least includes the system time and the current incrementing sequence number.
5. The automated testing method based on the traffic replication and playback strategy and the code interception technology according to claim 1, wherein: In step S3.3, when obtaining the consistency data during context reconstruction through business logic and request time: The business logic is used to help determine the uniqueness of the business data. In the case of changes to the uniqueness field, the request time is combined to further determine the specific data; The unique data write statement is found in the binary log through the table name, business data uniqueness field, and time point, and the primary key or sequence field value is obtained from it and returned to the interception point program for database writing, and it is injected into the data write statement to keep the write result in the test environment consistent with the production result.
6. The automated testing method based on the traffic replication and playback strategy and the code interception technology according to claim 1, characterized in that: In step S3.4, when making nested calls to the program code, the called program code needs to maintain isolation when intercepting and processing the context, and manages the context of the nested calls in a stack manner.
7. The automated testing method based on the traffic replication playback strategy and code interception technology according to claim 1, characterized in that: In step S4, when performing comparison verification on the write operation of the database, it is carried out in the order of time sequence, table, and primary key.
8. The automated testing method based on the traffic replication and playback strategy and code interception technology according to claim 1, characterized in that: In step S5, when optimizing the traffic replication playback strategy, the traffic playback result is used as the baseline result, and the running result of the test code is compared with this baseline result: When the test result is consistent with the baseline result, it is determined that no optimization is required, and the interception optimization process ends; When the test result is inconsistent with the baseline result, first judge the type of inconsistent data: When the inconsistent data is non-status data, it is determined that the code needs to be analyzed; When the inconsistent data is status data, it is determined that interception optimization is required; The interception optimization method at least includes adding new interception points and adding new interception logics.
Citation Information
Patent Citations
Non-intrusive strategy reconstruction method and device based on traffic recording and playback
CN115391223A
Automatic testing method and related device based on traffic replication and playback
CN117555809A