-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriter.py
51 lines (36 loc) · 1.36 KB
/
writer.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
import socket
import os
import time
import numpy as np
# Constants
PORT = 7000
DATA_DIRECTORY = "./data"
START_MESSAGE = "start"
# Assuming a Poisson distribution for file sizes (in KB) for simplicity
LAMBDA = 10
# File creation rate (files per second)
RATE = 1
# Create data directory if it doesn't exist
if not os.path.exists(DATA_DIRECTORY):
os.makedirs(DATA_DIRECTORY)
# Start TCP server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind(('', PORT))
server_socket.listen()
print(f"Listening on port {PORT}...")
while True:
client_socket, addr = server_socket.accept()
with client_socket:
message = client_socket.recv(1024).decode('utf-8')
message = message.strip()
if message == START_MESSAGE:
print("Start message received. Beginning data write.")
break
# Begin data generation
while True: # Replace with a suitable condition to stop the test
file_size = np.random.poisson(LAMBDA) * 1024 # File size in bytes
file_content = os.urandom(file_size) # Generate random content
file_path = os.path.join(DATA_DIRECTORY, f"{time.time()}.dat")
with open(file_path, 'wb') as file:
file.write(file_content)
time.sleep(1/RATE) # Adjust the rate of file creation