My App
React

Performance Testing — Load Testing, Cache Analysis, and Response Optimization

Learn how to test app performance using Locust, analyze cache hit rates, and optimize response times with simple examples and real-world cases.

⚡ Performance Testing

Ensure your app stays fast and stable under heavy usage.


🧠 What is Performance Testing?

Performance testing checks how your application behaves when many users use it at the same time.

It helps identify:

  • Slow APIs
  • Inefficient caching
  • Bottlenecks in database queries

In this guide, we’ll explore:

  • Load testing using Locust
  • Cache hit rate analysis
  • Response time optimization

Each with simple examples and real-world parallels.


🧪 1. Load Testing with Locust

Locust is an open-source Python tool for load testing your web apps.
It simulates many users making requests at once — helping you find weak spots.


🔸 Install Locust

pip install locust

🔹 Create a Locust Test (locustfile.py)

from locust import HttpUser, task, between

class WebsiteUser(HttpUser):
    wait_time = between(1, 3)

    @task
    def get_home(self):
        self.client.get("/")

    @task
    def get_tasks(self):
        self.client.get("/tasks")

🔹 Run Locust

locust -f locustfile.py --host=http://127.0.0.1:8000

Then open your browser at http://localhost:8089 and start the test by entering:

  • Number of users (e.g., 100)
  • Spawn rate (e.g., 10 users/second)

You’ll see graphs showing:

  • Requests per second (RPS)
  • Average response time
  • Failed requests

💡 Real-World Example: E-commerce sites like Flipkart and Amazon run load tests before major sales. They simulate thousands of users adding items to carts to make sure servers don’t crash during peak traffic.


📊 2. Cache Hit Rate Analysis

Caching improves performance — but only if the cache is being used effectively. A cache hit means data came from the cache, while a cache miss means it had to fetch from the database.

A good cache hit rate shows that your app is efficiently reusing data.


🔸 Check Redis Cache Statistics

Run this command:

redis-cli info stats

You’ll see:

keyspace_hits:1200
keyspace_misses:300

🔹 Calculate Cache Hit Rate

hits = 1200
misses = 300
hit_rate = hits / (hits + misses)
print(f"Cache Hit Rate: {hit_rate * 100:.2f}%")

Output:

Cache Hit Rate: 80.00%

💡 Real-World Example: Netflix maintains cache hit rates above 90% using Redis and CDN edge caching. This ensures that most video content loads from cache, not from the main servers — saving huge bandwidth.


🔹 How to Improve Cache Hit Rate

  1. Cache frequently accessed data (user sessions, product details).
  2. Increase cache size if possible.
  3. Use consistent cache keys (avoid typos or mismatched formats).
  4. Apply time-to-live (TTL) strategically — not too short, not too long.

⚙️ 3. Response Time Optimization

Response time measures how fast your app replies to requests. It directly affects user satisfaction — every 100ms delay can cause drop-offs in conversion rates.


🔸 Measure Response Time (FastAPI Example)

from fastapi import FastAPI, Request
import time

app = FastAPI()

@app.middleware("http")
async def measure_time(request: Request, call_next):
    start = time.time()
    response = await call_next(request)
    duration = time.time() - start
    print(f"Response time: {duration:.4f} seconds")
    return response

This logs how long each request takes.


💡 Real-World Example: Google found that increasing page load time by 0.5 seconds reduced traffic by 20%. That’s why companies continuously monitor API response times using tools like Datadog and Prometheus.


🔹 Tips to Reduce Response Time

MethodDescription
Use CachingAvoid repeated DB queries
Optimize QueriesUse indexes and select only needed columns
Minimize PayloadsReturn only required data in responses
Use Async APIsHandle multiple requests concurrently
Compress ResponsesEnable gzip or Brotli compression

🔹 Example: Enable Gzip Compression in FastAPI

from fastapi import FastAPI
from fastapi.middleware.gzip import GZipMiddleware

app = FastAPI()
app.add_middleware(GZipMiddleware, minimum_size=1000)

Now large responses are automatically compressed, reducing network time.


💡 Real-World Example: Twitter compresses timeline data before sending it to mobile devices — this helps users on slow networks get updates faster.


🧾 Summary

ConceptWhat It DoesReal-World Example
Load TestingSimulates users to test app limitsFlipkart’s Diwali Sale traffic test
Cache Hit RateChecks cache efficiencyNetflix maintaining 90% cache hit
Response Time OptimizationSpeeds up API repliesGoogle improving search latency

💡 Final Thought

Performance testing is not about breaking the app — it’s about understanding its limits. With tools like Locust, Redis, and FastAPI metrics, you can measure, improve, and maintain a smooth experience for every user — even at scale.