Skip to content

Commit df7bb0e

Browse files
cputest: use threads to remain responsive
1 parent 5180ee5 commit df7bb0e

File tree

1 file changed

+29
-18
lines changed
  • internal_filesystem/apps/com.example.cputest/assets

1 file changed

+29
-18
lines changed

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
# Busy loop with yield: 15923 iterations/second
1111
# SHA-256 (1KB): 5269 iterations/second
1212

13-
appscreen = lv.screen_active()
14-
1513
import time
1614
import hashlib
1715
import os
16+
import _thread
1817

1918
# Configuration
2019
START_SPACING = 2000 # Wait for task bar to go up
@@ -23,29 +22,29 @@
2322
DATA_SIZE = 1024 # 1KB of data for SHA-256 test
2423
DATA = os.urandom(DATA_SIZE) # Generate 1KB of random data for SHA-256
2524

26-
def stress_test_busy_loop(timer):
25+
def stress_test_busy_loop():
2726
print("\nStarting busy loop stress test...")
28-
global status, summary
27+
global summary
2928
summary += "Busy loop without yield:"
30-
status.set_text(summary)
29+
#status.set_text(summary)
3130
iterations = 0
3231
start_time = time.ticks_ms()
3332
end_time = start_time + TEST_DURATION
3433
while time.ticks_ms() < end_time and appscreen == lv.screen_active():
3534
iterations += 1
3635
duration_ms = time.ticks_diff(time.ticks_ms(), start_time)
3736
iterations_per_second = (iterations / duration_ms) * 1000
38-
print(f"Busy loop test completed: {iterations_per_second:.2f} iterations/second")
37+
print(f"Busy loop test ran duration: {duration_ms}, average: {iterations_per_second:.2f} iterations/second")
3938
summary += f" {iterations_per_second:.2f}/s\n"
40-
status.set_text(summary)
39+
#status.set_text(summary)
4140
return iterations_per_second
4241

4342

44-
def stress_test_busy_loop_with_yield(timer):
43+
def stress_test_busy_loop_with_yield():
4544
print("\nStarting busy loop with yield (sleep_ms(0)) stress test...")
46-
global status, summary
45+
global summary
4746
summary += "Busy loop with yield:"
48-
status.set_text(summary)
47+
#status.set_text(summary)
4948
iterations = 0
5049
start_time = time.ticks_ms()
5150
end_time = start_time + TEST_DURATION
@@ -56,9 +55,12 @@ def stress_test_busy_loop_with_yield(timer):
5655
iterations_per_second = (iterations / duration_ms) * 1000
5756
print(f"Busy loop with yield test completed: {iterations_per_second:.2f} iterations/second")
5857
summary += f" {iterations_per_second:.2f}/s\n"
59-
status.set_text(summary)
58+
#status.set_text(summary)
6059
return iterations_per_second
6160

61+
def stress_test_busy_loop_with_yield_thread(timer):
62+
#_thread.stack_size(12*1024)
63+
_thread.start_new_thread(stress_test_busy_loop_with_yield, ())
6264

6365
def stress_test_sha256(timer):
6466
print("\nStarting SHA-256 stress test (1KB data)...")
@@ -78,15 +80,20 @@ def stress_test_sha256(timer):
7880
return iterations_per_second
7981

8082

83+
def update_status_cb(timer):
84+
status.set_text(summary)
85+
86+
8187
def janitor_cb(timer):
8288
if lv.screen_active() != appscreen:
8389
print("cputest.py backgrounded, cleaning up...")
8490
janitor.delete()
85-
#sock.close()
86-
#get_price_timer.delete()
91+
update_status_timer.delete()
92+
stress_test_busy_loop_with_yield_timer.delete()
8793

8894
appscreen = lv.screen_active()
8995
janitor = lv.timer_create(janitor_cb, 500, None)
96+
update_status_timer = lv.timer_create(update_status_cb, 200, None)
9097

9198
status = lv.label(appscreen)
9299
status.align(lv.ALIGN.TOP_LEFT, 5, 10)
@@ -95,12 +102,16 @@ def janitor_cb(timer):
95102
summary = "Running 3 CPU tests...\n\n"
96103
status.set_text(summary)
97104

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)
105+
_thread.stack_size(12*1024)
106+
_thread.start_new_thread(stress_test_busy_loop, ())
107+
108+
#stress_test_busy_loop_timer = lv.timer_create(stress_test_busy_loop, START_SPACING, None)
109+
#stress_test_busy_loop_timer.set_repeat_count(1)
100110

101-
stress_test_busy_loop_with_yield_timer = lv.timer_create(stress_test_busy_loop_with_yield, START_SPACING + TEST_DURATION + TEST_SPACING, None)
111+
stress_test_busy_loop_with_yield_timer = lv.timer_create(stress_test_busy_loop_with_yield_thread, TEST_DURATION * 2, None)
102112
stress_test_busy_loop_with_yield_timer.set_repeat_count(1)
113+
stress_test_busy_loop_with_yield_timer.set_auto_delete(False)
103114

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)
115+
#sha256_timer = lv.timer_create(stress_test_sha256, START_SPACING + 2 * TEST_DURATION + 2 * TEST_SPACING, None)
116+
#sha256_timer.set_repeat_count(1)
106117

0 commit comments

Comments
 (0)