-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-testv2.py
61 lines (49 loc) · 1.66 KB
/
load-testv2.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
# 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 = 1000
# 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):
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(1)
# 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()