Skip to content

Commit 5180ee5

Browse files
cputest: rework for timers
1 parent 08fb82a commit 5180ee5

File tree

1 file changed

+40
-30
lines changed
  • internal_filesystem/apps/com.example.cputest/assets

1 file changed

+40
-30
lines changed

internal_filesystem/apps/com.example.cputest/assets/cputest.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,80 +17,90 @@
1717
import os
1818

1919
# 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)
2123
DATA_SIZE = 1024 # 1KB of data for SHA-256 test
2224
DATA = os.urandom(DATA_SIZE) # Generate 1KB of random data for SHA-256
2325

24-
def stress_test_busy_loop():
26+
def stress_test_busy_loop(timer):
2527
print("\nStarting busy loop stress test...")
28+
global status, summary
29+
summary += "Busy loop without yield:"
30+
status.set_text(summary)
2631
iterations = 0
2732
start_time = time.ticks_ms()
28-
end_time = start_time + (TEST_DURATION * 1000)
33+
end_time = start_time + TEST_DURATION
2934
while time.ticks_ms() < end_time and appscreen == lv.screen_active():
3035
iterations += 1
3136
duration_ms = time.ticks_diff(time.ticks_ms(), start_time)
3237
iterations_per_second = (iterations / duration_ms) * 1000
3338
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)
3441
return iterations_per_second
3542

3643

37-
def stress_test_busy_loop_with_yield():
44+
def stress_test_busy_loop_with_yield(timer):
3845
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)
3949
iterations = 0
4050
start_time = time.ticks_ms()
41-
end_time = start_time + (TEST_DURATION * 1000)
51+
end_time = start_time + TEST_DURATION
4252
while time.ticks_ms() < end_time and appscreen == lv.screen_active():
4353
iterations += 1
4454
time.sleep_ms(0) # Yield to other tasks
4555
duration_ms = time.ticks_diff(time.ticks_ms(), start_time)
4656
iterations_per_second = (iterations / duration_ms) * 1000
4757
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)
4860
return iterations_per_second
4961

5062

51-
def stress_test_sha256():
63+
def stress_test_sha256(timer):
5264
print("\nStarting SHA-256 stress test (1KB data)...")
65+
global status, summary
5366
iterations = 0
5467
start_time = time.ticks_ms()
55-
end_time = start_time + (TEST_DURATION * 1000)
68+
end_time = start_time + TEST_DURATION
5669
while time.ticks_ms() < end_time and appscreen == lv.screen_active():
5770
hashlib.sha256(DATA).digest() # Compute SHA-256 on 1KB data
5871
iterations += 1
5972
duration_ms = time.ticks_diff(time.ticks_ms(), start_time)
6073
iterations_per_second = (iterations / duration_ms) * 1000
6174
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)
6278
return iterations_per_second
6379

6480

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+
6591
status = lv.label(appscreen)
6692
status.align(lv.ALIGN.TOP_LEFT, 5, 10)
6793
status.set_style_text_color(lv.color_hex(0xFFFFFF), 0)
6894

69-
summary = "Running CPU tests...\n\n"
95+
summary = "Running 3 CPU tests...\n\n"
7096
status.set_text(summary)
7197

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)
84100

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)
87103

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)
93106

94-
# Wait until the user closes the app
95-
while appscreen == lv.screen_active():
96-
time.sleep_ms(5000)

0 commit comments

Comments
 (0)