From 5c059b55cae7ca71a40921b8b65a17b70249be0a Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Fri, 16 May 2025 12:18:24 +0200 Subject: [PATCH] cputest: simplify --- .../com.example.cputest/assets/cputest.py | 39 +++++++------------ 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/internal_filesystem/apps/com.example.cputest/assets/cputest.py b/internal_filesystem/apps/com.example.cputest/assets/cputest.py index e53a34ad..9fb6479a 100644 --- a/internal_filesystem/apps/com.example.cputest/assets/cputest.py +++ b/internal_filesystem/apps/com.example.cputest/assets/cputest.py @@ -15,53 +15,50 @@ import hashlib import os import _thread +keeprunning = True + # Configuration -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_thread(): print("\nStarting busy loop stress test...") - global summary + global summary, keeprunning summary += "Busy loop without yield: " iterations = 0 start_time = time.ticks_ms() end_time = start_time + TEST_DURATION - while time.ticks_ms() < end_time and appscreen == lv.screen_active(): + while time.ticks_ms() < end_time and keeprunning: iterations += 1 duration_ms = time.ticks_diff(time.ticks_ms(), start_time) iterations_per_second = (iterations / duration_ms) * 1000 print(f"Busy loop test ran duration: {duration_ms}, average: {iterations_per_second:.2f} iterations/second") summary += f"{iterations_per_second:.2f}/s\n" - return iterations_per_second - - -def stress_test_busy_loop_with_yield(): + # + time.sleep_ms(TEST_SPACING) print("\nStarting busy loop with yield (sleep_ms(0)) stress test...") - global summary summary += "Busy loop with yield: " iterations = 0 start_time = time.ticks_ms() end_time = start_time + TEST_DURATION - while time.ticks_ms() < end_time and appscreen == lv.screen_active(): + while time.ticks_ms() < end_time and keeprunning: 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" - return iterations_per_second - -def stress_test_sha256(): + # + time.sleep_ms(TEST_SPACING) print("\nStarting SHA-256 stress test (1KB data)...") global summary summary += "Busy loop with SHA-256 (1KB): " iterations = 0 start_time = time.ticks_ms() end_time = start_time + TEST_DURATION - while time.ticks_ms() < end_time and appscreen == lv.screen_active(): + while time.ticks_ms() < end_time and keeprunning: hashlib.sha256(DATA).digest() # Compute SHA-256 on 1KB data iterations += 1 duration_ms = time.ticks_diff(time.ticks_ms(), start_time) @@ -69,7 +66,6 @@ def stress_test_sha256(): print(f"SHA-256 test completed: {iterations_per_second:.2f} iterations/second") summary += f" {iterations_per_second:.2f}/s\n" summary += "\nAll tests completed." - return iterations_per_second def update_status_cb(timer): @@ -77,11 +73,12 @@ def update_status_cb(timer): def janitor_cb(timer): + global keeprunning if lv.screen_active() != appscreen: print("cputest.py backgrounded, cleaning up...") janitor.delete() + keeprunning = False update_status_timer.delete() - stress_test_busy_loop_with_yield_timer.delete() appscreen = lv.screen_active() janitor = lv.timer_create(janitor_cb, 500, None) @@ -95,13 +92,5 @@ summary = "Running 3 CPU tests...\n\n" status.set_text(summary) _thread.stack_size(12*1024) -_thread.start_new_thread(stress_test_busy_loop, ()) - -stress_test_busy_loop_with_yield_timer = lv.timer_create(lambda timer: _thread.start_new_thread(stress_test_busy_loop_with_yield, ()), TEST_DURATION * 2, None) -stress_test_busy_loop_with_yield_timer.set_repeat_count(1) -stress_test_busy_loop_with_yield_timer.set_auto_delete(False) - -stress_test_busy_loop_with_yield_timer = lv.timer_create(lambda timer: _thread.start_new_thread(stress_test_sha256, ()), TEST_DURATION * 4, None) -stress_test_busy_loop_with_yield_timer.set_repeat_count(1) -stress_test_busy_loop_with_yield_timer.set_auto_delete(False) +_thread.start_new_thread(stress_test_thread, ())