bzr branch
http://suren.me/webbzr/alps/pcitool
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
1 |
import threading |
346.1.33
by Vasilii Chernov
Move scripts handing code from py.c to Python wrap |
2 |
import pcilib |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
3 |
import random |
4 |
import os |
|
5 |
import json |
|
6 |
import requests |
|
7 |
import time |
|
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
8 |
from optparse import OptionParser, OptionGroup |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
9 |
|
346.1.33
by Vasilii Chernov
Move scripts handing code from py.c to Python wrap |
10 |
class test_pcilib(): |
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
11 |
def __init__(self, |
12 |
device, |
|
13 |
model, |
|
14 |
num_threads = 150, |
|
15 |
write_percentage = 0.1, |
|
16 |
register = 'reg1', |
|
17 |
prop = '/test/prop1', |
|
18 |
branch = '/test', |
|
19 |
server_host = 'http://localhost', |
|
20 |
server_port = 12412, |
|
21 |
server_message_delay = 0): |
|
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
22 |
#initialize enviroment variables
|
23 |
if not 'APP_PATH' in os.environ: |
|
24 |
APP_PATH = '' |
|
25 |
file_dir = os.path.dirname(os.path.abspath(__file__)) |
|
26 |
APP_PATH = str(os.path.abspath(file_dir + '/../..')) |
|
27 |
os.environ["APP_PATH"] = APP_PATH |
|
28 |
||
29 |
if not 'PCILIB_MODEL_DIR' in os.environ: |
|
30 |
os.environ['PCILIB_MODEL_DIR'] = os.environ["APP_PATH"] + "/xml" |
|
31 |
if not 'LD_LIBRARY_PATH' in os.environ: |
|
32 |
os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib" |
|
33 |
||
34 |
random.seed() |
|
35 |
#create pcilib_instance
|
|
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
36 |
self.device = device |
37 |
self.model = model |
|
367
by Suren A. Chilingaryan
Further improvements of Python scripting and web-interface API for register manipulations by Vasiliy Chernov |
38 |
self.pcilib = pcilib.pcilib(device, model) |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
39 |
self.num_threads = num_threads |
40 |
self.write_percentage = write_percentage |
|
41 |
self.register = register |
|
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
42 |
self.prop = prop |
43 |
self.branch = branch |
|
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
44 |
self.server_message_delay = server_message_delay |
45 |
self.server_port = server_port |
|
46 |
self.server_host = server_host |
|
346.1.22
by Vasilii Chernov
1. Http server add tree view |
47 |
|
48 |
def testLocking(self, message, lock_id = None): |
|
49 |
url = str(self.server_host + ':' + str(self.server_port)) |
|
50 |
headers = {'content-type': 'application/json'} |
|
51 |
payload =[{'command': 'lock', 'lock_id': lock_id}, |
|
52 |
{'command': 'try_lock', 'lock_id': lock_id}, |
|
53 |
{'command': 'unlock', 'lock_id': lock_id}, |
|
54 |
{'command': 'lock_global'}, |
|
55 |
{'command': 'unlock_global'}, |
|
56 |
{'command': 'help'}] |
|
57 |
r = requests.get(url, data=json.dumps(payload[message]), headers=headers) |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
58 |
print(json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))) |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
59 |
|
60 |
def testThreadSafeReadWrite(self): |
|
61 |
def threadFunc(): |
|
62 |
if random.randint(0, 100) >= (self.write_percentage * 100): |
|
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
63 |
ret = self.pcilib.get_property(self.prop) |
346.1.35
by Vasilii Chernov
Add Python3 support |
64 |
print(self.register, ':', ret) |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
65 |
del ret |
66 |
else: |
|
67 |
val = random.randint(0, 65536) |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
68 |
print('set value:', val) |
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
69 |
self.pcilib.set_property(val, self.prop) |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
70 |
try: |
71 |
while(1): |
|
72 |
thread_list = [threading.Thread(target=threadFunc) for i in range(0, self.num_threads)] |
|
73 |
for i in range(0, self.num_threads): |
|
74 |
thread_list[i].start() |
|
75 |
for i in range(0, self.num_threads): |
|
76 |
thread_list[i].join() |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
77 |
print('cycle done') |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
78 |
except KeyboardInterrupt: |
346.1.35
by Vasilii Chernov
Add Python3 support |
79 |
print('testing done') |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
80 |
pass
|
81 |
||
82 |
def testMemoryLeak(self): |
|
83 |
try: |
|
84 |
while(1): |
|
346.1.38
by Vasilii Chernov
Fix python3 initialization from pcilib_t |
85 |
val = random.randint(0, 8096) |
367
by Suren A. Chilingaryan
Further improvements of Python scripting and web-interface API for register manipulations by Vasiliy Chernov |
86 |
self.pcilib = pcilib.pcilib(self.device, self.model) |
346.1.35
by Vasilii Chernov
Add Python3 support |
87 |
print(self.pcilib.get_property_list(self.branch)) |
88 |
print(self.pcilib.get_register_info(self.register)) |
|
89 |
print(self.pcilib.get_registers_list()) |
|
90 |
print(self.pcilib.write_register(val, self.register)) |
|
91 |
print(self.pcilib.read_register(self.register)) |
|
92 |
print(self.pcilib.set_property(val, self.prop)) |
|
93 |
print(self.pcilib.get_property(self.prop)) |
|
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
94 |
except KeyboardInterrupt: |
346.1.35
by Vasilii Chernov
Add Python3 support |
95 |
print('testing done') |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
96 |
pass
|
97 |
||
98 |
def testServer(self): |
|
99 |
url = str(self.server_host + ':' + str(self.server_port)) |
|
100 |
headers = {'content-type': 'application/json'} |
|
101 |
payload =[{'com': 'open', 'data2' : '12341'}, |
|
102 |
#{'command': 'open', 'device' : '/dev/fpga0', 'model': 'test_pywrap'},
|
|
103 |
{'command': 'help'}, |
|
104 |
{'command': 'get_registers_list'}, |
|
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
105 |
{'command': 'get_register_info', 'reg': self.register}, |
106 |
{'command': 'get_property_list', 'branch': self.branch}, |
|
107 |
{'command': 'read_register', 'reg': self.register}, |
|
108 |
{'command': 'write_register', 'reg': self.register}, |
|
109 |
{'command': 'get_property', 'prop': self.prop}, |
|
110 |
{'command': 'set_property', 'prop': self.prop}] |
|
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
111 |
|
112 |
def sendRandomMessage(): |
|
113 |
message_number = random.randint(1, len(payload) - 1) |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
114 |
print('message number: ', message_number) |
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
115 |
payload[message_number]['value'] = random.randint(0, 8096) |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
116 |
r = requests.get(url, data=json.dumps(payload[message_number]), headers=headers) |
346.1.35
by Vasilii Chernov
Add Python3 support |
117 |
if(r.headers['content-type'] == 'application/json'): |
118 |
print(json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))) |
|
119 |
else: |
|
120 |
print(r.content) |
|
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
121 |
try: |
122 |
r = requests.get(url, data=json.dumps(payload[1]), headers=headers) |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
123 |
if(r.headers['content-type'] == 'application/json'): |
124 |
print(json.dumps(r.json(), sort_keys=True, indent=3, separators=(',', ': '))) |
|
125 |
else: |
|
126 |
print(r.content) |
|
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
127 |
|
128 |
while(1): |
|
129 |
time.sleep(self.server_message_delay) |
|
130 |
thread_list = [threading.Thread(target=sendRandomMessage) for i in range(0, self.num_threads)] |
|
131 |
for i in range(0, self.num_threads): |
|
132 |
thread_list[i].start() |
|
133 |
for i in range(0, self.num_threads): |
|
134 |
thread_list[i].join() |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
135 |
print('cycle done') |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
136 |
|
137 |
except KeyboardInterrupt: |
|
346.1.35
by Vasilii Chernov
Add Python3 support |
138 |
print('testing done') |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
139 |
pass
|
140 |
||
141 |
if __name__ == '__main__': |
|
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
142 |
#parce command line options
|
143 |
parser = OptionParser() |
|
144 |
parser.add_option("-d", "--device", action="store", |
|
145 |
type="string", dest="device", default=str('/dev/fpga0'), |
|
146 |
help="FPGA device (/dev/fpga0)") |
|
147 |
parser.add_option("-m", "--model", action="store", |
|
367
by Suren A. Chilingaryan
Further improvements of Python scripting and web-interface API for register manipulations by Vasiliy Chernov |
148 |
type="string", dest="model", default=str('test'), |
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
149 |
help="Memory model (autodetected)") |
150 |
parser.add_option("-t", "--threads", action="store", |
|
151 |
type="int", dest="threads", default=150, |
|
152 |
help="Threads number (150)") |
|
153 |
||
154 |
server_group = OptionGroup(parser, "Server Options", |
|
155 |
"Options for testing server.") |
|
156 |
server_group.add_option("-p", "--port", action="store", |
|
157 |
type="int", dest="port", default=9000, |
|
158 |
help="Set testing server port (9000)") |
|
159 |
server_group.add_option("--host", action="store", |
|
160 |
type="string", dest="host", default='http://localhost', |
|
161 |
help="Set testing server host (http://localhost)") |
|
162 |
server_group.add_option("--delay", action="store", |
|
163 |
type="float", dest="delay", default=0.0, |
|
164 |
help="Set delay in seconds between sending messages to setver (0.0)") |
|
165 |
parser.add_option_group(server_group) |
|
166 |
||
167 |
rw_group = OptionGroup(parser, "Registers/properties Options", |
|
168 |
"Group stores testing register and property options") |
|
169 |
rw_group.add_option("--write_percentage", action="store", |
|
170 |
type="float", dest="write_percentage", default=0.5, |
|
171 |
help="Set percentage (0.0 - 1.0) of write commands in multithread (0.5)" |
|
172 |
"read/write test") |
|
173 |
rw_group.add_option("-r", "--register", action="store", |
|
174 |
type="string", dest="register", default='reg1', |
|
175 |
help="Set register name (reg1)") |
|
176 |
rw_group.add_option("--property", action="store", |
|
177 |
type="string", dest="prop", default='/test/prop1', |
|
178 |
help="Set property name (/test/prop1)") |
|
179 |
rw_group.add_option("-b", "--branch", action="store", |
|
180 |
type="string", dest="branch", default='/test', |
|
181 |
help="Set property branch (/test)") |
|
182 |
||
183 |
parser.add_option_group(rw_group) |
|
184 |
||
185 |
test_group = OptionGroup(parser, "Test commands group", |
|
186 |
"This group conatains aviable commands for testing. "
|
|
187 |
"If user add more than one command, they will process"
|
|
188 |
"sequientally. To stop test, press Ctrl-C."
|
|
189 |
)
|
|
190 |
test_group.add_option("--test_mt_rw", action="store_true", |
|
191 |
dest="test_mt_rw", default=False, |
|
192 |
help="Multithread read/write test. This test will execute " |
|
193 |
"get_property/set_property commands with random values in multi-thread mode") |
|
194 |
test_group.add_option("--test_memory_leak", action="store_true", |
|
195 |
dest="test_memory_leak", default=False, |
|
196 |
help="Python wrap memory leak test. This test will execute all" |
|
197 |
"Python wrap memory leak test. This test will execute all"
|
|
198 |
)
|
|
199 |
test_group.add_option("--test_server", action="store_true", |
|
200 |
dest="test_server", default=False, |
|
201 |
help="Python server test. This test will send " |
|
202 |
"random commands to server in multi-thread mode"
|
|
203 |
)
|
|
204 |
parser.add_option_group(test_group) |
|
346.1.22
by Vasilii Chernov
1. Http server add tree view |
205 |
|
206 |
lock_group = OptionGroup(parser, "Locking test group") |
|
207 |
||
208 |
lock_group.add_option("--lock", action="store", |
|
209 |
dest="lock_id", default=None, type="string", |
|
210 |
help="Lock id." |
|
211 |
)
|
|
212 |
lock_group.add_option("--try_lock", action="store", |
|
213 |
dest="try_lock_id", default=None, type="string", |
|
214 |
help="Try to lock id." |
|
215 |
)
|
|
216 |
lock_group.add_option("--unlock", action="store", |
|
217 |
dest="unlock_id", default=None, type="string", |
|
218 |
help="Unlock lock with id." |
|
219 |
)
|
|
220 |
lock_group.add_option("--lock_global", action="store_true", |
|
221 |
dest="lock_global", default=False, |
|
222 |
help="Global pcilib lock." |
|
223 |
)
|
|
224 |
lock_group.add_option("--unlock_global", action="store_true", |
|
225 |
dest="unlock_global", default=False, |
|
226 |
help="Global pcilib unlock." |
|
227 |
)
|
|
228 |
||
229 |
lock_group.add_option("--get_server_message", action="store_true", |
|
230 |
dest="get_server_message", default=False, |
|
231 |
help="Global pcilib unlock." |
|
232 |
)
|
|
233 |
||
234 |
||
235 |
parser.add_option_group(lock_group) |
|
236 |
||
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
237 |
opts = parser.parse_args()[0] |
238 |
||
239 |
#create pcilib test instance
|
|
346.1.33
by Vasilii Chernov
Move scripts handing code from py.c to Python wrap |
240 |
lib = test_pcilib(opts.device, |
346.1.19
by Vasilii Chernov
1. Fix warnings in test_multithread app |
241 |
opts.model, |
242 |
num_threads = opts.threads, |
|
243 |
write_percentage = opts.write_percentage, |
|
244 |
register = opts.register, |
|
245 |
prop = opts.prop, |
|
246 |
branch = opts.branch, |
|
247 |
server_host = opts.host, |
|
248 |
server_port = opts.port, |
|
249 |
server_message_delay = opts.delay) |
|
250 |
||
251 |
#make some tests
|
|
252 |
if opts.test_mt_rw: |
|
253 |
lib.testThreadSafeReadWrite() |
|
254 |
if opts.test_memory_leak: |
|
255 |
lib.testMemoryLeak() |
|
256 |
if opts.test_server: |
|
257 |
lib.testServer() |
|
346.1.22
by Vasilii Chernov
1. Http server add tree view |
258 |
if opts.lock_id: |
259 |
lib.testLocking(0, opts.lock_id) |
|
260 |
if opts.try_lock_id: |
|
261 |
lib.testLocking(1, opts.try_lock_id) |
|
262 |
if opts.unlock_id: |
|
263 |
lib.testLocking(2, opts.unlock_id) |
|
264 |
if opts.lock_global: |
|
265 |
lib.testLocking(3) |
|
266 |
if opts.unlock_global: |
|
267 |
lib.testLocking(4) |
|
268 |
if(opts.get_server_message): |
|
346.1.24
by Vasilii Chernov
Merge with Suren branch. Fix memory leaks. |
269 |
lib.testLocking(5) |
346.1.18
by Vasilii Chernov
Add test to repository. Add GIL states to pcilib_set_value_from_pyobject |
270 |