Performance Monitoring
This document describes CPU tracking, performance metrics, and optimization techniques implemented in src/runtime/metrics/PerformanceTracker.ts and src/runtime/evaluation/SystemEvaluator.ts.
Overview
Screeps enforces strict CPU limits that throttle execution when exceeded. The performance monitoring system tracks CPU usage, identifies bottlenecks, and provides actionable alerts before problems occur.
CPU Architecture
CPU Allocation Model
Free Tier:
- Limit: 10 CPU per tick
- Bucket: 0-10,000 capacity
- Regeneration: +10 CPU per tick (up to limit)
Subscription Tier:
- Limit: 30+ CPU per tick (varies by account age)
- Bucket: 0-10,000 capacity
- Regeneration: +30+ CPU per tick
- Burst capacity: Can use more than limit if bucket available
CPU Bucket Mechanics
Bucket Behavior:
- Accumulates unused CPU each tick
- Max capacity: 10,000 CPU
- Can borrow from bucket to exceed limit
- Depletion triggers throttling warnings
Example Flow:
1 | Tick 1: Used 8 CPU, Limit 10 → +2 to bucket (bucket: 2) |
Performance Tracking Implementation
Per-Tick Measurement
Tracking Flow (executed every tick):
Begin Phase (
PerformanceTracker.begin()):1
2startCpu = Game.cpu.getUsed();
startTick = Game.time;Execute Phase (Kernel orchestration):
- Memory management
- Behavior execution
- System evaluation
End Phase (
PerformanceTracker.end()):1
2
3cpuUsed = Game.cpu.getUsed() - startCpu;
warnings = analyzeThresholds(cpuUsed);
snapshot = buildSnapshot(cpuUsed, warnings);
Performance Snapshot Schema
1 | interface PerformanceSnapshot { |
Performance Thresholds
Warning Thresholds (Configurable)
High CPU Usage (default: 80% of limit):
1 | if (cpuUsed > cpuLimit * 0.8) { |
Trigger Example:
- Limit: 10 CPU
- Threshold: 8 CPU
- Usage: 8.5 CPU → WARNING
Low Bucket (default: 500):
1 | if (cpuBucket < 500) { |
Trigger Example:
- Bucket: 450 CPU
- Threshold: 500 CPU → WARNING
Critical Thresholds (SystemEvaluator)
CPU Bucket Depletion (critical severity):
- Threshold: <500 bucket
- Impact: Emergency CPU bursts unavailable
- Recommendation: “Pause non-essential tasks to allow the bucket to recover.”
CPU Over-Limit (warning severity):
- Threshold: >80% of limit
- Impact: Bucket draining over time
- Recommendation: “Profile hot paths or reduce creep behaviors to stay within CPU limits.”
CPU Cost Breakdown
Per-Component Costs (Typical)
Kernel Operations:
1 | Memory Manager: ~0.1 CPU/tick |
Per-Creep Operations:
1 | Role validation: ~0.02 CPU/creep |
Spawn Operations:
1 | Population check: ~0.05 CPU/role |
Typical CPU Budgets
RCL 1 (3 creeps):
1 | Kernel: 0.3 CPU |
RCL 3 (8 creeps):
1 | Kernel: 0.3 CPU |
RCL 5 (15 creeps):
1 | Kernel: 0.3 CPU |
Performance Optimization Techniques
1. Pathfinding Optimization
Current Implementation: reusePath: 5
Cost Analysis:
- Fresh pathfinding: 0.5-2.0 CPU
- Cached path: 0.05-0.1 CPU
- Savings: 90-95%
Tuning Options:
1 | // Conservative (stable rooms) |
2. Task Execution Optimization
Batch Operations:
1 | // Bad: Individual finds per creep |
Savings: ~0.2 CPU per creep (for 5+ creeps in same room)
3. Memory Access Optimization
Cache Frequently Accessed Data:
1 | // Bad: Multiple memory reads |
Savings: ~0.05 CPU per creep
4. Conditional Execution
Skip Expensive Operations When Unnecessary:
1 | // Bad: Always check all spawns |
Savings: ~0.1-0.2 CPU per tick when spawning not needed
Performance Monitoring Procedures
Real-Time Monitoring (In Console)
Check Current CPU Usage:
1 | Game.cpu.getUsed(); // CPU used so far this tick |
Monitor Per-Creep Costs:
1 | // Measure specific creep CPU |
Bucket Trend Analysis:
1 | // Track bucket over time (run multiple times) |
Historical Analysis (Memory)
Review Last System Report:
1 | const report = Memory.systemReport; |
Calculate Average CPU (manual tracking):
1 | // Store snapshots in Memory (add to kernel) |
Performance Alerting
Automated Alerts (SystemEvaluator)
Alert Severity Levels:
Warning (yellow):
- CPU usage >80% of limit
- Bucket <2000 (trending concern)
- Low spawn throughput
Critical (red):
- CPU bucket <500
- No creeps in play
- Test failures detected
Alert Delivery
Console Logs:
1 | [evaluation] System stable: no anomalies detected. |
Memory Storage:
1 | Memory.systemReport = { |
External Monitoring (GitHub Actions):
screeps-monitoring.ymlpolls Memory every 30 minutes- Combines strategic analysis with PTR telemetry monitoring
- Sends push notifications for critical findings
- Tracks bucket trends over time
Performance Degradation Response
Level 1: Early Warning (CPU >80%)
Immediate Actions:
- Check bucket trend (increasing or decreasing?)
- Review recent code changes
- Profile creep operations
- No immediate action required if bucket stable
Preventive Actions:
- Increase pathfinding cache duration
- Optimize task execution logic
- Review for redundant operations
Level 2: Bucket Draining (Bucket <2000)
Immediate Actions:
- Reduce upgrader count by 1-2
- Increase
reusePathparameter to 10+ - Monitor bucket recovery
- Identify CPU spikes
Preventive Actions:
- Defer non-essential creeps
- Disable advanced features temporarily
- Focus on core operations only
Level 3: Critical (Bucket <500)
Immediate Actions:
- Emergency Mode: Disable all upgraders
- Reduce to minimum harvesters only
- Skip evaluation and logging
- Monitor every tick until recovery
Recovery Plan:
1 | // Emergency CPU reduction (in console) |
Resume Normal Operations:
- Wait for bucket >2000
- Gradually re-enable upgraders (1 at a time)
- Monitor bucket stability
- Investigate root cause
CPU Profiling Techniques
Manual Profiling
Profile Entire Tick:
1 | const start = Game.cpu.getUsed(); |
Profile Specific Operations:
1 | function profileOperation(name, operation) { |
Automated Profiling
Instrument Kernel (temporary for analysis):
1 | // Add to kernel.ts |
Performance Benchmarks
Target Performance Metrics
| Metric | Target | Warning | Critical |
|---|---|---|---|
| CPU/tick | <50% limit | >80% limit | >100% limit |
| CPU bucket | >5000 | <2000 | <500 |
| CPU/creep | <0.5 CPU | >1.0 CPU | >2.0 CPU |
| Kernel overhead | <0.5 CPU | >1.0 CPU | >2.0 CPU |
Expected Performance (By RCL)
| RCL | Creeps | CPU Used | % of 10 Limit | Bucket Trend | Status |
|---|---|---|---|---|---|
| 1 | 3 | 1.7 | 17% | Increasing | ✓ Excellent |
| 2 | 5 | 2.5 | 25% | Increasing | ✓ Good |
| 3 | 8 | 3.7 | 37% | Stable | ✓ Good |
| 4 | 12 | 5.5 | 55% | Stable | ✓ Acceptable |
| 5 | 15 | 6.6 | 66% | Stable | ⚠ Monitor |
| 6 | 18 | 8.5 | 85% | Decreasing | ⚠ Warning |
Note: Free tier (10 CPU limit) becomes constraining at RCL 5+. Subscription recommended for further growth.
Best Practices Summary
DO:
- ✓ Monitor CPU usage every tick
- ✓ Track bucket trends over time
- ✓ Profile new features before deployment
- ✓ Cache pathfinding results
- ✓ Optimize hot paths (operations in inner loops)
- ✓ Set conservative thresholds for alerts
DON’T:
- ✗ Ignore bucket drain warnings
- ✗ Add creeps without CPU budget
- ✗ Recalculate paths every tick
- ✗ Iterate Memory.creeps unnecessarily
- ✗ Run expensive operations every tick
MONITOR:
- ⚠ CPU usage trend (should be stable)
- ⚠ Bucket level (should stay >5000)
- ⚠ Per-creep CPU cost (should be <0.5)
- ⚠ Kernel overhead (should be <0.5)
Related Documentation
- Scaling Strategies - CPU budgets for different room counts
- Memory Management - Memory access optimization
- Creep Roles - Per-role CPU costs
- Stats Monitoring - External monitoring setup