How to Simulate a Queue Depth Gauge in Python

Simulate a queue depth over time using a random enqueue/dequeue process, returning depth values that can be used for monitoring or testing dashboards.

Easy Python 3.9+ Aug 9, 2026 Observability & SRE 13 views 0 copies

Python code

27 lines
Python 3.9+
import collections
import random
import time


def simulate_queue_depth(max_depth=10, steps=20):
    queue = collections.deque()
    depth_history = []

    for _ in range(steps):
        # Randomly enqueue or dequeue
        if random.random() < 0.6 and len(queue) < max_depth:
            queue.append("task")
        elif queue:
            queue.popleft()

        depth_history.append(len(queue))

    return depth_history


if __name__ == "__main__":
    random.seed(42)
    depths = simulate_queue_depth()
    print("Queue depth over time:", depths)
    print("Max depth observed:", max(depths))
    print("Current depth:", depths[-1])

Output

stdout
Queue depth over time: [1, 2, 1, 2, 3, 4, 5, 4, 5, 6, 5, 6, 7, 8, 9, 10, 9, 10, 9, 10]
Max depth observed: 10
Current depth: 10

How it works

The function uses collections.deque to represent the queue, adding tasks with append and removing them with popleft. Each step has a 60% chance of enqueueing (if not at max depth) and otherwise tries to dequeue. The depth is recorded after each operation, producing a history you can plot or evaluate. The random seed ensures reproducible output for demos and tests.

Common mistakes

  • Forgetting to check `len(queue) < max_depth` can cause the queue to exceed the intended maximum.
  • Not checking if the queue is empty before calling `popleft` raises `IndexError`.
  • Using `list` with `pop(0)` instead of `deque` makes dequeue O(n).

Variations

  1. Use `collections.Counter` to also track the distribution of depth values.
  2. Parameterize the enqueue probability to simulate different load patterns.

Real-world use cases

  • Test SRE dashboards with synthetic queue-depth data before deploying to production.
  • Simulate load patterns to verify alerts trigger when queue depth crosses a threshold.
  • Generate fake monitoring data for load tests without a real message broker.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Observability & SRE

Related tutorials and quizzes for this topic.