|
|
| """
|
| Test script for the Energy & Memory RAM Optimization Environment.
|
| """
|
|
|
| import sys
|
| import os
|
|
|
|
|
| project_root = os.path.dirname(__file__)
|
| sys.path.insert(0, project_root)
|
|
|
|
|
| import types
|
| he_demo = types.ModuleType('he_demo')
|
|
|
|
|
| from models import EnergyOptimizationAction, EnergyOptimizationObservation, Task, TaskSummary
|
| he_demo.EnergyOptimizationAction = EnergyOptimizationAction
|
| he_demo.EnergyOptimizationObservation = EnergyOptimizationObservation
|
| he_demo.Task = Task
|
| he_demo.TaskSummary = TaskSummary
|
|
|
|
|
| sys.modules['he_demo'] = he_demo
|
| sys.modules['he_demo.models'] = he_demo
|
|
|
|
|
| from server.he_demo_environment import EnergyOptimizationEnvironment
|
|
|
| def test_environment():
|
| """Test the energy optimization environment."""
|
| print("Testing Energy & Memory RAM Optimization Environment")
|
| print("=" * 60)
|
|
|
|
|
| env = EnergyOptimizationEnvironment()
|
|
|
|
|
| print("\n1. Testing reset...")
|
| obs = env.reset()
|
| print(f"Initial RAM usage: {obs.ram_usage:.1f}%")
|
| print(f"Initial energy consumption: {obs.energy_consumption:.1f} kWh")
|
| print(f"Initial system load: {obs.system_load:.2f}")
|
| print(f"Current task: {obs.current_task.name if obs.current_task else 'None'}")
|
| print(f"Tasks completed: {obs.tasks_completed}")
|
|
|
|
|
| actions_to_test = [
|
| ("reduce_ram", 0.8),
|
| ("optimize_energy", 0.7),
|
| ("balance_resources", 0.6),
|
| ("monitor_system", 0.5)
|
| ]
|
|
|
| print("\n2. Testing actions...")
|
| for action_type, intensity in actions_to_test:
|
| action = EnergyOptimizationAction(action_type=action_type, intensity=intensity)
|
| obs = env.step(action)
|
|
|
| print(f"\nAction: {action_type} (intensity: {intensity})")
|
| print(f"RAM usage: {obs.ram_usage:.1f}%")
|
| print(f"Energy consumption: {obs.energy_consumption:.1f} kWh")
|
| print(f"System load: {obs.system_load:.2f}")
|
| print(f"Reward: {obs.reward:.2f}")
|
| print(f"Task progress: {obs.task_progress:.2f}")
|
| print(f"Efficiency score: {obs.efficiency_score:.2f}")
|
| print(f"Current task: {obs.current_task.name if obs.current_task else 'None'}")
|
| print(f"Tasks completed: {obs.tasks_completed}")
|
|
|
| if obs.done:
|
| print("Episode completed!")
|
| break
|
|
|
| print("\n3. Testing task progression...")
|
|
|
| obs = env.reset()
|
| steps = 0
|
| max_test_steps = 20
|
|
|
| while not obs.done and steps < max_test_steps:
|
|
|
| if steps % 2 == 0:
|
| action = EnergyOptimizationAction(action_type="reduce_ram", intensity=0.9)
|
| else:
|
| action = EnergyOptimizationAction(action_type="optimize_energy", intensity=0.8)
|
|
|
| obs = env.step(action)
|
| steps += 1
|
|
|
| print(f"Step {steps}: RAM={obs.ram_usage:.1f}%, Energy={obs.energy_consumption:.1f}kWh, Reward={obs.reward:.2f}")
|
|
|
| if obs.current_task and obs.task_progress >= 1.0:
|
| print(f"Task '{obs.current_task.name}' completed!")
|
| break
|
|
|
| print("\nTest completed successfully!")
|
| print(f"Final state: RAM={obs.ram_usage:.1f}%, Energy={obs.energy_consumption:.1f}kWh")
|
| print(f"Tasks completed: {len(obs.tasks_completed)}")
|
| print(f"Total steps: {steps}")
|
|
|
| if __name__ == "__main__":
|
| test_environment() |