Skip to content

Commit 2eaf432

Browse files
committed
tutorial: host script to simulate logs
1 parent 30f3c78 commit 2eaf432

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#!/bin/env python3
3+
4+
import datetime
5+
import math
6+
import random
7+
import sys
8+
import time
9+
10+
11+
12+
requests_per_second = 2
13+
failure_rate = 0.05
14+
get_post_ratio = 0.9
15+
get_average_duration_ms = 500
16+
post_average_duration_ms = 2000
17+
18+
19+
while True:
20+
21+
# Exponential distribution random value of average 1/lines_per_second.
22+
d = random.expovariate(requests_per_second)
23+
time.sleep(d)
24+
if random.random() < failure_rate:
25+
status = "500"
26+
else:
27+
status = "200"
28+
if random.random() < get_post_ratio:
29+
method = "GET"
30+
duration_ms = math.floor(random.expovariate(1/get_average_duration_ms))
31+
else:
32+
method = "POST"
33+
duration_ms = math.floor(random.expovariate(1/post_average_duration_ms))
34+
timestamp = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
35+
print(f"{timestamp} level=info method={method} url=/ status={status} duration={duration_ms}ms")
36+
sys.stdout.flush()

0 commit comments

Comments
 (0)