-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstress_test.py
71 lines (60 loc) · 2.08 KB
/
stress_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import json
import random
import string
import datetime
import requests
from tqdm import tqdm
from dotenv import load_dotenv
load_dotenv()
# Function to generate random strings
def generate_random_string(size: int) -> str:
return ''.join(random.choices(string.digits, k=size))
# Function to generate a large random file of 6MB
def generate_file(file_path: str, size_mb: int = 6):
size_bytes = size_mb * 1024 * 1024 # Convert size to bytes
with open(file_path, 'w') as f:
f.write(generate_random_string(size_bytes))
def generate_large_file(file_path: str):
generate_file(file_path, 6)
def send_log_to_server(url: str, log_data: dict):
try:
response = requests.post(url, json=log_data)
if response.status_code == 200:
print(f"Log sent successfully: {response.json()}")
else:
print(f"Failed to send log: {response.status_code}, {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error sending log: {e}")
# Generate a random large file
large_file_path = "large_file.txt"
generate_large_file(large_file_path)
# Create log data
log_data = {
"timestamp": "",
"system_name": "test_system",
"username": "test_user",
"service_name": "test_service",
"interaction_type": "file_upload",
"value": {"file": ""},
}
# Get the log server URL from environment variables
HOST = os.getenv("HOST", "localhost")
PORT = os.getenv("PORT", "3030")
log_server_url = f"http://{HOST}:{PORT}/log"
for i in tqdm(range(10000)):
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%dT%H:%M:%S")
system_name = random.choice(["system1"])
username = random.choice(["user1", "user2", "user3"])
log_data["timestamp"] = timestamp
log_data["system_name"] = system_name
log_data["username"] = username
if i % 10 == 0:
log_data["value"]["file"] = open(large_file_path, 'r').read()
else:
# small file
log_data["value"]["file"] = generate_random_string(1000)
send_log_to_server(log_server_url, log_data)
# clean up
os.remove(large_file_path)