Performance Optimizer Script

The Validator Performance Optimizer Script is a tool developed by Forest Staking to monitor your Berachain validator node's performance and ensure its efficiency.


Validator Performance Optimizer Script

Overview

The Validator Performance Optimizer Script is a tool developed by Forest Staking to monitor your Berachain validator node's performance and ensure its efficiency. This script monitors key system metrics (CPU, memory, and missed blocks) and provides recommendations or takes actions to optimize the performance of your validator.


Key Features

  • CPU & Memory Monitoring: Automatically checks your node's CPU and memory usage.

  • Missed Blocks Detection: Queries your Prometheus server for missed blocks.

  • Threshold-Based Alerts: Suggests optimizations when usage exceeds defined thresholds.

  • Auto-Restart: Automatically restarts the validator if critical resource usage thresholds are breached.

  • Customizable: Adjust thresholds to suit your specific requirements.


Prerequisites

Before running the script, ensure the following:

  1. Prometheus is set up and monitoring your validator's metrics.

  2. Your server can run Python 3.

  3. Your validator node is managed by systemd (or adjust the restart command accordingly).


Installation & Setup

Step 1: Install Dependencies

Ensure Python 3 is installed on your machine. You can install the required Python package by running:

bashCopy codepip install requests

Step 2: Prometheus Setup

Make sure you have Prometheus installed and scraping the following metrics for your Berachain node:

  • CPU usage

  • Memory usage

  • Missed blocks (berachain_missed_blocks)

Set up Prometheus as per Forest Staking's Prometheus Setup Guide. Ensure it exposes an API endpoint to query metrics.


Script: Validator Performance Optimizer

Copy and paste the following Python script to your server.

pythonCopy codeimport os
import subprocess
import requests

# Prometheus API endpoint for querying validator metrics
PROMETHEUS_URL = 'http://localhost:9090/api/v1/query'

# Thresholds for optimizations
CPU_THRESHOLD = 80  # CPU usage percentage threshold
MEMORY_THRESHOLD = 70  # Memory usage percentage threshold
MISSED_BLOCKS_THRESHOLD = 10  # Missed blocks threshold

# Function to get metrics from Prometheus
def get_prometheus_metric(query):
    try:
        response = requests.get(f'{PROMETHEUS_URL}?query={query}')
        results = response.json()['data']['result']
        return float(results[0]['value'][1]) if results else None
    except Exception as e:
        print(f"Error fetching Prometheus metrics: {e}")
        return None

# Function to get CPU usage from system
def get_cpu_usage():
    try:
        # This assumes you're using Linux and can use top/htop commands
        cpu_usage = subprocess.check_output("top -bn1 | grep 'Cpu(s)' | awk '{print $2}'", shell=True)
        return float(cpu_usage)
    except Exception as e:
        print(f"Error fetching CPU usage: {e}")
        return None

# Function to get memory usage from system
def get_memory_usage():
    try:
        # This assumes you're using Linux and can use free command
        mem_info = subprocess.check_output("free -m", shell=True).decode('utf-8').splitlines()[1].split()
        total_mem = int(mem_info[1])
        used_mem = int(mem_info[2])
        return (used_mem / total_mem) * 100  # Return memory usage percentage
    except Exception as e:
        print(f"Error fetching memory usage: {e}")
        return None

# Function to check missed blocks from Prometheus
def check_missed_blocks():
    return get_prometheus_metric('berachain_missed_blocks')

# Function to optimize validator performance
def optimize_performance():
    cpu_usage = get_cpu_usage()
    memory_usage = get_memory_usage()
    missed_blocks = check_missed_blocks()

    # Suggest optimizations or make adjustments based on thresholds
    if cpu_usage and cpu_usage > CPU_THRESHOLD:
        print(f"High CPU usage detected: {cpu_usage}%")
        print("Recommendation: Increase CPU resources or optimize node configuration.")
    
    if memory_usage and memory_usage > MEMORY_THRESHOLD:
        print(f"High memory usage detected: {memory_usage}%")
        print("Recommendation: Increase RAM or optimize node memory usage.")

    if missed_blocks and missed_blocks > MISSED_BLOCKS_THRESHOLD:
        print(f"Missed blocks detected: {missed_blocks} blocks missed.")
        print("Recommendation: Investigate node performance or network connection.")

# Optional: Function to automatically restart the validator node if critical thresholds are breached
def restart_validator():
    print("Restarting validator due to critical performance issues...")
    os.system("sudo systemctl restart berachaind")

# Run performance optimization and make adjustments if needed
def main():
    print("Starting validator performance optimizer...")
    
    cpu_usage = get_cpu_usage()
    memory_usage = get_memory_usage()
    missed_blocks = check_missed_blocks()

    print(f"Current CPU Usage: {cpu_usage}%")
    print(f"Current Memory Usage: {memory_usage}%")
    print(f"Missed Blocks: {missed_blocks}")

    optimize_performance()

    # Restart validator if CPU or memory usage is critically high
    if (cpu_usage and cpu_usage > 90) or (memory_usage and memory_usage > 90):
        restart_validator()

if __name__ == "__main__":
    main()

Step 3: Configure Script

You can adjust the following parameters based on your preferences:

  • CPU_THRESHOLD: The maximum CPU usage percentage before triggering an optimization alert.

  • MEMORY_THRESHOLD: The maximum memory usage percentage before triggering an alert.

  • MISSED_BLOCKS_THRESHOLD: The number of missed blocks that will trigger an alert.


Running the Script

To run the performance optimizer script, follow these steps:

  1. Ensure you are in the directory where the script is saved.

  2. Run the script using Python 3:

    bashCopy codepython3 validator_optimizer.py

The script will now monitor your validator’s performance and provide optimizations or restart the validator if thresholds are breached.


Future Enhancements

  • Alerts & Notifications: Integrate the script with Telegram, Discord, or PagerDuty for real-time notifications.

  • Performance Dashboard: Combine this script with Prometheus and Grafana for a visual performance dashboard.

  • Expanded Metrics: Add monitoring for additional metrics such as disk usage, network traffic, and staking rewards.

This script will be continuously improved by Forest Staking to ensure the best performance for validators operating on Berachain and other networks.


Last updated

Was this helpful?