Add CPU and mem test

This commit is contained in:
Thomas Farstrike
2025-04-22 13:28:49 +02:00
parent 8bc4da10ea
commit 63cfeb8b2e
8 changed files with 181 additions and 4 deletions
@@ -0,0 +1,2 @@
Manifest-Version: 1.0
Name: CPUTester
+83
View File
@@ -0,0 +1,83 @@
import time
import hashlib
import os
# Configuration
TEST_DURATION = 5 # Duration of each test in seconds
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():
print("\nStarting busy loop stress test...")
iterations = 0
start_time = time.ticks_ms()
end_time = start_time + (TEST_DURATION * 1000)
while time.ticks_ms() < end_time:
iterations += 1
duration_ms = time.ticks_diff(time.ticks_ms(), start_time)
iterations_per_second = (iterations / duration_ms) * 1000
print(f"Busy loop test completed: {iterations_per_second:.2f} iterations/second")
return iterations_per_second
def stress_test_busy_loop_with_yield():
print("\nStarting busy loop with yield (sleep_ms(0)) stress test...")
iterations = 0
start_time = time.ticks_ms()
end_time = start_time + (TEST_DURATION * 1000)
while time.ticks_ms() < end_time:
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")
return iterations_per_second
def stress_test_sha256():
print("\nStarting SHA-256 stress test (1KB data)...")
iterations = 0
start_time = time.ticks_ms()
end_time = start_time + (TEST_DURATION * 1000)
while time.ticks_ms() < end_time:
hashlib.sha256(DATA).digest() # Compute SHA-256 on 1KB data
iterations += 1
duration_ms = time.ticks_diff(time.ticks_ms(), start_time)
iterations_per_second = (iterations / duration_ms) * 1000
print(f"SHA-256 test completed: {iterations_per_second:.2f} iterations/second")
return iterations_per_second
def main():
print("Starting CPU stress tests...")
# Run busy loop test
busy_loop_ips = stress_test_busy_loop()
# Small delay to stabilize system
time.sleep_ms(500)
# Run busy loop with yield test
yield_loop_ips = stress_test_busy_loop_with_yield()
# Small delay to stabilize system
time.sleep_ms(500)
# Run SHA-256 test
sha256_ips = stress_test_sha256()
# Summary
print("\nTest Summary:")
print(f"Busy loop: {busy_loop_ips:.2f} iterations/second")
print(f"Busy loop with yield: {yield_loop_ips:.2f} iterations/second")
print(f"SHA-256 (1KB): {sha256_ips:.2f} iterations/second")
print("All tests completed.")
try:
main()
except Exception as e:
print(f"Error during tests: {e}")
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

@@ -0,0 +1,2 @@
Manifest-Version: 1.0
Name: MemTest
+83
View File
@@ -0,0 +1,83 @@
import gc
import time
# Configuration
ALLOCATION_TIMEOUT_MS = 100 # Timeout for a single allocation (in milliseconds)
def test_allocation(buffer_size, n):
"""Test how many buffers of a given size can be allocated with a timeout."""
print(f"\nTesting buffer size: {buffer_size} bytes (2^{n})")
buffers = []
count = 0
try:
while True:
# Measure time for allocation
start_time = time.ticks_ms()
# Allocate a new buffer
buffer = bytearray(buffer_size)
allocation_time = time.ticks_diff(time.ticks_ms(), start_time)
# Check if allocation took too long
if allocation_time > ALLOCATION_TIMEOUT_MS:
print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: Allocation timeout ({allocation_time}ms > {ALLOCATION_TIMEOUT_MS}ms)")
break
buffers.append(buffer)
count += 1
# Print progress every 100 allocations to avoid flooding serial
if count % 100 == 0:
print(f"Allocated {count} buffers of {buffer_size} bytes", end="\r")
except MemoryError:
print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: MemoryError")
except Exception as e:
print(f"\nStopped after allocating {count} buffers of {buffer_size} bytes: {e}")
# Free allocated buffers
buffers.clear()
gc.collect()
return count
def main():
print("Starting memory allocation test...")
# Store results for summary
results = []
# Test buffer sizes of 2^n, starting from n=1 (2 bytes)
n = 1
while True:
buffer_size = 2 ** n
# Run allocation test
max_buffers = test_allocation(buffer_size, n)
results.append((buffer_size, max_buffers))
# Check if we allocated 0 buffers (indicates we can't allocate this size)
if max_buffers == 0:
print(f"Cannot allocate buffers of size {buffer_size} bytes. Stopping test.")
break
# Clean up memory before next test
gc.collect()
time.sleep_ms(100) # Brief delay to stabilize system
n += 1
# Print summary report
print("\n=== Memory Allocation Test Summary ===")
print("Buffer Size (bytes) | Max Buffers Allocated")
print("-" * 40)
for buffer_size, max_buffers in results:
print(f"{buffer_size:>18} | {max_buffers:>20}")
print("=====================================")
print("Test completed.")
if __name__ == "__main__":
try:
# Run garbage collection before starting to ensure clean state
gc.collect()
main()
except Exception as e:
print(f"Error during test: {e}")
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

+9 -2
View File
@@ -1,3 +1,11 @@
# Maximum threads with stack size 1024: 48
# Maximum threads with stack size 2048: 48
# Maximum threads with stack size 4096: 48
# Maximum threads with stack size 8192: 22
# Maximum threads with stack size 16386: 10
# Maximum threads with stack size 32768: 3
# Maximum threads with stack size 65536: 1
import _thread
import time
@@ -65,7 +73,7 @@ def main():
wifi_icon = lv.label(lv.screen_active())
wifi_icon.set_text("Test label")
wifi_icon.align(lv.ALIGN.CENTER, 0, 0)
wifi_icon.set_style_text_color(COLOR_TEXT_WHITE, 0)
wifi_icon.set_style_text_color(lv.color_hex(0x0000FF), 0)
print("done!")
@@ -76,4 +84,3 @@ if __name__ == "__main__":
except Exception as e:
print(f"Error in main: {e}")
main()
+2 -2
View File
@@ -230,8 +230,8 @@ def execute_script(script_source, is_file, lvgl_obj, return_to_launcher):
script_globals = {
'lv': lv,
'subwindow': lvgl_obj,
'run_app': run_app
#'run_launcher' : run_launcher
'run_app': run_app,
'__name__': "__main__"
}
if is_file:
print(f"Thread {thread_id}: reading script from file: {script_source}")