forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_superpi.py
53 lines (42 loc) · 1.88 KB
/
test_superpi.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
import json
import logging
import os
def test_superpi(dut, request):
LOGGER = logging.getLogger(__name__)
# Match "Runs: %d"
res = dut.expect(r"Runs: (\d+)", timeout=60)
runs = int(res.group(0).decode("utf-8").split(" ")[1])
LOGGER.info("Number of runs: {}".format(runs))
# Match "Digits: %d"
res = dut.expect(r"Digits: (\d+)", timeout=60)
digits = int(res.group(0).decode("utf-8").split(" ")[1])
LOGGER.info("Number of decimal digits: {}".format(digits))
list_time = []
for i in range(runs):
# Match "Run %d"
res = dut.expect(r"Run (\d+)", timeout=120)
run = int(res.group(0).decode("utf-8").split(" ")[1])
LOGGER.info("Run {}".format(run))
assert run == i, "Invalid run number"
# Match "Time: %lu.%03lu s"
res = dut.expect(r"Time: (\d+)\.(\d+) s", timeout=300)
time = float(res.group(0).decode("utf-8").split(" ")[1])
LOGGER.info("Time on run {}: {} s".format(i, time))
assert time > 0 and time < 1000, "Invalid time"
list_time.append(time)
avg_time = round(sum(list_time) / len(list_time), 3)
# Create JSON with results and write it to file
# Always create a JSON with this format (so it can be merged later on):
# { TEST_NAME_STR: TEST_RESULTS_DICT }
results = {"superpi": {"runs": runs, "digits": digits, "avg_time": avg_time}}
current_folder = os.path.dirname(request.path)
file_index = 0
report_file = os.path.join(current_folder, "result_superpi" + str(file_index) + ".json")
while os.path.exists(report_file):
report_file = report_file.replace(str(file_index) + ".json", str(file_index + 1) + ".json")
file_index += 1
with open(report_file, "w") as f:
try:
f.write(json.dumps(results))
except Exception as e:
LOGGER.warning("Failed to write results to file: {}".format(e))