12
12
from __future__ import print_function
13
13
import logging , os
14
14
from time import time
15
- # from pymodbus.client.sync import ModbusTcpClient
15
+ from pymodbus .client .sync import ModbusTcpClient
16
16
from pymodbus .client .sync import ModbusSerialClient
17
17
18
18
try :
@@ -59,22 +59,56 @@ def single_client_test(host, cycles):
59
59
:param cycles: The number of iterations to perform
60
60
"""
61
61
logger = log_to_stderr ()
62
- logger .setLevel (logging .DEBUG )
62
+ logger .setLevel (logging .WARNING )
63
63
logger .debug ("starting worker: %d" % os .getpid ())
64
64
65
65
try :
66
66
count = 0
67
- # client = ModbusTcpClient(host, port=5020)
68
- client = ModbusSerialClient (method = "rtu" ,
69
- port = "/dev/ttyp0" , baudrate = 9600 )
67
+ client = ModbusTcpClient (host , port = 5020 )
68
+ # client = ModbusSerialClient(method="rtu",
69
+ # port="/dev/ttyp0", baudrate=9600)
70
70
while count < cycles :
71
- with _thread_lock :
72
- client .read_holding_registers (10 , 1 , unit = 1 ).registers [0 ]
73
- count += 1
71
+ # print(count)
72
+ # with _thread_lock:
73
+ client .read_holding_registers (10 , 123 , unit = 1 )
74
+ count += 1
74
75
except :
75
76
logger .exception ("failed to run test successfully" )
76
77
logger .debug ("finished worker: %d" % os .getpid ())
77
78
79
+
80
+ def multiprocessing_test (fn , args ):
81
+ from multiprocessing import Process as Worker
82
+ start = time ()
83
+ procs = [Worker (target = fn , args = args )
84
+ for _ in range (workers )]
85
+
86
+ any (p .start () for p in procs ) # start the workers
87
+ any (p .join () for p in procs ) # wait for the workers to finish
88
+ return start
89
+
90
+
91
+ def thread_test (fn , args ):
92
+ from threading import Thread as Worker
93
+ start = time ()
94
+ procs = [Worker (target = fn , args = args )
95
+ for _ in range (workers )]
96
+
97
+ any (p .start () for p in procs ) # start the workers
98
+ any (p .join () for p in procs ) # wait for the workers to finish
99
+ return start
100
+
101
+
102
+ def thread_pool_exe_test (fn , args ):
103
+ from concurrent .futures import ThreadPoolExecutor as Worker
104
+ from concurrent .futures import as_completed
105
+ start = time ()
106
+ with Worker (max_workers = workers , thread_name_prefix = "Perform" ) as exe :
107
+ futures = {exe .submit (fn , * args ): job for job in range (workers )}
108
+ for future in as_completed (futures ):
109
+ future .result ()
110
+ return start
111
+
78
112
# --------------------------------------------------------------------------- #
79
113
# run our test and check results
80
114
# --------------------------------------------------------------------------- #
@@ -91,12 +125,24 @@ def single_client_test(host, cycles):
91
125
92
126
if __name__ == "__main__" :
93
127
args = (host , int (cycles * 1.0 / workers ))
94
- procs = [Worker (target = single_client_test , args = args )
95
- for _ in range (workers )]
96
- start = time ()
97
- any (p .start () for p in procs ) # start the workers
98
- any (p .join () for p in procs ) # wait for the workers to finish
99
- stop = time ()
100
- print ("%d requests/second" % ((1.0 * cycles ) / (stop - start )))
101
- print ("time taken to complete %s cycle by "
102
- "%s workers is %s seconds" % (cycles , workers , stop - start ))
128
+ # with Worker(max_workers=workers, thread_name_prefix="Perform") as exe:
129
+ # futures = {exe.submit(single_client_test, *args): job for job in range(workers)}
130
+ # for future in as_completed(futures):
131
+ # data = future.result()
132
+ # for _ in range(workers):
133
+ # futures.append(Worker.submit(single_client_test, args=args))
134
+ # procs = [Worker(target=single_client_test, args=args)
135
+ # for _ in range(workers)]
136
+
137
+ # any(p.start() for p in procs) # start the workers
138
+ # any(p.join() for p in procs) # wait for the workers to finish
139
+ # start = multiprocessing_test(single_client_test, args)
140
+ # start = thread_pool_exe_test(single_client_test, args)
141
+ for tester in [multiprocessing_test , thread_test , thread_pool_exe_test ]:
142
+ print (tester .__name__ )
143
+ start = tester (single_client_test , args )
144
+ stop = time ()
145
+ print ("%d requests/second" % ((1.0 * cycles ) / (stop - start )))
146
+ print ("time taken to complete %s cycle by "
147
+ "%s workers is %s seconds" % (cycles , workers , stop - start ))
148
+ print ()
0 commit comments