|
17 | 17 | import os |
18 | 18 |
|
19 | 19 | # Configuration |
20 | | -TEST_DURATION = 5 # Duration of each test in seconds |
| 20 | +START_SPACING = 2000 # Wait for task bar to go up |
| 21 | +TEST_DURATION = 5000 # Duration of each test (ms) |
| 22 | +TEST_SPACING = 1000 # Wait between tests (ms) |
21 | 23 | DATA_SIZE = 1024 # 1KB of data for SHA-256 test |
22 | 24 | DATA = os.urandom(DATA_SIZE) # Generate 1KB of random data for SHA-256 |
23 | 25 |
|
24 | | -def stress_test_busy_loop(): |
| 26 | +def stress_test_busy_loop(timer): |
25 | 27 | print("\nStarting busy loop stress test...") |
| 28 | + global status, summary |
| 29 | + summary += "Busy loop without yield:" |
| 30 | + status.set_text(summary) |
26 | 31 | iterations = 0 |
27 | 32 | start_time = time.ticks_ms() |
28 | | - end_time = start_time + (TEST_DURATION * 1000) |
| 33 | + end_time = start_time + TEST_DURATION |
29 | 34 | while time.ticks_ms() < end_time and appscreen == lv.screen_active(): |
30 | 35 | iterations += 1 |
31 | 36 | duration_ms = time.ticks_diff(time.ticks_ms(), start_time) |
32 | 37 | iterations_per_second = (iterations / duration_ms) * 1000 |
33 | 38 | print(f"Busy loop test completed: {iterations_per_second:.2f} iterations/second") |
| 39 | + summary += f" {iterations_per_second:.2f}/s\n" |
| 40 | + status.set_text(summary) |
34 | 41 | return iterations_per_second |
35 | 42 |
|
36 | 43 |
|
37 | | -def stress_test_busy_loop_with_yield(): |
| 44 | +def stress_test_busy_loop_with_yield(timer): |
38 | 45 | print("\nStarting busy loop with yield (sleep_ms(0)) stress test...") |
| 46 | + global status, summary |
| 47 | + summary += "Busy loop with yield:" |
| 48 | + status.set_text(summary) |
39 | 49 | iterations = 0 |
40 | 50 | start_time = time.ticks_ms() |
41 | | - end_time = start_time + (TEST_DURATION * 1000) |
| 51 | + end_time = start_time + TEST_DURATION |
42 | 52 | while time.ticks_ms() < end_time and appscreen == lv.screen_active(): |
43 | 53 | iterations += 1 |
44 | 54 | time.sleep_ms(0) # Yield to other tasks |
45 | 55 | duration_ms = time.ticks_diff(time.ticks_ms(), start_time) |
46 | 56 | iterations_per_second = (iterations / duration_ms) * 1000 |
47 | 57 | print(f"Busy loop with yield test completed: {iterations_per_second:.2f} iterations/second") |
| 58 | + summary += f" {iterations_per_second:.2f}/s\n" |
| 59 | + status.set_text(summary) |
48 | 60 | return iterations_per_second |
49 | 61 |
|
50 | 62 |
|
51 | | -def stress_test_sha256(): |
| 63 | +def stress_test_sha256(timer): |
52 | 64 | print("\nStarting SHA-256 stress test (1KB data)...") |
| 65 | + global status, summary |
53 | 66 | iterations = 0 |
54 | 67 | start_time = time.ticks_ms() |
55 | | - end_time = start_time + (TEST_DURATION * 1000) |
| 68 | + end_time = start_time + TEST_DURATION |
56 | 69 | while time.ticks_ms() < end_time and appscreen == lv.screen_active(): |
57 | 70 | hashlib.sha256(DATA).digest() # Compute SHA-256 on 1KB data |
58 | 71 | iterations += 1 |
59 | 72 | duration_ms = time.ticks_diff(time.ticks_ms(), start_time) |
60 | 73 | iterations_per_second = (iterations / duration_ms) * 1000 |
61 | 74 | print(f"SHA-256 test completed: {iterations_per_second:.2f} iterations/second") |
| 75 | + summary += f"SHA-256 (1KB): {iterations_per_second:.2f}/s\n" |
| 76 | + summary += "\nAll tests completed." |
| 77 | + status.set_text(summary) |
62 | 78 | return iterations_per_second |
63 | 79 |
|
64 | 80 |
|
| 81 | +def janitor_cb(timer): |
| 82 | + if lv.screen_active() != appscreen: |
| 83 | + print("cputest.py backgrounded, cleaning up...") |
| 84 | + janitor.delete() |
| 85 | + #sock.close() |
| 86 | + #get_price_timer.delete() |
| 87 | + |
| 88 | +appscreen = lv.screen_active() |
| 89 | +janitor = lv.timer_create(janitor_cb, 500, None) |
| 90 | + |
65 | 91 | status = lv.label(appscreen) |
66 | 92 | status.align(lv.ALIGN.TOP_LEFT, 5, 10) |
67 | 93 | status.set_style_text_color(lv.color_hex(0xFFFFFF), 0) |
68 | 94 |
|
69 | | -summary = "Running CPU tests...\n\n" |
| 95 | +summary = "Running 3 CPU tests...\n\n" |
70 | 96 | status.set_text(summary) |
71 | 97 |
|
72 | | -# Run busy loop test |
73 | | -busy_loop_ips = stress_test_busy_loop() |
74 | | -summary += f"Busy loop: {busy_loop_ips:.2f}/second\n" |
75 | | -status.set_text(summary) |
76 | | - |
77 | | -# Small delay to stabilize system |
78 | | -time.sleep_ms(500) |
79 | | - |
80 | | -# Run busy loop with yield test |
81 | | -yield_loop_ips = stress_test_busy_loop_with_yield() |
82 | | -summary += f"Busy loop with yield: {yield_loop_ips:.2f}/second\n" |
83 | | -status.set_text(summary) |
| 98 | +stress_test_busy_loop_timer = lv.timer_create(stress_test_busy_loop, START_SPACING, None) |
| 99 | +stress_test_busy_loop_timer.set_repeat_count(1) |
84 | 100 |
|
85 | | -# Small delay to stabilize system |
86 | | -time.sleep_ms(500) |
| 101 | +stress_test_busy_loop_with_yield_timer = lv.timer_create(stress_test_busy_loop_with_yield, START_SPACING + TEST_DURATION + TEST_SPACING, None) |
| 102 | +stress_test_busy_loop_with_yield_timer.set_repeat_count(1) |
87 | 103 |
|
88 | | -# Run SHA-256 test |
89 | | -sha256_ips = stress_test_sha256() |
90 | | -summary += f"SHA-256 (1KB): {sha256_ips:.2f}/second\n" |
91 | | -summary += "\nAll tests completed." |
92 | | -status.set_text(summary) |
| 104 | +sha256_timer = lv.timer_create(stress_test_sha256, START_SPACING + 2 * TEST_DURATION + 2 * TEST_SPACING, None) |
| 105 | +sha256_timer.set_repeat_count(1) |
93 | 106 |
|
94 | | -# Wait until the user closes the app |
95 | | -while appscreen == lv.screen_active(): |
96 | | - time.sleep_ms(5000) |
|
0 commit comments