|
26 | 26 | # 2097152 | 0 |
27 | 27 | #===================================== |
28 | 28 |
|
29 | | -appscreen = lv.screen_active() |
30 | | - |
31 | 29 | import gc |
32 | 30 | import time |
| 31 | +import _thread |
33 | 32 |
|
34 | 33 | # Configuration |
35 | 34 | ALLOCATION_TIMEOUT_MS = 100 # Timeout for a single allocation (in milliseconds) |
@@ -59,56 +58,58 @@ def test_allocation(buffer_size, n): |
59 | 58 | # Print progress every 100 allocations to avoid flooding serial |
60 | 59 | if count % 100 == 0: |
61 | 60 | print(f"Allocated {count} buffers of {buffer_size} bytes", end="\r") |
62 | | - except MemoryError: |
| 61 | + except MemoryError as e: |
| 62 | + buffers.clear() |
63 | 63 | print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: MemoryError") |
64 | 64 | except Exception as e: |
| 65 | + buffers.clear() |
65 | 66 | print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: {e}") |
66 | 67 |
|
67 | 68 | # Free allocated buffers |
68 | | - buffers.clear() |
| 69 | + #buffers.clear() |
69 | 70 | gc.collect() |
70 | 71 | return count |
71 | 72 |
|
72 | 73 |
|
73 | | -# Test buffer sizes of 2^n, starting from n=1 (2 bytes) |
74 | | -n = 1 |
75 | 74 |
|
| 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() |
76 | 109 | status = lv.label(appscreen) |
77 | 110 | status.align(lv.ALIGN.TOP_LEFT, 5, 10) |
78 | 111 | status.set_style_text_color(lv.color_hex(0xFFFFFF), 0) |
79 | 112 | status.set_style_text_font(lv.font_unscii_8, 0) |
80 | 113 |
|
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