Untitled conversation
- Userpython
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import psutil
import curses
import time
def draw_progress_bar(stdscr, progress, total, height, width, y, x):
"""Draw a progress bar on the screen."""
bar_length = width - 2 # Length of the bar excluding brackets
filled_length = int(bar_length * progress // total)
bar = f"[{'=' * filled_length}{' ' * (bar_length - filled_length)}]"
stdscr.addstr(y, x, bar)
def benchmark_ram(stdscr, target_usage):
allocated_memory = []
total_allocated = 0
stdscr.nodelay(1) # Make getch() non-blocking
stdscr.clear()
while True:
# Get current RAM usage and CPU usage
current_memory = psutil.virtual_memory()
current_usage = current_memory.percent
total_memory = current_memory.total / (1024 ** 3) # Convert bytes to GB
used_memory = total_memory * current_usage / 100 # Calculate used memory in GB
current_cpu_usage = psutil.cpu_percent()
# Update RAM allocation
if current_usage < target_usage:
try:
# Allocate 100 MB
allocated_memory.append(bytearray(100 * 1024 * 1024)) # 100 MB
total_allocated += 0.1 # Track total allocated in GB
allocation_progress = total_allocated * 100 / (total_memory - used_memory)
except MemoryError:
stdscr.addstr(1, 0, "Memory allocation failed. Check your RAM usage.")
stdscr.refresh()
break
except KeyboardInterrupt:
print("Exited.")
else:
stdscr.addstr(1, 0, "Target RAM usage reached. Press 'Ctrl+C' to exit.")
stdscr.refresh()
stdscr.getch() # Wait for user input
break
# Clear the screen and draw the UI
stdscr.clear()
# Display RAM usage
draw_progress_bar(stdscr, used_memory, total_memory, height=1, width=30, y=0, x=0)
stdscr.addstr(0, 35, f"{used_memory:.1f}/{total_memory:.1f} GB - {current_usage:.1f}% used")
# Display RAM allocation progress
draw_progress_bar(stdscr, total_allocated, total_memory, height=1, width=30, y=1, x=0)
stdscr.addstr(1, 35, f"Allocating {total_allocated:.1f} GB RAM, {allocation_progress:.1f}% done")
# Display CPU usage
draw_progress_bar(stdscr, current_cpu_usage, 100, height=1, width=30, y=2, x=0)
stdscr.addstr(2, 35, f"{current_cpu_usage:.1f}% CPU usage")
stdscr.addstr(4, 0, "Press 'Ctrl+C' to quit.")
stdscr.refresh()
time.sleep(0.5) # Delay before the next update
if __name__ == "__main__":
target_usage = 95
curses.wrapper(benchmark_ram, target_usage)```
New UI:
initial:
RAM Filler
> [start]
> settings
> quit
control with arrow keys
filling:
[+-+ 3% ] allocation progress
[+-+-+ 50% -+ ] RAM usage
[+-+ 10% ] CPU usage
press C to go back, Ctrl+C to quit
settings:
> fill usage: [95%]
> display unit: [percent] GB
> quit on system slowdown (RAM=99%): [yes] no
control with arrow keys
Certainly! Below is an enhanced version of your RAM benchmarking tool with the new UI specifications you've outlined. This version includes a navigable menu with arrow keys, settings configuration, and a more detailed filling screen.
Main Menu:
Filling Screen:
C to go back or Ctrl+C to quit.Settings Menu:
import psutil
import curses
import time
# Constants for menu navigation
KEY_UP = curses.KEY_UP
KEY_DOWN = curses.KEY_DOWN
KEY_ENTER = 10
KEY_ESC = 27
KEY_C = ord('c')
# Default settings
settings = {
'fill_usage': 95, # in percent
'display_unit': 'percent', # or 'GB'
'quit_on_slowdown': True,
'slowdown_threshold': 99 # in percent
}
def draw_progress_bar(stdscr, y, x, width, label, progress, max_progress):
bar_length = width - len(label) - 10 # Adjust bar length based on label
filled_length = int(bar_length * progress // max_progress)
bar = f"[{'=' * filled_length}{' ' * (bar_length - filled_length)}]"
stdscr.addstr(y, x, f"{label}: {bar} {progress:.1f}%")
def main_menu(stdscr):
curses.curs_set(0) # Hide cursor
stdscr.nodelay(False)
stdscr.clear()
menu = ['RAM Filler', 'Settings', 'Quit']
current_row = 0
while True:
stdscr.clear()
height, width = stdscr.getmaxyx()
title = "RAM Benchmark Tool"
stdscr.addstr(1, width//2 - len(title)//2, title, curses.A_BOLD | curses.A_UNDERLINE)
for idx, row in enumerate(menu):
x = width//2 - 10
y = height//2 - len(menu)//2 + idx
if idx == current_row:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, f"> {row}")
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, f" {row}")
stdscr.refresh()
key = stdscr.getch()
if key == KEY_UP and current_row > 0:
current_row -= 1
elif key == KEY_DOWN and current_row < len(menu)-1:
current_row += 1
elif key in [KEY_ENTER, ord('\n')]:
if menu[current_row] == 'RAM Filler':
benchmark_ram(stdscr)
elif menu[current_row] == 'Settings':
settings_menu(stdscr)
elif menu[current_row] == 'Quit':
break
def settings_menu(stdscr):
curses.curs_set(0) # Hide cursor
stdscr.nodelay(False)
stdscr.clear()
menu = ['Fill Usage', 'Display Unit', 'Quit on System Slowdown', 'Back']
current_row = 0
while True:
stdscr.clear()
height, width = stdscr.getmaxyx()
title = "Settings"
stdscr.addstr(1, width//2 - len(title)//2, title, curses.A_BOLD | curses.A_UNDERLINE)
for idx, row in enumerate(menu):
x = width//2 - 20
y = height//2 - len(menu)//2 + idx
display_text = row
if row == 'Fill Usage':
display_text += f": {settings['fill_usage']}%"
elif row == 'Display Unit':
display_text += f": {settings['display_unit']}"
elif row == 'Quit on System Slowdown':
display_text += f": {'Yes' if settings['quit_on_slowdown'] else 'No'}"
if idx == current_row:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, f"> {display_text}")
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, f" {display_text}")
stdscr.refresh()
key = stdscr.getch()
if key == KEY_UP and current_row > 0:
current_row -= 1
elif key == KEY_DOWN and current_row < len(menu)-1:
current_row += 1
elif key in [KEY_ENTER, ord('\n')]:
selected = menu[current_row]
if selected == 'Fill Usage':
settings['fill_usage'] = get_user_input(stdscr, "Set Fill Usage (%)", settings['fill_usage'], 0, 100)
elif selected == 'Display Unit':
settings['display_unit'] = toggle_option(stdscr, "Display Unit", settings['display_unit'], ['percent', 'GB'])
elif selected == 'Quit on System Slowdown':
settings['quit_on_slowdown'] = toggle_option(stdscr, "Quit on Slowdown", settings['quit_on_slowdown'], ['Yes', 'No'])
elif selected == 'Back':
break
def get_user_input(stdscr, prompt, current_value, min_val, max_val):
curses.echo()
while True:
stdscr.clear()
height, width = stdscr.getmaxyx()
stdscr.addstr(height//2 -1, width//2 - len(prompt)//2, prompt)
stdscr.addstr(height//2, width//2 - 5, f"{current_value}")
stdscr.refresh()
input_str = stdscr.getstr(height//2, width//2 - 5, 5).decode('utf-8')
try:
val = float(input_str)
if min_val <= val <= max_val:
curses.noecho()
return val
except ValueError:
pass
stdscr.addstr(height//2 +2, width//2 -10, "Invalid input. Press any key to retry.")
stdscr.getch()
curses.noecho()
def toggle_option(stdscr, prompt, current_value, options):
index = options.index(str(current_value)) if str(current_value) in options else 0
while True:
stdscr.clear()
height, width = stdscr.getmaxyx()
stdscr.addstr(height//2 -1, width//2 - len(prompt)//2, f"{prompt}:")
for idx, option in enumerate(options):
x = width//2 - 5
y = height//2 + idx
if idx == index:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, f"> {option}")
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, f" {option}")
stdscr.refresh()
key = stdscr.getch()
if key == KEY_UP and index > 0:
index -= 1
elif key == KEY_DOWN and index < len(options)-1:
index += 1
elif key in [KEY_ENTER, ord('\n')]:
return options[index]
elif key == KEY_ESC:
return current_value
def benchmark_ram(stdscr):
allocated_memory = []
total_allocated = 0
stdscr.nodelay(True) # Make getch() non-blocking
stdscr.clear()
height, width = stdscr.getmaxyx()
try:
while True:
# Get current RAM usage and CPU usage
current_memory = psutil.virtual_memory()
current_usage = current_memory.percent
total_memory_gb = current_memory.total / (1024 ** 3) # Convert bytes to GB
used_memory_gb = total_memory_gb * current_usage / 100 # Calculate used memory in GB
current_cpu_usage = psutil.cpu_percent(interval=None)
# Update RAM allocation
if current_usage < settings['fill_usage']:
try:
# Allocate 100 MB
allocated_memory.append(bytearray(100 * 1024 * 1024)) # 100 MB
total_allocated += 0.1 # Track total allocated in GB
allocation_progress = (total_allocated / (total_memory_gb - used_memory_gb)) * 100 if (total_memory_gb - used_memory_gb) > 0 else 100
except MemoryError:
stdscr.addstr(height//2, width//2 - 20, "Memory allocation failed. Press any key to continue.")
stdscr.refresh()
stdscr.getch()
break
else:
stdscr.addstr(height//2, width//2 - 20, "Target RAM usage reached. Press 'C' to go back.")
stdscr.refresh()
key = stdscr.getch()
if key in [KEY_C, KEY_ENTER, ord('\n')]:
break
# Check for system slowdown
if settings['quit_on_slowdown'] and current_usage >= settings['slowdown_threshold']:
break
# Clear the screen and draw the UI
stdscr.clear()
# Display Allocation Progress
draw_progress_bar(stdscr, 2, 2, width - 4, "Allocation", total_allocated, total_memory_gb)
stdscr.addstr(2, width - 35, f"{total_allocated:.1f} GB - {allocation_progress:.1f}% done")
# Display RAM usage
ram_label = "RAM Usage"
ram_progress = current_usage
draw_progress_bar(stdscr, 4, 2, width - 4, ram_label, ram_progress, 100)
if settings['display_unit'] == 'percent':
stdscr.addstr(4, width - 35, f"{current_usage:.1f}% used")
else:
stdscr.addstr(4, width - 35, f"{used_memory_gb:.1f}/{total_memory_gb:.1f} GB used")
# Display CPU usage
draw_progress_bar(stdscr, 6, 2, width - 4, "CPU Usage", current_cpu_usage, 100)
stdscr.addstr(6, width - 20, f"{current_cpu_usage:.1f}% CPU")
stdscr.addstr(height - 2, 2, "Press 'C' to go back, Ctrl+C to quit.")
stdscr.refresh()
# Handle user input
try:
key = stdscr.getch()
if key == KEY_C:
break
except KeyboardInterrupt:
break
time.sleep(0.5) # Delay before the next update
except KeyboardInterrupt:
pass
finally:
allocated_memory.clear() # Free allocated memory
def setup_colors():
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
def run_curses_app():
curses.wrapper(inner_run)
def inner_run(stdscr):
setup_colors()
main_menu(stdscr)
if __name__ == "__main__":
run_curses_app()
Menu Navigation:
KEY_UP, KEY_DOWN) to navigate between menu options.main_menu function handles the main interface with options: RAM Filler, Settings, and Quit.settings_menu allows users to adjust settings like fill usage, display unit, and whether to quit on system slowdown.Settings Configuration:
Filling Screen Enhancements:
C to go back or Ctrl+C to quit.Utility Functions:
get_user_input: Facilitates user input for numerical settings.toggle_option: Allows toggling between different options like yes/no or different display units.draw_progress_bar: A reusable function to draw labeled progress bars.Color Scheme:
Launching the Application:
python3 ram_benchmark.py
Navigating Menus:
Adjusting Settings:
Running the RAM Filler:
Ensure you have the necessary Python packages installed. You can install psutil using pip if it's not already installed:
pip install psutil
This enhanced version provides a more user-friendly and interactive experience for benchmarking RAM usage. It allows users to customize settings to suit their specific needs and offers clear visual feedback during the benchmarking process.
Feel free to further customize the UI, add more features, or adjust settings as per your requirements!
Not yet included in a published release.