-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-testv1.py
76 lines (54 loc) · 2.03 KB
/
load-testv1.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
72
73
74
75
76
# To confirm, broker must suscribe to the 3 sensors, first 2 sensors have priority one and second sensor has priority 2
# mosquitto_sub -h localhost -t "sensor1" -q 1 -t "sensor2" -q 1 -t "sensor3" -q 2
#Added # Function to show a summary of the data transferred
#To modulate the amount of data modify the number of devices.
#11k equals aprox 10gb over 3 hrs
# Import necessary modules
import paho.mqtt.client as mqtt
import random
import time
# Set the broker address and port
broker_address = "37.152.61.101"
broker_port = 1883
counter = 0
# Set the topics and number of devices
topics = ["sensor1", "sensor2", "sensor3"]
num_devices = 3
# Function to generate random data
def generate_data():
data = ""
for i in range(10):
data += str(random.randint(0,9))
return data
# Function to be called when a message is published
def on_publish(client, userdata, mid):
global counter
counter += 1
# Create a new MQTT client
client = mqtt.Client()
# Set the on_publish function to be called when a message is published
client.on_publish = on_publish
# Connect to the broker
client.connect(broker_address, broker_port)
# Set the time variable
run_time = 10 # 10800->3 hours in seconds
# Start a loop to publish messages from each device
start_time = time.time()
while time.time() - start_time < run_time:
for i in range(num_devices * 100):
for topic in topics:
# Generate data for each device and topic
data = generate_data()
# Publish the data to the broker
client.publish(topic, data)
# Sleep for 1 second before publishing again
time.sleep(0.01)
# Function to show a summary of the data transferred
def show_summary():
data_transferred = counter * 10 # 10 bytes per message
data_transferred_mb = data_transferred / 1000000 # Convert to megabytes
# print(f"Total data transferred: {data_transferred_mb} MB")
print(f"Total messages sent: {counter}")
print(f"Total time elapsed: {run_time} seconds")
# Call the show_summary function
show_summary()