Stress testing method and device based on raw socket and user-mode microstack thread
By using raw sockets and user-mode micro-stack threads in the stress test, it breaks through the connection number limit and thread switching resource consumption of the TCP/IP protocol stack, achieves efficient system resource utilization, and solves the problem of the connection number problem of traditional stress testing tools. Limitation and resource waste issues are eliminated, significantly improving test efficiency and performance.
Patent Information
- Application Number
- CN202411171191.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-26
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2044-08-26
AI Technical Summary
Traditional stress testing tools are limited by TCP/IP protocol stack and thread switching resource consumption, resulting in a limited number of connections initiated by a single machine and serious waste of system resources in high-concurrency scenarios, making it impossible to efficiently utilize system resources.
Adopt a stress testing method based on raw sockets and user-mode micro-stack threads to break through the limit on the number of connections by modifying the source address IP, and reduce thread switching resource consumption through user-mode micro-stack threads to achieve efficient use of system resources.
With the same machine performance, a single machine can support millions of levels of pressure testing, significantly saving costs and time, and simplifying high-performance pressure testing operation steps.
Smart Images

Figure CN118694684B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a pressure testing method and device based on original socket and user-mode micro-stack thread, belonging to the technical field of computer network. Background Art
[0002] In the modern computer network environment, stress testing is one of the important means to evaluate system performance and stability. Traditional stress testing generally involves building a stress testing cluster or purchasing online stress testing services to perform stress testing on the target system. The cost of stress testing is high, and the stress testing process is uncontrollable.
[0003] At present, there are two bottlenecks in traditional stress testing tools. First, when a single test machine initiates large-scale connection requests, it is limited by the total number of ports of the TCP / IP protocol (a single test machine IP address has a maximum of 65536 ports). In other words, due to the limitations of the traditional TCP / IP protocol stack, the maximum number of connections that can be initiated by a single machine is 65536. Second, traditional stress testing tools use each link to initiate a thread. When each thread occupies and gives up the CPU, there are context switches and operating system resources for kernel mode and user mode switching. Especially when tens of thousands of threads occupy the CPU at the same time, a large amount of system resources are consumed on context switching, which greatly wastes limited system resources.
[0004] In the scenario of high-concurrency stress testing, it is extremely important to efficiently utilize the system resources of the stress test initiator. To this end, the present invention proposes a stress testing method based on raw sockets and user-mode micro-stack threads (hereinafter referred to as "micro-stack threads"). Summary of the invention
[0005] In order to solve the above problems, the present invention proposes a stress testing method and device based on raw sockets and user-mode microstack threads, which can efficiently utilize the system resources of the test machine, reduce the cost of stress testing, and simplify the operating steps of high-performance stress testing.
[0006] The technical solution adopted by the present invention to solve the technical problem is:
[0007] In a first aspect, an embodiment of the present invention provides a stress testing method based on raw sockets and user-mode microstack threads, comprising the following steps:
[0008] Step S1, obtaining the pressure test parameters of the target pressure test server, wherein the pressure test parameters include the target pressure test server address, the target port number, the number of start test nodes and the random IP range;
[0009] Step S2, constructing a user-mode micro-stack thread framework and initiating a request for each connection;
[0010] Step S3, setting the micro stack thread to initiate connection and response logic nodes, and generating a specified number of test nodes;
[0011] Step S4, sending a stress test request to each test node according to the stress test parameters, and performing stress testing on each test node of the target stress test server;
[0012] Step S5, collecting the stress test results of each test node, and determining the stress test results of the target stress test server.
[0013] As a possible implementation of this embodiment, the step of constructing a user-mode micro-stack thread framework includes the following steps:
[0014] Step S21, define a microstack thread structure, and define a microstack thread list variable and a scheduler context variable; the microstack thread structure includes a thread context, a microfunction pointer, a parameter list, a stack pointer and a microstack thread state, and the microstack thread state includes "READY", "RUNNING", "WAITTING" and "TERMINATED"; the microstack thread list variable is used to store all microstack thread state information; the scheduler context variable is used to store the scheduler context;
[0015] Step S22, create a micro stack thread structure, and write the micro function pointer and parameter list into the structure;
[0016] Step S23, traverse the microstack thread list in sequence, find the running microstack thread, mark it as ready, and save its running context;
[0017] Step S24, define a yield function, save the context of the running thread, mark its state as ready, and switch to the context of the scheduler;
[0018] Step S25, defining a resume function, finding a specified microstack thread in the microstack thread list, marking it as running, switching to the context of the microstack thread and running it;
[0019] Step S26, define a schedule function, poll the status of all microstack threads in the microstack thread list, and resume their operation if their status is ready.
[0020] As a possible implementation of this embodiment, step S22 includes the following steps:
[0021] Define the create_thread function and apply to the system for at least 64KB of stack memory to write into the structure;
[0022] Use the C language library function setjmp to save the current program context. If the save is successful, set the current microstack thread state to the ready state and register the structure to the microstack thread list.
[0023] The return value of the library function setjmp determines whether the context is restored by the C language standard library longjmp. If not, the current function is terminated and returned to the caller. If so, the CPU registers are manually operated using inline assembly.
[0024] By assigning the tail end of the stack pointer address in the microstack thread structure to the rsp register;
[0025] Mark the currently running microstack thread as running; use the function pointer in the microstack thread structure to pass in the specified parameters to run the specified function. When the function is executed, mark the microstack thread as terminated, and then use the C language standard library longjmp to switch back to the scheduler to return the CPU time slice.
[0026] As a possible implementation of this embodiment, step S23 includes the following steps:
[0027] Define the switch_thread function, traverse the microstack thread list in sequence, find the running microstack thread x, mark it as ready, and use the library function setjmp to save its running context;
[0028] Take the next microstack thread y of microstack thread x, set the running state of microstack thread y to running state, use the C language standard library longjmp to restore the context of microstack thread y and run.
[0029] As a possible implementation of this embodiment, step S3 includes the following steps:
[0030] Step S31, constructing an original socket and initiating a connection, using the socket function to create an original socket;
[0031] Step S32, defining the original socket sending function, receiving the original socket handle and the data to be sent;
[0032] Step S33, define a heartbeat sending function, send heartbeat data to the socket handle, and use the yield function to give up the time slice. When the time slice returns, check whether it has waited for more than 60 seconds. If not, continue to give up the time slice. If so, repeat sending the heartbeat data packet and finally return the CPU occupancy.
[0033] Step S34, initiating a connection and response logic node, generating a specified number of test nodes.
[0034] As a possible implementation of this embodiment, step S32 includes the following steps:
[0035] Apply for a memory buffer from the system, and assign the data to be sent to the specified position in the memory buffer according to the size of the IP header and TCP header as the offset;
[0036] Construct the IP header and TCP header of the original socket, point to the memory buffer respectively and calculate the checksum, the structures of the IP header and TCP header are iphdr and tcphdr respectively, and the field saddr (session address, indicating the memory address of the current record) of the IP header is a random source address IP;
[0037] Construct a pseudo header, perform TCP checksum calculation, and fill the checksum value into the check field of the TCP header. The source address of the pseudo header is the random source address IP of the IP header, and the structure of the pseudo header is pseudo_header;
[0038] Use the C language library function send_to to send memory buffer data.
[0039] As a possible implementation of this embodiment, step S34 includes the following steps:
[0040] Create a raw socket handle and initiate a connection to the target service;
[0041] Build a registration data package and send it to the target stress test server;
[0042] Use microstack threads to build and run the heartbeat sending node to generate a test node;
[0043] Construct an infinite loop on the test node to listen to the original socket handle and wait for server messages. Use the yeild function to give up the CPU usage and wait until the next resumption of operation to check whether the server data is received. If the server data is received, parse the server data and return the test node IP address and port and the target stress test server IP address and port to the server. Otherwise, give up the CPU usage again.
[0044] In a second aspect, an embodiment of the present invention provides a stress testing device based on raw sockets and user-mode microstack threads, comprising:
[0045] A data acquisition module is used to obtain the pressure test parameters of the target pressure test server, wherein the pressure test parameters include the target pressure test server address, the target port number, the number of start test nodes and the random IP range;
[0046] The connection request module is used to build a user-mode micro-stack thread framework and initiate a request for each connection;
[0047] The test node generation module is used to set the micro stack thread to initiate the connection and respond to the logical node, and generate a specified number of test nodes;
[0048] The stress test module is used to send stress test requests to each test node according to the stress test parameters, and perform stress tests on each test node of the target stress test server;
[0049] The test result acquisition module is used to collect the stress test results of each test node and determine the stress test results of the target stress test server.
[0050] In a third aspect, an embodiment of the present invention provides a computer device, comprising a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor. When the computer device is running, the processor communicates with the memory through the bus, and the processor executes the machine-readable instructions to perform any step of the above-mentioned stress testing method based on raw sockets and user-mode microstack threads.
[0051] In a fourth aspect, an embodiment of the present invention provides a storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above-mentioned stress testing methods based on raw sockets and user-mode microstack threads are executed.
[0052] The beneficial effects of the technical solution of the embodiment of the present invention are as follows:
[0053] A stress test method based on raw sockets and user-mode micro-stack threads of the technical solution of the embodiment of the present invention comprises the following steps: step S1, obtaining stress test parameters of the target stress test server, the stress test parameters including the target stress test server address, the target port number, the number of test nodes started and the random IP range; step S2, constructing a user-mode micro-stack thread framework, initiating a request for each connection; step S3, setting the micro-stack thread to initiate a connection and respond to a logical node, generating a specified number of test nodes; step S4, sending a stress test request to each test node according to the stress test parameters, and performing a stress test on each test node of the target stress test server; step S5, collecting the stress test results of each test node, and determining the stress test results of the target stress test server. The present invention not only uses raw sockets to initiate a connection, but also a single stress test machine can support a million-level stress test under the same machine performance, which greatly saves the money cost and time cost of the stress test, and the system resources of the test machine are efficiently utilized through the user-mode micro-stack thread, which reduces the cost of the stress test and simplifies the operation steps of the high-performance stress test.
[0054] A stress testing device based on raw sockets and user-mode micro-stack threads according to the technical solution of an embodiment of the present invention has the same beneficial effects as a stress testing method based on raw sockets and user-mode micro-stack threads according to the technical solution of an embodiment of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 It is a flow chart of a stress testing method based on raw sockets and user-mode microstack threads according to an exemplary embodiment;
[0056] Figure 2 It is a structural schematic diagram of a stress testing device based on raw sockets and user-mode microstack threads according to an exemplary embodiment;
[0057] Figure 3 is a structural schematic diagram of a specific implementation scheme of a pressure test of the present invention according to an exemplary embodiment;
[0058] Figure 4 This is a traditional stress test thread switching that requires deep kernel state example diagram;
[0059] Figure 5 This is an example diagram showing that the microstack thread does not need to go deep into the kernel state as shown in the present invention. DETAILED DESCRIPTION
[0060] In order to more clearly illustrate the technical features of the solution of the present invention, the present invention is described in detail below through specific implementation methods and in conjunction with the accompanying drawings.
[0061] Description of the key technical terms used in this invention.
[0062] C language standard library functions setjmp and longjmp, setjmp and longjmp are two functions in the C standard library, which are the core of implementing user-mode micro-stack threads. They are used to implement non-local jumps. These two functions are mainly used in scenarios where the execution status needs to be saved and restored between different functions, such as exception handling, coroutines, and user-level threads.
[0063] The setjmp function is used to save the current execution context (including the program counter, stack pointer, and general registers) to a variable of type jmp_buf. The longjmp function is used to restore the execution context previously saved by setjmp. After calling longjmp, the program execution will jump back to the location where setjmp was previously called, and a specified non-zero value will be returned from setjmp.
[0064] The rsp register (Stack Pointer) is used to point to the top of the current stack in the x86-64 architecture. The stack is a memory area used to store local variables, return addresses of function calls, and other temporary data. The rsp register plays a vital role in stack operations. By operating the rsp register, stack push and pop operations can be implemented to ensure that temporary data and status are properly managed during function execution.
[0065] like Figure 1 As shown, a stress testing method based on raw sockets and user-mode microstack threads provided by an embodiment of the present invention includes the following steps:
[0066] Step S1, obtaining the pressure test parameters of the target pressure test server, wherein the pressure test parameters include the target pressure test server address, the target port number, the number of start test nodes and the random IP range;
[0067] Step S2, constructing a user-mode micro-stack thread framework and initiating a request for each connection;
[0068] Step S3, setting the micro stack thread to initiate connection and response logic nodes, and generating a specified number of test nodes;
[0069] Step S4, sending a stress test request to each test node according to the stress test parameters, and performing stress testing on each test node of the target stress test server;
[0070] Step S5, collecting the stress test results of each test node, and determining the stress test results of the target stress test server.
[0071] As a possible implementation of this embodiment, the step of constructing a user-mode micro-stack thread framework includes the following steps:
[0072] Step S21, define a microstack thread structure, and define a microstack thread list variable and a scheduler context variable; the microstack thread structure includes a thread context, a microfunction pointer, a parameter list, a stack pointer and a microstack thread state, and the microstack thread state includes "READY", "RUNNING", "WAITTING" and "TERMINATED"; the microstack thread list variable is used to store all microstack thread state information; the scheduler context variable is used to store the scheduler context;
[0073] Step S22, create a micro stack thread structure, and write the micro function pointer and parameter list into the structure;
[0074] Step S23, traverse the microstack thread list in sequence, find the running microstack thread, mark it as ready, and save its running context;
[0075] Step S24, define a yield function, save the context of the running thread, mark its state as ready, and switch to the context of the scheduler;
[0076] Step S25, defining a resume function, finding a specified microstack thread in the microstack thread list, marking it as running, switching to the context of the microstack thread and running it;
[0077] Step S26, define a schedule function, poll the status of all microstack threads in the microstack thread list, and resume their operation if their status is ready.
[0078] As a possible implementation of this embodiment, step S22 includes the following steps:
[0079] Define the create_thread function and apply to the system for at least 64KB of stack memory to write into the structure;
[0080] Use the C language library function setjmp to save the current program context. If the save is successful, set the current microstack thread state to the ready state and register the structure to the microstack thread list.
[0081] The return value of the library function setjmp is used to determine whether the context is restored by the C language standard library longjmp. If not, the current function is terminated and returned to the caller. If so, the CPU registers are manually operated using the inline assembly method.
[0082] By assigning the tail end of the stack pointer address in the microstack thread structure to the rsp register;
[0083] Mark the currently running microstack thread as running; use the function pointer in the microstack thread structure to pass in the specified parameters to run the specified function. When the function is executed, mark the microstack thread as terminated, and then use the C language standard library longjmp to switch back to the scheduler to return the CPU time slice.
[0084] As a possible implementation of this embodiment, step S23 includes the following steps:
[0085] Define the switch_thread function, traverse the microstack thread list in sequence, find the running microstack thread x, mark it as ready, and use the library function setjmp to save its running context;
[0086] Take the next microstack thread y of microstack thread x, set the running state of microstack thread y to running state, use the C language standard library longjmp to restore the context of microstack thread y and run.
[0087] As a possible implementation of this embodiment, step S3 includes the following steps:
[0088] Step S31, constructing an original socket and initiating a connection, using the socket function to create an original socket;
[0089] Step S32, defining the original socket sending function, receiving the original socket handle and the data to be sent;
[0090] Step S33, define a heartbeat sending function, send heartbeat data to the socket handle, and use the yield function to give up the time slice. When the time slice returns, check whether it has waited for more than 60 seconds. If not, continue to give up the time slice. If so, repeat sending the heartbeat data packet and finally return the CPU occupancy.
[0091] Step S34, initiating a connection and response logic node, generating a specified number of test nodes.
[0092] As a possible implementation of this embodiment, step S32 includes the following steps:
[0093] Apply for a memory buffer from the system, and assign the data to be sent to the specified position in the memory buffer according to the size of the IP header and TCP header as the offset;
[0094] Construct the IP header and TCP header of the original socket, point to the memory buffer respectively and calculate the checksum, the structures of the IP header and TCP header are iphdr and tcphdr respectively, and the field saddr of the IP header is the random source address IP;
[0095] Construct a pseudo header, perform TCP checksum calculation, and fill the checksum value into the check field of the TCP header. The source address of the pseudo header is the random source address IP of the IP header, and the structure of the pseudo header is pseudo_header;
[0096] Use the C language library function send_to to send memory buffer data.
[0097] As a possible implementation of this embodiment, step S34 includes the following steps:
[0098] Create a raw socket handle and initiate a connection to the target service;
[0099] Build a registration data package and send it to the target stress test server;
[0100] Use microstack threads to build and run the heartbeat sending node to generate a test node;
[0101] Construct an infinite loop on the test node to listen to the original socket handle and wait for server messages. Use the yeild function to give up the CPU usage and wait until the next resumption of operation to check whether the server data is received. If the server data is received, parse the server data and return the unique identifier (the test node IP address and port and the target stress test server IP address and port) to the server. Otherwise, give up the CPU usage again.
[0102] like Figure 2 As shown, an embodiment of the present invention provides a stress testing device based on raw sockets and user-mode microstack threads, comprising:
[0103] A data acquisition module is used to obtain the pressure test parameters of the target pressure test server, wherein the pressure test parameters include the target pressure test server address, the target port number, the number of start test nodes and the random IP range;
[0104] The connection request module is used to build a user-mode micro-stack thread framework and initiate a request for each connection;
[0105] The test node generation module is used to set the micro stack thread to initiate the connection and respond to the logical node, and generate a specified number of test nodes;
[0106] The stress test module is used to send stress test requests to each test node according to the stress test parameters, and perform stress tests on each test node of the target stress test server;
[0107] The test result acquisition module is used to collect the stress test results of each test node and determine the stress test results of the target stress test server.
[0108] In the scenario of high-concurrency stress testing, it is extremely important to efficiently utilize the system resources of the stress test initiator. The present invention makes efficient use of the system resources of the test machine through the following two improvements.
[0109] 1. Use raw sockets to forge the source IP address to break the upper limit of the number of connections initiated by a single machine.
[0110] In software stress testing, a large number of clients are used to simultaneously initiate a large number of connection requests to the target stress test machine to test the connection pressure that the target stress test machine can bear. Usually, the stress test service under test needs to be able to handle millions or even tens of millions of connections.
[0111] Due to the design of the TCP / IP protocol stack, the TCP / IP service as a server is not limited by the number of ports, while the client initiating a connection needs to use the source IP address, source port, destination IP address and destination port unique identifiers to mark the uniqueness of this connection.
[0112] In the stress test scenario, the target IP and target port number in the TCP / IP protocol cannot be changed. The source address port number is a 16-bit unsigned integer ranging from 0 to 65535, which can represent a total of 65536 different port numbers. Therefore, a single IP address can only support a maximum of 65536 concurrent connections when using TCP to initiate a connection. The result is that if the requirement is to initiate a stress test with one million connections to the target service, in an ideal situation, all the port numbers of each machine will be used, which requires at least 14 (1000000 / 65536) stress test machines. This limitation will become a bottleneck in the stress test scenario that needs to simulate large-scale concurrent connections.
[0113] The first problem to be solved by the present invention is to bypass the TCP / IP protocol connection number limit of a single machine and allow a single machine to initiate more connections (more than 65536). Usually, network programming uses sockets for network communication. When building a socket, the system will write the IP address on the network card into the socket configuration, and developers are not allowed to configure it by themselves. Figure 3 As shown, the present invention modifies the source address IP configuration by using raw sockets, so that the source address IP can be any value that complies with IP rules, so that the combination of source address IP and source port can be arbitrarily large, thereby breaking through the upper limit (65536) of the number of TCP links that can be initiated by a single physical computer.
[0114] 2. User-mode micro-stack threads reduce resource consumption caused by frequent thread switching.
[0115] Mainstream stress testing tools such as Apache JMeter initiate requests for each connection by creating threads. The number of threads initiated is the same as the number of connections initiated. However, for the CPU, once a large number of threads are created, each thread is competing for the available time slice of the CPU, which will cause a huge waste of resources in the thread switching process (kernel state and user state conversion, etc.). This limitation is particularly prominent in stress testing scenarios that require simulating large-scale concurrent connections. The more concurrent connections and threads there are, the more computing resources are consumed in thread switching. Generally speaking, the performance consumption of this part grows exponentially. After exceeding a certain number of threads, multi-threaded operations will become a performance bottleneck.
[0116] The second problem that the present invention needs to solve is the waste of resources caused by the creation of a large number of threads and frequent context switching. Through the micro-stack thread in user mode, the waste of resources caused by traditional thread switching is reduced, and the utilization efficiency of system performance is improved.
[0117] The present invention improves the performance of a single stress testing machine through the above two technologies, so that a machine can initiate as many connection requests as possible, so as to maximize the performance of the stress testing machine.
[0118] Because C language is the de facto underlying application development language in the industry, the present invention uses the standard library functions of C language as examples.
[0119] The present invention assumes that the requirements of the target test service are: active message "registration" and "heartbeat"; the response logic is "confirmation receipt". In layman's terms, after each client is successfully connected, the client needs to actively report its own ID, location and other information, and the heartbeat is to actively initiate heartbeat data every minute to prove that the client itself is alive. The confirmation receipt is a reply message unique identifier to the message actively sent by the server, so that the server knows that the client has received the message. The client described in the present invention represents each stress test initiating node described in the present invention, and the server is the target stress test service.
[0120] The present invention assumes that the stress test process uses a configuration file to mark configuration items in the stress test process, such as the target stress test server address, target port number, number of start test nodes, random IP range and other configuration items.
[0121] The target test service requirement assumed by the present invention is a basic server-client interaction method in the industry, which is a common practice in the industry.
[0122] Based on socket network programming, such as Figure 3 As shown, the key steps of the technical solution of the present invention under the Linux system are mainly described below.
[0123] 1. Build a user-mode micro-stack thread framework (hereinafter referred to as the micro-stack thread framework).
[0124] 1.1 Define [microthread_t], including thread context, function pointer, parameter list, stack pointer, and microthread status. The microthread statuses are "READY", "RUNNING", "WAITTING", and "TERMINATED".
[0125] Define the variable [microthread threads] to save all microthread status information.
[0126] Define the variable [scheduler_context] to save the scheduler context.
[0127] 1.2 Define the function [Create and register micro-stack thread (create_thread)], which accepts parameters such as micro-function pointer and parameter list. Create a micro-stack thread structure, and write the parameter function pointer and parameter list into the structure. Apply for 64KB (experience value) from the system as stack memory and write it into the structure. Use the C language library function setjmp to save the current program context. If the save is successful, set the current micro-stack thread state to the ready state and register the structure to the micro-stack thread list. Use the setjmp return value to determine whether it is the context environment restored by the C language standard library longjmp. If not, end the current function and return to the caller. If it is the context environment restored by the C language standard library longjmp, use the embedded assembly method to manually operate the CPU register. By assigning the tail end of the stack pointer address in the micro-stack thread structure to the rsp register. So that this micro-stack thread uses the specified stack space and no longer uses the original stack space. Mark the currently running micro-stack thread as running. Use the function pointer in the microstack thread structure to pass in the specified parameters to run the specified function. When the function is executed, mark the microstack thread as terminated, and then use longjmp to switch back to the scheduler to return the CPU time slice. This function ends.
[0128] 1.3 Define the function [thread switch (switch_thread)], traverse the microstack thread list in turn, find the running microstack thread x, mark it as ready, and use setjmp to save its running context. Get the next microstack thread y of microstack thread x, set the running state of microstack thread y to running state, use the C language standard library longjmp to restore the context of microstack thread y and run.
[0129] 1.4 Define the function [yield], save the context of the running thread, and mark its state as ready; use longjmp to switch to the context of the scheduler.
[0130] 1.5 Define the function [resume], find the specified microstack thread x in the microstack thread list, mark it as running, use longjmp to switch to the context of microstack thread x and run it.
[0131] 1.6 Define the function [Schedule], which is an infinite loop function. In the function, it always polls the status of all microstack threads in the microstack thread list. If its status is ready, the resume function is used to resume its operation.
[0132] 2 Define the raw socket handle function (create_socket).
[0133] 2.1 Build a raw socket and initiate a connection. First, use the socket function to create a raw socket using the following statement:
[0134] int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
[0135] Among them, the parameters SOCK_RAW and IPPROTO_TCP are used to create raw sockets.
[0136] 2.2 Return the sock for reuse.
[0137] 3. Define the original socket sending function (send_package), and the receiving parameters are the original socket handle and the data to be sent.
[0138] 3.1 Apply for a memory buffer from the system. And assign the data to be sent to the specified position of the memory buffer according to the size of the IP header and TCP header as the offset.
[0139] 3.2 Build the original socket IP header (struct iphdr) and TCP header (struct tcphdr), point to the memory buffer and calculate the checksum. Note that the IP header field saddr is set to a random source IP address (random within the range specified in the configuration file).
[0140] 3.3 Build a pseudo header (struct pseudo_header) for TCP checksum calculation. Note that source_address is set to the random source IP address mentioned above. And fill the check value into the check field of the TCP header.
[0141] 3.4 Use the C language library function send_to to send the above memory buffer data.
[0142] 4. Define the heartbeat sending function (the receiving parameter is the socket handle).
[0143] 4.1 Send heartbeat data to the handle using step 3 above.
[0144] 4.2 Use the yield function in step 1.4 to give up the time slice.
[0145] 4.3 When the time slice returns, check whether the waiting time has exceeded 60 seconds. If not, continue to give up the time slice. If so, repeat step 4.2 to send heartbeat data packets and finally return the CPU occupation.
[0146] 5. Define the microstack thread to initiate connection and respond to logical nodes.
[0147] 5.1 Use step 2 to create a raw socket handle and initiate a connection to the target service.
[0148] 5.2 Build a registration data packet and send the registration data packet to the target server using step 3.
[0149] 5.3 Use step 4 to build a heartbeat sending node using the microstack thread and run it to generate a test node.
[0150] 5.4 Build an infinite loop in the test node to listen to the original socket handle and wait for the server message, use the yeild function to give up the CPU usage, and wait until the next resumption of operation to check whether the server data is received. If the server data is received, parse the server data and return the unique identifier (the test node IP address and port and the target stress test server IP address and port) to the server using the above step 3. If the server data is not received, give up the CPU usage again.
[0151] 6. Build a connection initiating node and initiate a connection.
[0152] 6.1 Load the configuration and read the target stress test server address, target port number, number of test nodes to be started, random IP range and other configuration items.
[0153] 6.2 Use step 1 to build the microstack thread management node and run it.
[0154] 6.3 Test the number of nodes in the configuration file. Use step 5 to generate the specified number of test nodes and run.
[0155] 6.4 Continue to run and record test results.
[0156] Through the above steps, the present invention completes the basic process of building a high-performance stress test service using microstack processes and raw sockets. After testing, when using Intel i7-10700K @3.8GHz / 64G memory, the specified number of test nodes in the present invention can exceed 2,000,000 and run stably. However, when using conventional stress testing tools such as Apache JMeter to initiate more than 40,000 test nodes in this environment, the system occupancy has reached the limit.
[0157] In traditional stress testing solutions such as Apache JMeter [stress testing software], a single physical machine can initiate any number of connections exceeding one million, which can maximize the performance of the test machine. Under the same machine performance, the present invention can support a million-level stress test on a single machine, greatly saving the money and time cost of stress testing.
[0158] Traditional stress testing thread switching requires deep kernel state, such as Figure 4 As shown in the figure, the traditional stress test tool uses each link to initiate a thread. When each thread occupies and gives up the CPU, there are context switches and operating system resources for kernel state and user state switching. Especially when tens of thousands of threads occupy the CPU at the same time, a large amount of system resources are consumed on context switching, which greatly wastes limited system resources. The micro-stack thread used in the present invention does not need to go deep into the kernel state, such as Figure 5 As shown, the present invention adopts micro-stack threads in user mode and does not need to go deep into kernel mode, which can efficiently utilize the system resources of the test machine, reduce the cost of stress testing, and simplify the operating steps of high-performance stress testing.
[0159] The original socket of the present invention can be replaced by a virtual machine solution or a virtual network card, and can also break through the limit of 65536 number of single-machine TCP / IP protocol stack, but it needs to configure a virtual machine or a virtual network card. However, multiple virtual machines also need to distribute data synchronously, which is complicated to implement.
[0160] It should be emphasized that this description is only a specific solution for a certain platform. Other platforms may need to be modified, but the ideas are the same and the inspiration should also be protected.
[0161] A computer device provided by an embodiment of the present invention includes a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor. When the device is running, the processor communicates with the memory through the bus, and the processor executes the machine-readable instructions to perform any step of the above-mentioned stress testing method based on raw sockets and user-mode microstack threads.
[0162] Specifically, the above-mentioned memory and processor can be general-purpose memory and processor, which are not specifically limited here. When the processor runs the computer program stored in the memory, it can execute the above-mentioned stress testing method based on raw sockets and user-mode microstack threads.
[0163] Corresponding to the method for starting the above-mentioned application, an embodiment of the present invention further provides a storage medium on which a computer program is stored. When the computer program is run by a processor, the steps of any of the above-mentioned stress testing methods based on raw sockets and user-mode microstack threads are executed.
[0164] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, ordinary technicians in the relevant field should understand that the specific implementation methods of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A stress testing method based on raw sockets and user-mode microstack threads, characterized in that: The steps include: Step S1, obtaining the pressure test parameters of the target pressure test server, wherein the pressure test parameters include the target pressure test server address, the target port number, the number of start test nodes and the random IP range; Step S2, constructing a user-state micro-stack thread framework, initiating a request for each connection, wherein the micro-stack thread is a user-state micro-stack thread; Step S3, setting the micro stack thread to initiate connection and response logic nodes, and generating a specified number of test nodes; Step S4, sending a stress test request to each test node according to the stress test parameters, and performing stress testing on each test node of the target stress test server; Step S5, collecting the stress test results of each test node and determining the stress test result of the target stress test server; The step S3 comprises the following steps: Step S31, constructing an original socket and initiating a connection, using the socket function to create an original socket; Step S32, defining the original socket sending function, receiving the original socket handle and the data to be sent; Step S33, define a heartbeat sending function, send heartbeat data to the socket handle, and use the yield function to give up the time slice. When the time slice returns, check whether it has waited for more than 60 seconds. If not, continue to give up the time slice. If so, repeat sending the heartbeat data packet and finally return the CPU occupancy. Step S34, initiating a connection and response logical node, generating a specified number of test nodes; The step S32 comprises the following steps: Apply for a memory buffer from the system, and assign the data to be sent to the specified position in the memory buffer according to the size of the IP header and TCP header as the offset; Construct the IP header and TCP header of the original socket, point to the memory buffer respectively and calculate the checksum, the structures of the IP header and TCP header are iphdr and tcphdr respectively, and the field saddr of the IP header is the random source address IP; Construct a pseudo header, perform TCP checksum calculation, and fill the checksum value into the check field of the TCP header. The source address of the pseudo header is the random source address IP of the IP header, and the structure of the pseudo header is pseudo_header; Use the C language library function send_to to send memory buffer data; The step S34 comprises the following steps: Create a raw socket handle and initiate a connection to the target service; Build a registration data package and send it to the target stress test server; Use microstack threads to build and run the heartbeat sending node to generate a test node; Construct an infinite loop on the test node to listen to the original socket handle and wait for server messages. Use the yeild function to give up the CPU usage and wait until the next resumption of operation to check whether the server data is received. If the server data is received, parse the server data and return the test node IP address and port and the target stress test server IP address and port to the server. Otherwise, give up the CPU usage again.
2. The stress testing method based on raw sockets and user-mode microstack threads according to claim 1, characterized in that: The construction of the user-mode micro-stack thread framework includes the following steps: Step S21, define a microstack thread structure, and define a microstack thread list variable and a scheduler context variable; the microstack thread structure includes a thread context, a microfunction pointer, a parameter list, a stack pointer and a microstack thread state, and the microstack thread state includes "ready", "running", "waiting" and "termination"; the microstack thread list variable is used to save all microstack thread state information; the scheduler context variable is used to save the scheduler context; Step S22, create a micro stack thread structure, and write the micro function pointer and parameter list into the structure; Step S23, traverse the microstack thread list in sequence, find the running microstack thread, mark it as ready, and save its running context; Step S24, define a yield function, save the context of the running microstack thread, mark its state as ready, and switch to the context of the scheduler; Step S25, defining a resume function, finding a specified microstack thread in the microstack thread list, marking it as running, switching to the context of the microstack thread and running it; Step S26, define a schedule function, poll the status of all microstack threads in the microstack thread list, and resume their operation if their status is ready.
3. The stress testing method based on raw sockets and user-mode microstack threads according to claim 2, characterized in that: The step S22 comprises the following steps: Define the create_thread function and apply to the system for at least 64KB of stack memory to write into the structure; Use the C language library function setjmp to save the current program context. If the save is successful, set the current microstack thread state to the ready state and register the structure to the microstack thread list. The return value of the library function setjmp is used to determine whether the context is restored by the C language standard library longjmp. If not, the current function is terminated and returned to the caller. If so, the CPU registers are manually operated using the inline assembly method. By assigning the tail end of the stack pointer address in the microstack thread structure to the rsp register; Mark the currently running microstack thread as running; use the function pointer in the microstack thread structure to pass in the specified parameters to run the specified function. When the function is executed, mark the microstack thread as terminated, and then use the C language standard library longjmp to switch back to the scheduler to return the CPU time slice.
4. The stress testing method based on raw sockets and user-mode microstack threads according to claim 3 is characterized in that: The step S23 comprises the following steps: Define the switch_thread function, traverse the microstack thread list in turn, find the running microstack thread x, mark it as ready, and use the library function setjmp to save its running context; Take the next microstack thread y of microstack thread x, set the running state of microstack thread y to running state, use the C language standard library longjmp to restore the context of microstack thread y and run.
5. A stress testing device based on raw sockets and user-mode microstack threads, characterized in that: include: A data acquisition module is used to obtain the pressure test parameters of the target pressure test server, wherein the pressure test parameters include the target pressure test server address, the target port number, the number of start test nodes and the random IP range; The connection request module is used to build a user-mode micro-stack thread framework and initiate a request for each connection; The test node generation module is used to set the micro stack thread to initiate the connection and respond to the logical node, and generate a specified number of test nodes; The stress test module is used to send stress test requests to each test node according to the stress test parameters, and perform stress tests on each test node of the target stress test server; The test result acquisition module is used to collect the stress test results of each test node and determine the stress test results of the target stress test server; The specific process of the test node generation module setting the micro stack thread to initiate connection and respond to logical nodes and generate a specified number of test nodes includes the following steps: Step S31, constructing an original socket and initiating a connection, using the socket function to create an original socket; Step S32, defining the original socket sending function, receiving the original socket handle and the data to be sent; Step S33, define a heartbeat sending function, send heartbeat data to the socket handle, and use the yield function to give up the time slice. When the time slice returns, check whether it has waited for more than 60 seconds. If not, continue to give up the time slice. If so, repeat sending the heartbeat data packet and finally return the CPU occupancy. Step S34, initiating a connection and response logical node, generating a specified number of test nodes; The step S32 comprises the following steps: Apply for a memory buffer from the system, and assign the data to be sent to the specified position in the memory buffer according to the size of the IP header and TCP header as the offset; Construct the IP header and TCP header of the original socket, point to the memory buffer respectively and calculate the checksum, the structures of the IP header and TCP header are iphdr and tcphdr respectively, and the field saddr of the IP header is the random source address IP; Construct a pseudo header, perform TCP checksum calculation, and fill the checksum value into the check field of the TCP header. The source address of the pseudo header is the random source address IP of the IP header, and the structure of the pseudo header is pseudo_header; Use the C language library function send_to to send memory buffer data; The step S34 comprises the following steps: Create a raw socket handle and initiate a connection to the target service; Build a registration data package and send it to the target stress test server; Use microstack threads to build and run the heartbeat sending node to generate a test node; Construct an infinite loop on the test node to listen to the original socket handle and wait for server messages. Use the yeild function to give up the CPU usage and wait until the next resumption of operation to check whether the server data is received. If the server data is received, parse the server data and return the test node IP address and port and the target stress test server IP address and port to the server. Otherwise, give up the CPU usage again.
6. A computer device, characterized in that: It includes a processor, a memory and a bus, the memory stores machine-readable instructions executable by the processor, when the computer device is running, the processor and the memory communicate through the bus, and the processor executes the machine-readable instructions to perform the steps of the stress testing method based on raw sockets and user-mode microstack threads as described in any one of claims 1-4.
7. A storage medium, characterized in that: The storage medium stores a computer program, which, when executed by a processor, executes the steps of the stress testing method based on raw sockets and user-mode microstack threads as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Flow pressure testing method and device
CN109726102A
Pressure testing method and device
CN109857626A
