Debian Codegraph @

test/speed_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2015-2019 Arm Limited
3 # Copyright (c) 2021 Chris Reed
4 # SPDX-License-Identifier: Apache-2.0
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 import os
19 import sys
20 from time import (sleep, time)
21 from random import randrange
22 import traceback
23 import argparse
24 import logging
25
26 from pyocd.core.helpers import ConnectHelper
27 from pyocd.probe.pydapaccess import DAPAccess
28 from pyocd.core.memory_map import MemoryType
29 from pyocd.utility import conversion
30
31 from test_util import (
32 Test,
33 TestResult,
34 get_session_options,
35 get_target_test_params
36 )
37
38 _1MB = (1 * 1024 * 1024)
39
40 class SpeedTestResult(TestResult):
41 def __init__(self):
42 super(SpeedTestResult, self).__init__(None, None, None)
43 self.name = "speed"
44
45 class SpeedTest(Test):
46 def __init__(self):
47 super(SpeedTest, self).__init__("Speed Test", speed_test)
48
49 def print_perf_info(self, result_list, output_file=None):
50 format_str = "{:<15}{:>18}{:>18}{:>18}"
51 result_list = filter(lambda x: isinstance(x, SpeedTestResult), result_list)
52 print("\n\n------ Speed Test Performance ------", file=output_file)
53 print(format_str.format("Target", "RAM Read Speed", "RAM Write Speed", "ROM Read Speed"),
54 file=output_file)
55 print("", file=output_file)
56 for result in result_list:
57 if result.passed:
58 read_speed = "%.3f KB/s" % (float(result.read_speed) / float(1000))
59 write_speed = "%.3f KB/s" % (float(result.write_speed) / float(1000))
60 rom_read_speed = "%.3f KB/s" % (float(result.rom_read_speed) / float(1000))
61 else:
62 read_speed = "Fail"
63 write_speed = "Fail"
64 rom_read_speed = "Fail"
65 print(format_str.format(result.board,
66 read_speed, write_speed, rom_read_speed),
67 file=output_file)
68 print("", file=output_file)
69
70 def run(self, board):
71 passed = False
72 read_speed = None
73 write_speed = None
74 try:
75 result = self.test_function(board.unique_id)
76 except Exception as e:
77 print("Exception %s when testing board %s" % (e, board.unique_id))
78 result = SpeedTestResult()
79 result.passed = False
80 traceback.print_exc(file=sys.stdout)
81 result.board = board
82 result.test = self
83 return result
84
85
86 def speed_test(board_id):
87 with ConnectHelper.session_with_chosen_probe(unique_id=board_id, **get_session_options()) as session:
88 board = session.board
89 target_type = board.target_type
90
91 memory_map = board.target.get_memory_map()
92 ram_region = memory_map.get_default_region_of_type(MemoryType.RAM)
93 rom_region = memory_map.get_boot_memory()
94
95 # Limit region sizes used for performance testing to 1 MB. We don't really need to
96 # be reading all 32 MB of a QSPI!
97 ram_start = ram_region.start
98 ram_size = min(ram_region.length, _1MB)
99 rom_start = rom_region.start
100 rom_size = min(rom_region.length, _1MB)
101
102 target = board.target
103
104 test_pass_count = 0
105 test_count = 0
106 result = SpeedTestResult()
107
108 test_params = get_target_test_params(session)
109 session.probe.set_clock(test_params['test_clock'])
110
111 test_config = "uncached 8-bit"
112
113 def test_ram(record_speed=False, width=8):
114 print("\n\n------ TEST RAM READ / WRITE SPEED [%s] ------" % test_config)
115 test_addr = ram_start
116 test_size = ram_size
117 data = [randrange(1, 50) for x in range(test_size)]
118 start = time()
119 if width == 8:
120 target.write_memory_block8(test_addr, data)
121 elif width == 32:
122 target.write_memory_block32(test_addr, conversion.byte_list_to_u32le_list(data))
123 target.flush()
124 stop = time()
125 diff = stop - start
126 if diff == 0:
127 print("Unexpected ram write elapsed time of 0!")
128 write_speed = 0
129 else:
130 write_speed = test_size / diff
131 if record_speed:
132 result.write_speed = write_speed
133 print("Writing %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, write_speed))
134 start = time()
135 if width == 8:
136 block = target.read_memory_block8(test_addr, test_size)
137 elif width == 32:
138 block = conversion.u32le_list_to_byte_list(target.read_memory_block32(test_addr, test_size // 4))
139 target.flush()
140 stop = time()
141 diff = stop - start
142 if diff == 0:
143 print("Unexpected ram read elapsed time of 0!")
144 read_speed = 0
145 else:
146 read_speed = test_size / diff
147 if record_speed:
148 result.read_speed = read_speed
149 print("Reading %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, read_speed))
150 error = False
151 if len(block) != len(data):
152 error = True
153 print("ERROR: read length (%d) != write length (%d)!" % (len(block), len(data)))
154 if not error:
155 for i in range(len(block)):
156 if (block[i] != data[i]):
157 error = True
158 print("ERROR: 0x%X, 0x%X, 0x%X!!!" % ((test_addr + i), block[i], data[i]))
159 if error:
160 print("TEST FAILED")
161 else:
162 print("TEST PASSED")
163 return not error
164
165 def test_rom(record_speed=False, width=8):
166 print("\n\n------ TEST ROM READ SPEED [%s] ------" % test_config)
167 test_addr = rom_start
168 test_size = rom_size
169 start = time()
170 if width == 8:
171 block = target.read_memory_block8(test_addr, test_size)
172 elif width == 32:
173 block = conversion.u32le_list_to_byte_list(target.read_memory_block32(test_addr, test_size // 4))
174 target.flush()
175 stop = time()
176 diff = stop - start
177 if diff == 0:
178 print("Unexpected rom read elapsed time of 0!")
179 read_speed = 0
180 else:
181 read_speed = test_size / diff
182 if record_speed:
183 result.rom_read_speed = read_speed
184 print("Reading %i byte took %.3f seconds: %.3f B/s" % (test_size, diff, read_speed))
185 print("TEST PASSED")
186 return True
187
188 # 8-bit without memcache
189 passed = test_ram(True, 8)
190 test_count += 1
191 test_pass_count += int(passed)
192
193 passed = test_rom(True, 8)
194 test_count += 1
195 test_pass_count += int(passed)
196
197 # 32-bit without memcache
198 test_config = "uncached 32-bit"
199 passed = test_ram(False, 32)
200 test_count += 1
201 test_pass_count += int(passed)
202
203 passed = test_rom(False, 32)
204 test_count += 1
205 test_pass_count += int(passed)
206
207 # With memcache
208 target = target.get_target_context()
209 test_config = "cached 8-bit, pass 1"
210
211 passed = test_ram()
212 test_count += 1
213 test_pass_count += int(passed)
214
215 passed = test_rom()
216 test_count += 1
217 test_pass_count += int(passed)
218
219 # Again with memcache
220 test_config = "cached 8-bit, pass 2"
221 passed = test_ram()
222 test_count += 1
223 test_pass_count += int(passed)
224
225 passed = test_rom()
226 test_count += 1
227 test_pass_count += int(passed)
228
229 board.target.reset()
230
231 result.passed = test_count == test_pass_count
232 return result
233
234 if __name__ == "__main__":
235 parser = argparse.ArgumentParser(description='pyOCD speed test')
236 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
237 parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
238 args = parser.parse_args()
239 level = logging.DEBUG if args.debug else logging.INFO
240 logging.basicConfig(level=level)
241 DAPAccess.set_args(args.daparg)
242 session = ConnectHelper.session_with_chosen_probe(**get_session_options())
243 test = SpeedTest()
244 result = [test.run(session.board)]
245 test.print_perf_info(result)