- Lab
- Core Tech
![Labs Labs](/etc.clientlibs/ps/clientlibs/clientlib-site/resources/images/labs/lab-icon.png)
Guided: Performance Testing
In this Guided Code Lab, you'll explore performance testing to gain practical skills in evaluating how a web application performs under different loads. You'll start by setting up Locust to simulate user interactions, helping you understand real-world traffic and high-stress scenarios. You'll assign user behaviors, set realistic wait times, and create reports in HTML and CSV formats. Then, using pandas, you'll analyze test data to uncover insights that are key to optimizing your application's reliability and speed. By the end, you'll feel confident in your ability to assess and improve web performance, equipping you with skills that boost efficiency and user satisfaction.
![Labs Labs](/etc.clientlibs/ps/clientlibs/clientlib-site/resources/images/labs/lab-icon.png)
Path Info
Table of Contents
-
Challenge
Introduction
In this lab, your goal is to set up, configure, and run performance tests for a web application using Locust. This hands-on experience will improve your skills in simulating real-world traffic, identifying bottlenecks, and optimizing your application's performance.
info> The testing environment is pre-configured using Python, allowing you to focus on writing and executing performance tests without worrying about complex setup tasks. This setup ensures you can focus how to create, execute, and analyze load tests.
Lab Overview:
-
Setting Up the Performance Testing Environment: Start by setting up your testing environment, including Locust. This step ensures you have everything in place to begin performance testing quickly and efficiently.
-
Configuring Test Settings: Next, create a test script in Locust that defines how simulated users will interact with your web application. You'll configure settings like the number of users, test duration, and the actions those users will perform.
-
Creating Basic Performance Tests: In this step, write your first performance test. You'll define user behavior, such as accessing key pages, and simulate real-world usage patterns. This will provide an initial understanding of your application's performance under regular conditions.
-
Executing Load and Stress Tests: Once your test script is ready, execute it and simulate multiple users accessing your application. You'll measure response times, server performance, and identify any bottlenecks that may occur under normal load conditions.
-
Stress Testing: After load testing, push your application further by conducting stress tests. This involves significantly increasing the number of users to see how the system handles extreme traffic, helping you identify the breaking points of your application.
-
Analyzing Test Results: Finally, review and analyze the performance reports generated by Locust. You'll use these reports to calculate success rates, and gather insights that will help you optimize your application for better performance.
info> Stuck or need a hint? If you need assistance or want to verify your approach, feel free to refer to the provided
solution
directory or the Locust documentation. These resources will help you stay on track and maximize your learning experience.Once you've completed the lab, review your test results to see how your application performs under different conditions and explore ways to improve its performance.
To get started, click on the Next step arrow!
-
-
Challenge
Setting up a Performance Testing Environment
In this step, you'll set up everything you need to start performance testing with Locust. You'll walk through setting up Locust and verifying that it was installed successfully. By the end, you'll be ready to simulate real users and see how your web application performs under load.
info> Note: Before you can start testing, you need to set up the right tools. Locust will allow you to simulate users visiting your site, helping you understand how your application behaves when it's handling multiple users at once. ### Set up Locust:
Objective: Install Locust, the tool you'll use to simulate users and measure web application performance.
Why It Matters: Locust is the core tool you'll use to simulate real-world traffic to your web application. Installing it is crucial because it will allow you to create and run performance tests. When installing Locust, use the following flags:
--no-index
: This tells pip not to look for packages online, since it has already been downloaded.--find-links ./locust_packages_complete
: This tells pip where to find the downloaded packages../
indicates that the file is in the same directory you are currently in. ### Confirm Locust Installation:
Objective: Check that Locust was installed correctly by verifying its version.
Why It Matters: Confirming the installation helps ensure that Locust is set up and ready to run performance tests. This quick check avoids issues down the road when you start executing tests. If Locust is installed correctly, you should see the version number printed out
Locust 2.32.0
. success> Important: When setting up Locust, follow the steps carefully during installation. Any issues here could prevent Locust from running tests properly. After installation, it's a good idea to check the Locust version to catch any setup issues early. This quick check saves you time later and helps avoid problems when simulating real user traffic in your performance tests. -
Challenge
Configure Test Settings
In this step, you'll complete and configure your test settings for Locust. You will set up the
locustfile.py
script, assign user tasks, set wait times between actions, and generate test reports. By the end of this step, you'll be ready to simulate users visiting your web application and track their interactions.info> Note: Correct configuration of test settings is essential for simulating realistic user behavior and obtaining accurate performance test results. ### Simulate User Behavior:
Objective: Define user behavior by configuring tasks that simulate how users interact with your web application.
Why It Matters: By simulating real-world user actions like visiting the homepage, you ensure that your Locust tests accurately reflect how users interact with your site. This is crucial for identifying performance bottlenecks during user interactions. ### Assign User Tasks and Set Wait Times:
Objective: Assign tasks to users and define how long they should wait between actions to create a realistic test.
Why It Matters: Assigning tasks and setting realistic wait times helps mimic actual user behavior, making your performance test more accurate. By simulating the time users take between different actions, your tests can better identify performance issues under real-world conditions. Takeaways:
- By linking
tasks = [UserBehavior]
, you're telling Locust to execute the actions you've defined in theUserBehavior
class. These could be tasks like visiting pages or simulating specific actions on the website. This setup is essential to control the behavior of simulated users. - Setting the wait time simulates more realistic user interactions. In the real world, users take time between clicking on different pages or interacting with different parts of your site. By assigning a wait time, you create a more accurate performance test that reflects actual user behavior.
Generate HTML and CSV Reports
Objective: Generate reports summarizing your test results, including performance metrics such as response times and failure rates.
Why It Matters: Generating reports in HTML and CSV formats allows you to analyze test results and identify performance issues like slow response times or high failure rates. This is crucial for improving the overall user experience. While running your Locust test, use the following flags:
-f locustfile.py
: Specifies the script file you are running--headless
: Runs the test without launching the web UI-u 100
: Simulates 100 users-r 10
: Simulate 10 users per second--run-time 1m
: Runs the test for 1 minute--html report.html
: Saves the test results in an HTML report--csv report
: Saves the test results in CSV format success> Important: When you're setting up your Locust test configuration, take your time with how tasks are assigned and the wait time between actions. These small settings really matter! They help make your tests feel more like how real users interact with your site. If something is off, like an unrealistic wait time or a missing task, it can throw off your test results.
- By linking
-
Challenge
Creating Basic Performance Tests
In this step, you'll define how your users interact with your web application by creating basic performance tests. You'll simulate real-world user behavior and assign weights to different tasks based on how frequently users visit various pages on your website.
info> Note: Defining realistic user interactions is key to getting meaningful insights from your performance tests. By assigning tasks and weights, you can simulate user behavior in a more accurate way, reflecting actual site usage. ### Simulate Basic User Interactions:
Objective: Simulate basic user behavior on your website by setting tasks and wait times between actions.
Why It Matters: Simulating how users behave on your site allows you to identify performance issues as users interact with different pages. By mimicking user actions and setting wait times, you create more accurate performance tests, helping you improve your site's performance under real-world conditions. ### Define Multiple Tasks for User Behavior:
Objective: Simulate users visiting different pages on your site and assign weights to tasks to reflect how frequently these pages are visited.
Why It Matters: Not all pages on your site get the same amount of traffic. By assigning different weights to tasks, you can simulate more realistic behavior, ensuring that the most popular pages get tested more frequently than others. success> Important: When assigning weights to tasks, think about how your users actually interact with your website. For example, if the homepage is the first stop for most visitors, make sure it has a higher weight. Similarly, pages like the Contact page might be visited less frequently, so assigning a lower weight gives you a more realistic simulation.
-
Challenge
Executing Load and Stress Tests
In this step, you'll simulate varying traffic levels to observe how your web application handles load over time. By the end of this step, you'll have valuable insights into how well your application performs under both everyday use and extreme traffic.
info> Note: Testing under different traffic conditions allows you to understand your web application's strengths and potential bottlenecks. ### Load Tests Under Normal Conditions:
Objective: Simulate normal traffic conditions by gradually increasing the number of users interacting with your application.
Why It Matters: Running load tests under normal conditions helps you see how your web application performs with typical user traffic. This is essential for ensuring that your site can handle day-to-day use efficiently. While running your Locust test, use the following flags:
-f user_behavior_locustfile.py
: Specifies the file containing your test script--headless
: Runs the test without launching the Locust web interface-u 100
: Simulates 100 users in total-r 10
: Spawns 10 new users per second--run-time 5m
: Runs the test for 5 minutes success> Important: Load testing shows how your app handles everyday traffic, but it’s important to test regularly as your traffic grows. Catching performance issues early helps you ensure your app stays fast and reliable for all users.
-
Challenge
Stress Testing
In this step, you'll push your web application to its limits by running a stress test. Stress testing allows you to see how your application performs when facing more users than usual, giving you valuable insights into potential bottlenecks and breaking points.
info> Note: Stress testing helps you understand how well your application can handle unexpected spikes in traffic and helps identify areas for optimization. ### Push Application Limits:
Objective: Simulate a large number of users to understand how your web application behaves under extreme load.
Why It Matters: Stress tests are crucial for understanding how your web application handles more users than it was designed for. This test helps identify when the application might break under pressure. While running your Locust test, use the following flags:
-f locustfile.py
: Specifies the file containing your test script--headless
: Runs the test without launching the Locust web interface-u 1000
: Simulates 1000 users-r 50
: Simulates 50 new users per second--run-time 10m
: Runs the test for 2 minutes--html stress_report.html
: Generates an HTML report of the stress test results--csv stress_report
: Generates CSV files with detailed stress test data ### Handle 429 Too Many Requests:
Objective: Modify your test script to handle 429 Too Many Requests errors during your performance tests.
Why It Matters: Servers can get overloaded during stress tests, and this can lead to 429 errors. By adding retry logic, your performance test can handle these errors, ensuring that the test continues smoothly and that you gather more accurate performance data. success> Important: Stress testing helps you uncover how your app handles heavy traffic. Don't worry if you see errors or slowdowns; that's valuable info! Each result enables you to optimize your app, ensuring it's ready for real-world spikes in users. Keep refining, and your app will perform even better under pressure!
-
Challenge
Analyzing Test Results
Now that you've run your performance tests, it's time to focus on exploring the results. In this step, you'll be using
pandas
, a powerful Python library, to help analyze the data generated by Locust and uncover valuable insights about your web application's performance.info> Note: Analyzing your test results will help you identify potential bottlenecks and calculate the success rate of your requests. ### Check if Pandas is Installed:
Objective: Verify that
pandas
is installed and ready to use. This is important because you'll rely on it to analyze performance test results from Locust.Why It Matters:
pandas
is a crucial tool for handling and analyzing large datasets, like the ones generated by your Locust tests. Ensuring it's installed will allow you to move forward with analyzing your test data. While completing the task below, don't forget to use the following flags:-c
: This flag tells Python to execute the command provided as a string in the double quotesimport pandas
.
info> Instead of running a Python script from a file, you can execute small Python code snippets directly using this flag. If the command completes with no error messages, then pandas is installed and ready for use in your data analysis tasks. If
pandas
is not installed, you will see anImportError
, meaning you'll need to install it. ### Analyze Test Results Using Pandas:Objective: Use
pandas
to analyze your performance test results and gain insights into which parts of your web application are performing well and where there might be bottlenecks.Why It Matters: By analyzing your Locust test results with
pandas
, you'll be able to pinpoint areas of improvement in your web application. ### Run Python Script to Analyze Results:Objective: Execute your Python script to analyze the performance test data, focusing on identifying slow endpoints and calculating the success rate of your requests.
Why It Matters: Running the analysis will give you a detailed breakdown of how your web application performed during the Locust test. You'll get insights into any slow-loading pages and how well the requests were handled overall. Once the script finishes running, you'll see the output in your Terminal:
- Success Rate: You'll get a clear percentage showing how many requests were successful without errors.
info> Running this analysis will give you a clear picture of where your web application might need improvements, especially when it's under heavy load. Awesome work! You've successfully set up and run your performance tests with Locust and analyzed the results using pandas. Keep up the great work and continue optimizing your apps!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.