Try traffic playback, no need to write annoying automated test cases anymore!

Hello everyone, I am an experienced driver of automated testing. Students who have been exposed to interface automated testing all know that we generally need to write automated cases based on a certain automated testing framework. The basis for writing automated cases comes from the interface document, and the requests in the interface document are compared. Parameters are manually added to the interface automation case

In fact, for the daily iteration of new server requirements, it is still acceptable to manually add less than 20 interface automation cases at one time. If more than 50 interface cases need to be added at one time, it will be very time-consuming to fill them in manually.

There is another common scenario where some old service modules need to be migrated or reconstructed. These old modules themselves may not have interface documents, and the business logic has not changed. How to ensure that the original business runs correctly after migration is very important. It is difficult to ensure that there are no problems online after migration by relying solely on QA for interface testing.

In order to maximize the coverage test, we can copy the online traffic to offline to generate automated cases for functional testing of the interface and diff testing. This technology is called traffic playback.

What is traffic replay

Traffic playback can be understood literally. Traffic can be understood as the amount of data sent and received on the Internet. Since our network communication protocols are generally HTTP requests, the front-end and back-end interaction methods are generally through the back-end API interface, so The form of traffic can be understood as the number of online interface requests

Playback is to change the location of the interface request information, such as storing it in an offline database, Redis, or a distributed big data cluster. It can also be played back in real time without storage, allowing us to take advantage of online traffic.

Traffic replay can be easily understood in two words, which is traffic

Traffic playback can be used directly after copying the traffic, or it can also be used after amplifying or reducing the traffic.

Traffic copy refers to online formal traffic through rewriting business logic, tcpcopy, log playback and other methods Playback in the environment to achieve the purpose of testing

Traffic copy can completely simulate online traffic to simulate and test complex business scenarios without affecting online services.

Copy one traffic to multiple copies, and send each traffic to the same instance after modifying the destination address. For example, if you copy 5 copies of traffic to the same instance, this instance can withstand 5 times more pressure than the online environment at the same time. It can realize stress testing of offline environments, which is called traffic amplification

Some friends asked, can it be enlarged or reduced? Of course, traffic reduction is achieved by copying part of the traffic and processing it to achieve traffic reduction. Reasons for doing this may be to reduce the size of the traffic or for further analysis and processing of the traffic

Scenarios that are difficult to cover in offline testing can be tested through traffic copy. The testing scenarios are wider and the coverage is larger.

Industry experience

At present, there are some commonly used traffic diversion solutions in the industry.

Ali Doom

Doom is a platform that replicates a portion of real online traffic and uses it for automated regression testing. Its traffic recording and playback functions are implemented through AOP aspect programming within the application. Since the bottom layer uses Java instrument to implement AOP, it currently only supports the access and use of Java applications. The schematic diagram is as follows:

Picture

TCPCopy

TCPCopy is a request copy (all tcp-based packets) tool, consisting of tcpcopy and intercept, where tcpcopy runs on the online service and uses the original socket to copy the online traffic to the test service (pink line), intercept is deployed on On the auxiliary server, it is responsible for receiving the response (green line) and passing it back to tcpcopy (purple line). See the figure below for details:

Picture

GoReplay

Goreplay is an HTTP real-time traffic replication tool developed in Golang. It supports traffic amplification, reduction, and frequency limitation. It also supports recording requests to files to facilitate playback and analysis. It also supports integration with ElasticSearch to store traffic into ES for real-time analysis. GoReplay is not a proxy, but Listening to traffic on a network interface requires no changes to the production infrastructure and simply runs on the same machine as the service. See the figure below for specific principles

Picture

GoReplay principle

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

Nginx’s ngx_http_mirror_module module

The ngx_http_miror_module module was introduced in Nginx 1.13.4 to support application layer traffic replication. This module implements traffic replication through mirror configuration instructions. As shown in the figure below, you can copy proxy.local traffic to test.local through the following configuration

Picture

upstream backend {
server backend.local:10000;
}

upstream test_backend {
server test.local:20000;
}

server {
server_name proxy.local;
listen 8000;

location/{
mirror /mirror;
proxy_pass http://backend;
}
\t
location = /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}
}

Each mirror configuration item corresponds to a copy of the user’s request, and the “traffic amplification” effect can be achieved by configuring multiple mirror instructions. Of course, you can also forward multiple copies to different backend target systems

Three ways of traffic playback

From the perspective of traffic drainage itself, there are three main types, namely: main path replication, bypass replication, and log playback.

Main path replication:

Picture

Main road copy

Main path copy refers to copying traffic in the call chain. One is to copy the traffic in the business logic. For example, in the process of calling the API, the business party writes code logic to record the request/response information content. The other is to copy the request/response information content in the framework (such as Alibaba’s Doom). ) to perform traffic replication in the processing logic.

  • Advantages: It can be highly combined with business logic to achieve fine-grained customized traffic replication. For example, it can only replicate traffic with certain characteristics.

  • Disadvantages: Business logic and traffic diversion logic are highly coupled and functionally affect each other; Each request requires additional traffic diversion processing, which has a performance impact on the business process

Bypass replication

Picture

Bypass replication Generally, a third-party service monitors the replication traffic in the network protocol stack and has no influence on the business. For example, TCPCopy, similar tools include GoReplay, etc.

  • Advantages: Decoupled from the business, the traffic diversion module can be independently deployed and upgraded. The business side needs to pay attention to the implementation of the traffic diversion function.

  • Disadvantages: After capturing network packets at the layer 4 network card level, data packets still need to be reorganized and parsed, which requires additional consumption of computing resources. It often requires full packet capture and parsing before filtering, which cannot be combined with business logic. Carry out customized sampling

