Back to Repositories

Testing System Statistics Monitoring Implementation in OpenHands

This test suite validates the system statistics monitoring functionality in OpenHands, focusing on CPU, memory, disk, and I/O metrics collection. The tests ensure accurate system resource tracking and data consistency across multiple measurements.

Test Coverage Overview

The test suite provides comprehensive coverage of system statistics gathering functionality.

Key areas tested include:
  • CPU utilization percentage validation
  • Memory statistics (RSS, VMS, usage percentage)
  • Disk metrics (total, used, free space, usage percentage)
  • I/O operations (read/write bytes)
Edge cases include boundary checking for percentage values and verification of disk space calculations.

Implementation Analysis

The testing approach employs systematic validation of system metrics using the psutil library integration. The implementation uses type checking and value range verification to ensure data integrity.

Key patterns include:
  • Dictionary structure validation
  • Data type assertions
  • Range boundary testing
  • Multiple execution stability checks

Technical Details

Testing tools and components:
  • pytest framework for test execution
  • psutil library for system metrics collection
  • Python’s assert statements for validation
  • Dictionary-based data structure validation
Configuration includes CPU count awareness and filesystem overhead considerations.

Best Practices Demonstrated

The test suite exemplifies robust testing practices through systematic validation of system metrics.

Notable practices include:
  • Comprehensive type checking
  • Boundary value testing
  • Multiple execution verification
  • Logical relationship validation
  • Modular test organization

all-hands-ai/openhands

tests/runtime/utils/test_system_stats.py

            
"""Tests for system stats utilities."""

import psutil

from openhands.runtime.utils.system_stats import get_system_stats


def test_get_system_stats():
    """Test that get_system_stats returns valid system statistics."""
    stats = get_system_stats()

    # Test structure
    assert isinstance(stats, dict)
    assert set(stats.keys()) == {'cpu_percent', 'memory', 'disk', 'io'}

    # Test CPU stats
    assert isinstance(stats['cpu_percent'], float)
    assert 0 <= stats['cpu_percent'] <= 100 * psutil.cpu_count()

    # Test memory stats
    assert isinstance(stats['memory'], dict)
    assert set(stats['memory'].keys()) == {'rss', 'vms', 'percent'}
    assert isinstance(stats['memory']['rss'], int)
    assert isinstance(stats['memory']['vms'], int)
    assert isinstance(stats['memory']['percent'], float)
    assert stats['memory']['rss'] > 0
    assert stats['memory']['vms'] > 0
    assert 0 <= stats['memory']['percent'] <= 100

    # Test disk stats
    assert isinstance(stats['disk'], dict)
    assert set(stats['disk'].keys()) == {'total', 'used', 'free', 'percent'}
    assert isinstance(stats['disk']['total'], int)
    assert isinstance(stats['disk']['used'], int)
    assert isinstance(stats['disk']['free'], int)
    assert isinstance(stats['disk']['percent'], float)
    assert stats['disk']['total'] > 0
    assert stats['disk']['used'] >= 0
    assert stats['disk']['free'] >= 0
    assert 0 <= stats['disk']['percent'] <= 100
    # Verify that used + free is less than or equal to total
    # (might not be exactly equal due to filesystem overhead)
    assert stats['disk']['used'] + stats['disk']['free'] <= stats['disk']['total']

    # Test I/O stats
    assert isinstance(stats['io'], dict)
    assert set(stats['io'].keys()) == {'read_bytes', 'write_bytes'}
    assert isinstance(stats['io']['read_bytes'], int)
    assert isinstance(stats['io']['write_bytes'], int)
    assert stats['io']['read_bytes'] >= 0
    assert stats['io']['write_bytes'] >= 0


def test_get_system_stats_stability():
    """Test that get_system_stats can be called multiple times without errors."""
    # Call multiple times to ensure stability
    for _ in range(3):
        stats = get_system_stats()
        assert isinstance(stats, dict)
        assert stats['cpu_percent'] >= 0