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
|
"""
This script is used to test the performance of the SNMP bulkwalk operation using threads and processes.
"""
import multiprocessing
import threading
from time import time
import sys
from globals import worker
if __name__ == "__main__":
start_time = time()
# Check if the number of processes/threads is provided as a command line argument
if len(sys.argv) > 1:
num_processes = int(sys.argv[1])
else:
raise ValueError(
"Please specify the number of processes/threads as the first argument"
)
# Check if the user wants to use threads or processes
type_of_execution = sys.argv[2]
if len(sys.argv) > 2 and type_of_execution == "thread":
# Create and start the specified number of threads
threads = []
for _ in range(num_processes):
thread = threading.Thread(target=worker, args=("bulkwalk",))
thread.start()
threads.append(thread)
# Main thread continues to execute while the worker threads are running
print(
f"multi_{type_of_execution}_snmp_bulkwalk: - {num_processes} - Main {type_of_execution} executing"
)
# Wait for all worker threads to finish
for thread in threads:
thread.join()
elif len(sys.argv) > 2 and type_of_execution == "process":
# Create and start the specified number of processes
processes = []
for _ in range(num_processes):
process = multiprocessing.Process(target=worker, args=("bulkwalk",))
process.start()
processes.append(process)
# Main process continues to execute while the worker processes are running
print(
f"multi_{type_of_execution}_snmp_bulkwalk: - {num_processes} - Main {type_of_execution} executing"
)
# Wait for all worker processes to finish
for process in processes:
process.join()
else:
raise ValueError("Please specify 'thread' or 'process' as the second argument")
# All processes/threads have finished, program exits
print(
f"multi_{type_of_execution}_snmp_bulkwalk: - {num_processes} - Main {type_of_execution} finished"
)
# Calculate and print the total execution time
execution_time = time() - start_time
print(
f"multi_{type_of_execution}_snmp_bulkwalk: - {num_processes} - Total execution time: {execution_time} seconds"
)
|