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.
Python code
27 linesimport 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
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
- Use `collections.Counter` to also track the distribution of depth values.
- 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
More from Observability & SRE
- Adding a Correlation ID to Log Context in Python medium
- Calculate Error Rate from Log Stream in Python easy
- Check if a Timestamp Falls in a Daily Maintenance Window in Python easy
- Export Metrics with OTLP Mock in Python medium
- Generate Mock CPU and Memory Metrics in Python easy
- Generate Prometheus Text Exposition Format in Python easy
Keep learning
Related tutorials and quizzes for this topic.