1515import os
1616import _thread
1717
18+ keeprunning = True
19+
1820# Configuration
19- START_SPACING = 2000 # Wait for task bar to go up
2021TEST_DURATION = 5000 # Duration of each test (ms)
2122TEST_SPACING = 1000 # Wait between tests (ms)
2223DATA_SIZE = 1024 # 1KB of data for SHA-256 test
2324DATA = os .urandom (DATA_SIZE ) # Generate 1KB of random data for SHA-256
2425
25- def stress_test_busy_loop ():
26+ def stress_test_thread ():
2627 print ("\n Starting busy loop stress test..." )
27- global summary
28+ global summary , keeprunning
2829 summary += "Busy loop without yield: "
2930 iterations = 0
3031 start_time = time .ticks_ms ()
3132 end_time = start_time + TEST_DURATION
32- while time .ticks_ms () < end_time and appscreen == lv . screen_active () :
33+ while time .ticks_ms () < end_time and keeprunning :
3334 iterations += 1
3435 duration_ms = time .ticks_diff (time .ticks_ms (), start_time )
3536 iterations_per_second = (iterations / duration_ms ) * 1000
3637 print (f"Busy loop test ran duration: { duration_ms } , average: { iterations_per_second :.2f} iterations/second" )
3738 summary += f"{ iterations_per_second :.2f} /s\n "
38- return iterations_per_second
39-
40-
41- def stress_test_busy_loop_with_yield ():
39+ #
40+ time .sleep_ms (TEST_SPACING )
4241 print ("\n Starting busy loop with yield (sleep_ms(0)) stress test..." )
43- global summary
4442 summary += "Busy loop with yield: "
4543 iterations = 0
4644 start_time = time .ticks_ms ()
4745 end_time = start_time + TEST_DURATION
48- while time .ticks_ms () < end_time and appscreen == lv . screen_active () :
46+ while time .ticks_ms () < end_time and keeprunning :
4947 iterations += 1
5048 time .sleep_ms (0 ) # Yield to other tasks
5149 duration_ms = time .ticks_diff (time .ticks_ms (), start_time )
5250 iterations_per_second = (iterations / duration_ms ) * 1000
5351 print (f"Busy loop with yield test completed: { iterations_per_second :.2f} iterations/second" )
5452 summary += f"{ iterations_per_second :.2f} /s\n "
55- return iterations_per_second
56-
57- def stress_test_sha256 ():
53+ #
54+ time .sleep_ms (TEST_SPACING )
5855 print ("\n Starting SHA-256 stress test (1KB data)..." )
5956 global summary
6057 summary += "Busy loop with SHA-256 (1KB): "
6158 iterations = 0
6259 start_time = time .ticks_ms ()
6360 end_time = start_time + TEST_DURATION
64- while time .ticks_ms () < end_time and appscreen == lv . screen_active () :
61+ while time .ticks_ms () < end_time and keeprunning :
6562 hashlib .sha256 (DATA ).digest () # Compute SHA-256 on 1KB data
6663 iterations += 1
6764 duration_ms = time .ticks_diff (time .ticks_ms (), start_time )
6865 iterations_per_second = (iterations / duration_ms ) * 1000
6966 print (f"SHA-256 test completed: { iterations_per_second :.2f} iterations/second" )
7067 summary += f" { iterations_per_second :.2f} /s\n "
7168 summary += "\n All tests completed."
72- return iterations_per_second
7369
7470
7571def update_status_cb (timer ):
7672 status .set_text (summary )
7773
7874
7975def janitor_cb (timer ):
76+ global keeprunning
8077 if lv .screen_active () != appscreen :
8178 print ("cputest.py backgrounded, cleaning up..." )
8279 janitor .delete ()
80+ keeprunning = False
8381 update_status_timer .delete ()
84- stress_test_busy_loop_with_yield_timer .delete ()
8582
8683appscreen = lv .screen_active ()
8784janitor = lv .timer_create (janitor_cb , 500 , None )
@@ -95,13 +92,5 @@ def janitor_cb(timer):
9592status .set_text (summary )
9693
9794_thread .stack_size (12 * 1024 )
98- _thread .start_new_thread (stress_test_busy_loop , ())
99-
100- 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 )
101- stress_test_busy_loop_with_yield_timer .set_repeat_count (1 )
102- stress_test_busy_loop_with_yield_timer .set_auto_delete (False )
103-
104- stress_test_busy_loop_with_yield_timer = lv .timer_create (lambda timer : _thread .start_new_thread (stress_test_sha256 , ()), TEST_DURATION * 4 , None )
105- stress_test_busy_loop_with_yield_timer .set_repeat_count (1 )
106- stress_test_busy_loop_with_yield_timer .set_auto_delete (False )
95+ _thread .start_new_thread (stress_test_thread , ())
10796
0 commit comments