Use Python to simulate thousands of horses to do performance automation testing | Live broadcast benefits are included at the end of the article
"IT Talk" is an IT professional information and service platform under the Machinery Industry Press. It is committed to helping readers master more professional and practical knowledge and skills in the broad IT field, and quickly enhance their workplace competitiveness. Click on the blue WeChat name to quickly follow us.
Performance automation testing is an important part of testing. Performance testing includes client-side performance testing and server-side performance testing. However, performance testing, stress testing, etc. usually refer to server-side performance testing.
At present, most applications are based on the HTTP protocol, so the introduction of performance automation testing in this chapter mainly focuses on the HTTP protocol. Of course, based on the performance automation testing framework, combined with various Python packages, it can be extended to performance automation testing of any protocol.
The mainstream performance testing tools include LoadRunner based on C-like language, Jmeter based on Java language, and the niche ones are Vegeta based on GO and wrk based on lua language. This article is mainly based on Python, through the simulation of threads, processes, and coroutines to systematically introduce how to use the Python language for performance automation testing. In addition, this article mainly focuses on how to use Python to perform performance automation testing, and some concepts in performance testing will not be expanded too much, so if you encounter some unclear concepts, please refer to the explanation of the search engine.
The Basics of Simulating Multiple Users in Python
In Python, you can simulate multi-user operations in performance testing through multi-threading, multi-process and coroutines. This section will combine the basic knowledge of this piece and integrate it with the HTTP protocol to simulate multi-user operations, and call related Built-in functions to perform preliminary result analysis.
Example 1: How to use multithreading for performance testing
Multithreading is an inevitable application technology in software development, and it is also the cornerstone of simulating multi-user technology in performance testing. Multithreading has the ability to complete multiple tasks synchronously, thereby improving the efficiency of resource use, so that more users can be simulated under the same resources, thereby reducing the required resources.
However, it should be noted that the more threads, the more the operating system needs to switch the context, and the more resources are occupied, so the thread lines that can be simulated by a single process are limited, and the resource usage of the test machine needs to be observed when doing performance testing. Happening.
Please refer to how to use multi-threading technology to implement performance automation testing in Python (as shown in the code example).
Explanation: In this example, the threading module is called to create the MyThread class, and the running time of each thread is collected in the run method. After the operation, use the max, min, get_median, and get_avg functions to calculate the maximum, minimum, median, and average values of the obtained time, and then print the obtained results. It is most appropriate to evaluate the performance of the system from the user's point of view, because for the user, there is only one feeling when using the software, that is, whether it is good or not, and whether it is fast enough, and the performance index felt by the user is mainly the maximum response time. , median, minimum response time, average response time, which are basic applications in statistics.
Example 2: How to use thread pools for performance testing
When calling multi-threading, you need to instantiate MyThread, x.start, and then x.json. This method is a bit troublesome, and the structure of the MyThread class is not elegant. The better way to solve this problem is to use Python's thread pool.
The thread pool is a technology for creating threads in advance. Before the task arrives, the thread pool creates a certain number of threads and puts them into the idle queue. These threads are all in the sleep state, but they are all started and not executed. It consumes CPU resources, but will occupy a small memory space. When the request comes, the buffer pool will allocate an idle thread for this request, and pass the request into this thread for processing. When the pre-created threads are all running, that is, when there are not enough pre-made threads, the thread pool is free to create a certain number of new threads to process more requests. When the system is idle, it also removes some threads that are in a deactivated state.
There are a few things to keep in mind when using thread pools:
- thread pool size
For multi-threaded applications, the more threads the better, the size of the thread pool needs to be determined according to the software and hardware environment in which the system runs and the characteristics of the application itself. Generally speaking, if the code structure is reasonable, the number of threads and the number of CPUs can be appropriate. If the thread is blocked when running, the size of the pool can be increased accordingly; if necessary, an adaptive algorithm can be used to dynamically adjust the size of the thread pool to improve the effective utilization of the CPU and the overall performance of the system.
- Concurrency error
Multithreaded applications should pay special attention to concurrency errors, logically ensure the correctness of the program, and pay attention to avoid deadlocks.
- Thread leak.
This is a serious problem in thread pool applications. Thread leaks occur when the task is completed and the thread fails to return to the pool.
Starting from Python 3.2, the Ptyhon standard library provides the concurrent.futures module, which provides two classes, ThreadPoolExecutor and ProcessPoolExecutor, which easily realize the processing of the thread pool and process pool of the threading and multiprocessing modules. For details, please refer to the following examples:
Description: By using the ThreadPoolExecutor in the concurrent.futures module, 10 threads are initialized, and then you only need to call the executor.submit method to start the execution of the multi-threaded function, and use as_completed(task1) to obtain the execution result of the thread pool. It should be noted that 10 For each thread, whoever responds first will be added to the result list first, and finally call the max, get_media, min, and get_avg methods again to obtain performance results. From the perspective of the process of using the thread pool, both in terms of code size and code structure, it is more concise and efficient than calling the threading module.
Example video
For more exciting content, you can pay attention to the following live courses
Free live sharing
This Thursday, Wen Qingshan, R&D director of Zhigu Group, senior software testing project manager, and automated testing framework expert, will bring you a live broadcast**"Programmers' Core Skills Improvement" Series of Live Lectures: Advanced Practice in Python Test Development**
Live topic: "Programmers' Core Skills Improvement" Series Live Lectures: Advanced Practice in Python Test Development
Opening Hours: October 13, 2022 (Thursday) 20:00
Lecturer: Wen Qingshan
Course Type: Live Video
Watching platforms: Kyushu Cloud Broadcasting Platform, Dangdang Live Broadcasting Platform, Jingdong Live Broadcasting Platform, Machinery Industry Publishing House "IT has a good chat" video account
Instructors
Wen Qingshan
Zhigu Group R&D director, senior software testing project manager, and automated testing framework expert.
He has been engaged in the testing and development of IOT, artificial intelligence and other fields for more than 10 years. He has rich experience in team project management and has led the CMMI3 and IOS9001 quality system certification. He has been involved in technical research such as python development and AI test platform development. He is good at establishing standards for business testing, test development, and performance automation lines for enterprises. He has been responsible for the quality assurance of tens of millions to hundreds of millions of monthly active projects, and can write multi-terminal codes. Good at cross-domain combat.
Related Books
Click on the cover to see details
▊ "Python Software Testing Practical Collection "
Written by Wen Qingshan
- The system covers 5 major software testing technical topics
- In-depth analysis of the theory, methods, tools and management methods of software testing
- Accurately locate software testing pain points
This book starts with the basics of Python, and systematically explains the use of Python language for interface automation testing, performance automation testing, security testing, and functional automation regression testing.
The whole book proceeds from the shallower to the deeper, systematically expounding the problems and solutions that the author has encountered in the test development work for many years. The rich example codes in the book can be used directly in various automated test scenarios of the software.
The author created this book in response to the needs of enterprises for test development positions in recent years, hoping that the publication of this book will enable more readers to better master the skills of Python test development and find more ideal software testing jobs.
This book comes with source code files of all test examples and a video of the core knowledge points of Python software testing. This book is suitable for technical personnel who are engaged in software testing and professionals who want to be engaged in software testing. It is also suitable for students and teachers in computer, software engineering, automation and other related majors.
Live content
Live entry
Kyushu Cloud Broadcasting Platform Live Room:
We will release the entrances of other live broadcast platforms before the live broadcast.
END
Written by: Zhang Lu
Responsible editor: Li Xinxin
Reviewer: Shi Jing
Latest Programming News and Information | GeekBar