DEV Community

Cover image for Load Testing using FastAPI and Postman: A Comprehensive Guide
Kairus Noah Tecson
Kairus Noah Tecson

Posted on

Load Testing using FastAPI and Postman: A Comprehensive Guide

Imagine this scenario wherein your startup app starts to have traction. Users count suddenly goes up! And now continuously grows from 10 to 100 users. All those users constantly do things in your app, clicking some buttons, using some features, making request from your backend server...

What you would not want in this kind of situation is unpreparedness. You want to ensure that your application is Reliable and Available to the users. This is where Testing comes into place and what we would be talking about here is a specific type of testing that is suitable for testing this kind of scenarios, Load testing.

In this guide, we’ll focus on using FastAPI and its automated OpenAPI specification generation to streamline the process of generating Postman collections for load testing. By the end, you’ll know how to utilize FastAPI’s OpenAPI specification and Postman to test your application at scale.


🤔 Why Use FastAPI and Postman for Load Testing?

FastAPI comes with built-in OpenAPI support, making it easy to document and test your APIs. By combining FastAPI with Postman, you can:

  • Automatically generate Postman collections from your FastAPI API’s OpenAPI documentation.
  • Simplify request setup by importing the collection directly into Postman.
  • Leverage Postman’s powerful testing and scripting features for dynamic data generation.
  • Scale tests programmatically with Newman, Postman’s CLI tool, for CI/CD integration.

This synergy between FastAPI and Postman allows developers to quickly simulate real-world traffic scenarios and identify bottlenecks in their applications.


🛠️ Step 1: Setting Up FastAPI and Swagger

1.1 Start Your FastAPI Application

Ensure your FastAPI application is running locally or on a server. For example:

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
Enter fullscreen mode Exit fullscreen mode

When the server starts, OpenAPI JSON endpoint will be available at http://127.0.0.1:8000/openapi.json.

1.2 Verify OpenAPI JSON

Open your browser and navigate to http://127.0.0.1:8000/openapi.json to ensure the OpenAPI JSON is accessible.


🚌 Step 2: Exporting the OpenAPI JSON to Postman

2.1 Copy the OpenAPI endpoint

You can either save the OpenAPI JSON file locally by using your browser or by curl:

curl -o openapi.json http://127.0.0.1:8000/openapi.json
Enter fullscreen mode Exit fullscreen mode

Or just by copying the OpenAPI endpoint URL, http://127.0.0.1:8000/openapi.json.

2.2 Import OpenAPI JSON into Postman

  1. Open Postman and click Import in the top-left corner.
  2. Select the downloaded openapi.json file.
  3. Postman will automatically generate a collection with all the endpoints defined in the OpenAPI specification.

If you just copied the endpoint URL, you can just paste the URL at the input bar at the top of the modal that appears when you click the Import

Import modal when you clicked on Import

2.3 Organize and Test Your Collection

Review the imported collection to ensure all endpoints are correctly configured. You can also add environment variables or scripts as needed for authentication or data management.


📦 Step 3: Preparing for Load Testing in Postman

3.1 Add Dynamic Data to Your Requests

To simulate real-world scenarios, modify your requests to include dynamic data. For example, use Postman’s built-in variables or pre-request scripts:

Example Pre-request Script:

pm.variables.set("random_id", Math.floor(Math.random() * 10000));
Enter fullscreen mode Exit fullscreen mode

Example Payload:

{
    "item_id": "{{random_id}}",
    "q": "sample_query"
}
Enter fullscreen mode Exit fullscreen mode

You can also just make use of built-in scripts like $randomInt to generate random values.
Example use case of using built-in scripts:

{
  "email": "user_{{$randomInt}}@gmail.com",
  "password": "DevPassword123",
  "full_name": "Testing 1_{{$randomInt}}",
  "company_name": "Testing Company"
}
Enter fullscreen mode Exit fullscreen mode

This built-in script will return random values between 0-1000 in every request made.

3.2 Configure Variables in you Collection

Use collection variables in Postman to manage API base URLs, authentication tokens, or dynamic parameters without your Collection. This simplifies updates and testing across your Collection.

Collection variable page in Postman


🧪 Step 4: Running Performance Tests with Postman's Collection Runner

Postman now includes built-in performance testing capabilities that allow you to simulate user traffic and assess your API's performance.

4.1 Launch the Collection Runner

  1. Click on the Runner button (beaker icon) in Postman.
  2. Select the collection imported from your FastAPI OpenAPI JSON.4.2 Set Test Parameters

4.2 Configure Performance Test Settings

  • Virtual Users: Specify the number of virtual users to simulate concurrent load.
  • Test Duration: Set the duration for which the test should run.
  • Load Profile: Choose between fixed, ramp-up, spike, or peak load profiles to simulate different traffic patterns.4.3 Execute the Load Test

4.3 Execute the Performance Test

Click Run to start the performance test. Postman will display real-time performance metrics such as average response time, error rate, and throughput.


📈 Step 5: Analyzing Test Results

After the test completes, analyze the results to identify performance bottlenecks:

  • Response Times: Check if the response times meet your application's performance criteria.
  • Error Rates: Identify any errors that occurred during the test and investigate their causes.
  • Throughput: Assess the number of requests handled per second to ensure it aligns with expected load.

Postman provides detailed metrics and allows you to compare multiple test runs to track performance changes over time.


🚀 Best Practices for Load Testing with FastAPI and Postman

  1. Keep OpenAPI Documentation Updated: Ensure your FastAPI documentation reflects the current state of your API for accurate testing.
  2. Use Dynamic Data: Incorporate variability in test data to simulate diverse real-world scenarios.
  3. Monitor System Resources: Use monitoring tools to observe CPU, memory, and network usage during tests.
  4. Automate Tests: Integrate performance tests into your CI/CD pipeline for continuous assessment.
  5. Iterate Based on Findings: Regularly update your tests and application based on performance test outcomes.

📌 Conclusion

By leveraging FastAPI's OpenAPI specification and Postman's performance testing features, you can effectively simulate user traffic and identify potential performance issues. This approach enables you to ensure your FastAPI application remains robust and responsive under varying load conditions.

Happy testing!

Top comments (0)