diff --git a/internal_filesystem/apps/com.example.cputest/assets/cputest.py b/internal_filesystem/apps/com.example.cputest/assets/cputest.py index 300a3bd9..4f76e873 100644 --- a/internal_filesystem/apps/com.example.cputest/assets/cputest.py +++ b/internal_filesystem/apps/com.example.cputest/assets/cputest.py @@ -17,80 +17,90 @@ import hashlib import os # Configuration -TEST_DURATION = 5 # Duration of each test in seconds +START_SPACING = 2000 # Wait for task bar to go up +TEST_DURATION = 5000 # Duration of each test (ms) +TEST_SPACING = 1000 # Wait between tests (ms) DATA_SIZE = 1024 # 1KB of data for SHA-256 test DATA = os.urandom(DATA_SIZE) # Generate 1KB of random data for SHA-256 -def stress_test_busy_loop(): +def stress_test_busy_loop(timer): print("\nStarting busy loop stress test...") + global status, summary + summary += "Busy loop without yield:" + status.set_text(summary) iterations = 0 start_time = time.ticks_ms() - end_time = start_time + (TEST_DURATION * 1000) + end_time = start_time + TEST_DURATION while time.ticks_ms() < end_time and appscreen == lv.screen_active(): iterations += 1 duration_ms = time.ticks_diff(time.ticks_ms(), start_time) iterations_per_second = (iterations / duration_ms) * 1000 print(f"Busy loop test completed: {iterations_per_second:.2f} iterations/second") + summary += f" {iterations_per_second:.2f}/s\n" + status.set_text(summary) return iterations_per_second -def stress_test_busy_loop_with_yield(): +def stress_test_busy_loop_with_yield(timer): print("\nStarting busy loop with yield (sleep_ms(0)) stress test...") + global status, summary + summary += "Busy loop with yield:" + status.set_text(summary) iterations = 0 start_time = time.ticks_ms() - end_time = start_time + (TEST_DURATION * 1000) + end_time = start_time + TEST_DURATION while time.ticks_ms() < end_time and appscreen == lv.screen_active(): iterations += 1 time.sleep_ms(0) # Yield to other tasks duration_ms = time.ticks_diff(time.ticks_ms(), start_time) iterations_per_second = (iterations / duration_ms) * 1000 print(f"Busy loop with yield test completed: {iterations_per_second:.2f} iterations/second") + summary += f" {iterations_per_second:.2f}/s\n" + status.set_text(summary) return iterations_per_second -def stress_test_sha256(): +def stress_test_sha256(timer): print("\nStarting SHA-256 stress test (1KB data)...") + global status, summary iterations = 0 start_time = time.ticks_ms() - end_time = start_time + (TEST_DURATION * 1000) + end_time = start_time + TEST_DURATION while time.ticks_ms() < end_time and appscreen == lv.screen_active(): hashlib.sha256(DATA).digest() # Compute SHA-256 on 1KB data iterations += 1 duration_ms = time.ticks_diff(time.ticks_ms(), start_time) iterations_per_second = (iterations / duration_ms) * 1000 print(f"SHA-256 test completed: {iterations_per_second:.2f} iterations/second") + summary += f"SHA-256 (1KB): {iterations_per_second:.2f}/s\n" + summary += "\nAll tests completed." + status.set_text(summary) return iterations_per_second +def janitor_cb(timer): + if lv.screen_active() != appscreen: + print("cputest.py backgrounded, cleaning up...") + janitor.delete() + #sock.close() + #get_price_timer.delete() + +appscreen = lv.screen_active() +janitor = lv.timer_create(janitor_cb, 500, None) + status = lv.label(appscreen) status.align(lv.ALIGN.TOP_LEFT, 5, 10) status.set_style_text_color(lv.color_hex(0xFFFFFF), 0) -summary = "Running CPU tests...\n\n" +summary = "Running 3 CPU tests...\n\n" status.set_text(summary) -# Run busy loop test -busy_loop_ips = stress_test_busy_loop() -summary += f"Busy loop: {busy_loop_ips:.2f}/second\n" -status.set_text(summary) +stress_test_busy_loop_timer = lv.timer_create(stress_test_busy_loop, START_SPACING, None) +stress_test_busy_loop_timer.set_repeat_count(1) -# Small delay to stabilize system -time.sleep_ms(500) +stress_test_busy_loop_with_yield_timer = lv.timer_create(stress_test_busy_loop_with_yield, START_SPACING + TEST_DURATION + TEST_SPACING, None) +stress_test_busy_loop_with_yield_timer.set_repeat_count(1) -# Run busy loop with yield test -yield_loop_ips = stress_test_busy_loop_with_yield() -summary += f"Busy loop with yield: {yield_loop_ips:.2f}/second\n" -status.set_text(summary) +sha256_timer = lv.timer_create(stress_test_sha256, START_SPACING + 2 * TEST_DURATION + 2 * TEST_SPACING, None) +sha256_timer.set_repeat_count(1) -# Small delay to stabilize system -time.sleep_ms(500) - -# Run SHA-256 test -sha256_ips = stress_test_sha256() -summary += f"SHA-256 (1KB): {sha256_ips:.2f}/second\n" -summary += "\nAll tests completed." -status.set_text(summary) - -# Wait until the user closes the app -while appscreen == lv.screen_active(): - time.sleep_ms(5000)