Features of gateway-based business: 1) Large traffic, indiscriminate packet capture, unpacking and filtering will consume a lot of CPU; 2) To undertake the traffic of all product lines, it is necessary to provide traffic replication capabilities for specific product lines

Special note: Traffic replication is performed on online services, so it will consume the CPU resources of online machines. In order not to affect online business, it is necessary to monitor the resource usage of online machines during traffic replication > Once too many resources are consumed, traffic replication needs to be stopped immediately

Log playback

Log playback is somewhat similar to bypass replication, but instead of listening to network protocol requests, a log agent is placed on the online service instance to transfer the logs to the big data distributed cluster.

The premise of using log playback is that the logs that need to be printed in the business code logic need to include the necessary interface information such as the request parameters of the interface, URL, Body, request method, etc. If there is no log that prints the interface information, log playback cannot be performed.

Therefore, to adopt this playback method, the first step is to develop to assist in standardizing logs.

Picture

After storing the logs in the HDFS/AFS cluster, you can deploy a hadoop client to pull the logs on the cluster, process the logs, parse out the interface request information, and analyze the traffic if necessary. After filtering, playback

Let’s share a demo program that uses Python multi-process to parse logs and obtain traffic. Assume that the logs have been pulled down from the cluster. The log directory structure is as follows:

Picture

The content of each log file service.log.wf.2023072615.18 is: warning logs line after line, and some warning logs contain interface information.

WARNING 247629242023072613:00:24 Present.php:44 qa_traffic_playback: {"request_method":"POST","service_method":"getUserInfo","path":"\ \/service\/test?ngscfr=getUserInfo_10.221.110.27_fm_smallapp & amp;method=getUserInfo & amp;format=php & amp;ie=utf-8","query":{"ngscfr" :"getUserInfo_10.221.110.27_fm_smallapp","method":"getUserInfo","format":"php","ie":"utf-8"}, "post":{"account_id":"59","account_type":"1","user_id":"123456","method":\ "getUserInfo","format":"php","ie":"utf-8","service_array_key":"a:0:{}"," tb_sig":"7f2fcc8b398ca16979"}}
WARNING 247629242023072613:00:24 Present.php:44 qa_traffic_playback: {"request_method":"POST","service_method":"getUserInfo","path":"\/service \/test?ngscfr=getUserInfo_10.221.110.27_fm_smallapp & amp;method=getUserInfo & amp;format=php & amp;ie=utf-8","query":{"ngscfr":" getUserInfo_10.221.110.27_fm_smallapp","method":"getUserInfo","format":"php","ie":"utf-8"},"post ":{"account_id":"59","account_type":"1","user_id":"56789","method":"getUserInfo\ ","format":"php","ie":"utf-8","service_array_key":"a:0:{}","tb_sig" :"7f2fcc8b398ca16979"}}
import os
import re
import json
import multiprocessing

def parse_log_file(log_file):

    log_entries = []

    with open(log_file, 'r') as file:
        for line in file:
            if'{"request_method"'in line:
                # Use regular expressions to match JSON dictionaries
                pattern = re.compile(r'{.*?}}')
                match = pattern.search(line)
                if match:
                    json_dict = match.group()

                    print("json_dict",json_dict)

                    # Parse JSON dictionary
                    data = json.loads(json_dict)
                    print("data",data)
                    print("Parse JSON Dictionary",json_dict)

    return log_entries

def process_directory(directory, results_dict):

    log_files = []
    
    for root, dirs, files in os.walk(directory):
        
        for file in files:
            if file.startswith('service_present.log.wf'):
                log_file = os.path.join(root, file)
                log_files.append(log_file)
                # Process each matching log file here
                print("log_files B", log_file)

    parsed_entries=[]
    for log_file in log_files:
        parsed_entries + = parse_log_file(log_file)
    results_dict[directory] = parsed_entries

if __name__ == '__main__':
    
    root_directory = '/home/work/'# Specify the root directory
    results = multiprocessing.Manager().dict()

    processes = []
    for subdir in os.listdir(root_directory):
        directory = os.path.join(root_directory, subdir)
        if os.path.isdir(directory):
            process = multiprocessing.Process(target=process_directory, args=(directory, results))
            processes.append(process)
            process.start()

    for process in processes:
        process.join()

    for directory, entries in results.items():
        print(f'Directory: {directory}')
        for entry in entries:
            print(entry)

Explain this program. The process_directory method is used to traverse the log directory and obtain the absolute path of the log file. The parse_log_file method is used to parse the log file under the corresponding path, using regular expressions. Extract the Json string containing interface information in the log and convert it into a dictionary for storage

The subsequent logic is to read the information in the dictionary and generate interface automation cases for interface automation testing or diff testing.

This code snippet can also be optimized, because it exists in the dict dictionary and directly occupies memory. If there is too much interface information, the memory usage may be too high and the memory will be exhausted. At this time, you can choose to replace the interface with Information is stored in the database or Redis

Finally, I would like to mention that in the actual application process, we may also use algorithms to filter traffic, and obtain the specified traffic through preliminary screening and fine screening. In addition, we also need to count the traffic coverage rate played back, so that we can be more accurate. Measure traffic coverage

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.