Skip to content

Commit 87eb030

Browse files
memtest: fix on desktop
1 parent 871a203 commit 87eb030

1 file changed

Lines changed: 41 additions & 40 deletions

File tree

  • internal_filesystem/apps/com.example.memtest/assets

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

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
# 2097152 | 0
2727
#=====================================
2828

29-
appscreen = lv.screen_active()
30-
3129
import gc
3230
import time
31+
import _thread
3332

3433
# Configuration
3534
ALLOCATION_TIMEOUT_MS = 100 # Timeout for a single allocation (in milliseconds)
@@ -59,56 +58,58 @@ def test_allocation(buffer_size, n):
5958
# Print progress every 100 allocations to avoid flooding serial
6059
if count % 100 == 0:
6160
print(f"Allocated {count} buffers of {buffer_size} bytes", end="\r")
62-
except MemoryError:
61+
except MemoryError as e:
62+
buffers.clear()
6363
print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: MemoryError")
6464
except Exception as e:
65+
buffers.clear()
6566
print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: {e}")
6667

6768
# Free allocated buffers
68-
buffers.clear()
69+
#buffers.clear()
6970
gc.collect()
7071
return count
7172

7273

73-
# Test buffer sizes of 2^n, starting from n=1 (2 bytes)
74-
n = 1
7574

75+
76+
def stress_test_thread():
77+
summary = "Running RAM memory tests...\n\n"
78+
summary += "Buffer Size (bytes) | Max Allocated\n"
79+
summary += "-----------------------------------\n"
80+
lv.async_call(lambda l: status.set_text(summary), None)
81+
# Test buffer sizes of 2^n, starting from n=1 (2 bytes)
82+
n = 1
83+
while appscreen == lv.screen_active():
84+
buffer_size = 2 ** n
85+
summary += f"{buffer_size:>12} | "
86+
#lv.async_call(lambda l: status.set_text(summary), None)
87+
# Run allocation test
88+
gc.collect()
89+
max_buffers = test_allocation(buffer_size, n)
90+
# Check if we allocated 0 buffers (indicates we can't allocate this size)
91+
if max_buffers == 0:
92+
print(f"Cannot allocate buffers of size {buffer_size} bytes. Stopping test.")
93+
summary += f"{max_buffers:>14}\n"
94+
#lv.async_call(lambda l: status.set_text(summary), None)
95+
break
96+
# Clean up memory before next test
97+
gc.collect()
98+
n += 1
99+
summary += f"{max_buffers:>14}\n"
100+
lv.async_call(lambda l: status.set_text(summary), None)
101+
time.sleep_ms(200) # Brief delay to stabilize system
102+
# Print summary report
103+
summary += "\n"
104+
summary += "===================================\n"
105+
summary += "Test completed.\n"
106+
lv.async_call(lambda l: status.set_text(summary), None)
107+
108+
appscreen = lv.screen_active()
76109
status = lv.label(appscreen)
77110
status.align(lv.ALIGN.TOP_LEFT, 5, 10)
78111
status.set_style_text_color(lv.color_hex(0xFFFFFF), 0)
79112
status.set_style_text_font(lv.font_unscii_8, 0)
80113

81-
summary = "Running RAM memory tests...\n\n"
82-
summary += "Buffer Size (bytes) | Max Allocated\n"
83-
summary += "-----------------------------------\n"
84-
status.set_text(summary)
85-
86-
while appscreen == lv.screen_active():
87-
buffer_size = 2 ** n
88-
summary += f"{buffer_size:>12} | "
89-
status.set_text(summary)
90-
# Run allocation test
91-
gc.collect()
92-
max_buffers = test_allocation(buffer_size, n)
93-
# Check if we allocated 0 buffers (indicates we can't allocate this size)
94-
if max_buffers == 0:
95-
print(f"Cannot allocate buffers of size {buffer_size} bytes. Stopping test.")
96-
summary += f"{max_buffers:>14}\n"
97-
status.set_text(summary)
98-
break
99-
# Clean up memory before next test
100-
gc.collect()
101-
time.sleep_ms(100) # Brief delay to stabilize system
102-
n += 1
103-
summary += f"{max_buffers:>14}\n"
104-
status.set_text(summary)
105-
106-
# Print summary report
107-
summary += "\n"
108-
summary += "===================================\n"
109-
summary += "Test completed.\n"
110-
status.set_text(summary)
111-
112-
# Wait until the user stops the app
113-
while appscreen == lv.screen_active():
114-
time.sleep(1)
114+
_thread.stack_size(12*1024)
115+
_thread.start_new_thread(stress_test_thread, ())

0 commit comments

Comments
 (0)