Benchmarker is a real-time monitoring tool designed to track network bandwidth and traffic metrics for Docker containers within a specific Docker Compose stack or Swarm deployment.
- Real-time Network Monitoring: Continuously monitors network statistics (RX/TX bytes and packets) for Docker containers
- Docker Stack Integration: Automatically discovers and monitors containers within a specific Docker Compose project or Docker Swarm stack
- Configurable Monitoring: Customizable monitoring intervals and duration
- Structured Logging: Comprehensive logging with logrus structured format for easy parsing and analysis
- Graceful Shutdown: Supports both manual interruption (Ctrl+C) and automatic timeout-based shutdown
- Bandwidth Calculation: Real-time calculation of bytes/packets per second with period tracking
The application follows a clean architecture with separation of concerns:
cmd/benchmarker/ # Application entry point
├── main.go # Main application logic
internal/
├── containers/ # Docker container discovery
│ └── containers.go # Container listing and filtering
├── monitor/ # Core monitoring service
│ └── monitor.go # Monitoring orchestration
├── stats/ # Network statistics collection
│ ├── bandwidth.go # Bandwidth calculation logic
│ └── network.go # Docker API network stats collection
└── utils/
├── config/ # Configuration management
│ └── config.go # Environment-based configuration
└── logger/ # Logging utilities
└── logger.go # Structured logging setup
configs/ # Configuration files
└── .env # Environment variables
logs/ # Log output directory
├── benchmarker.log # Main application logs
└── monitor.log # Monitoring-specific logs
- Go 1.24.1 or later
- Docker Engine with API access
- Docker Compose (if monitoring compose stacks)
# Clone the repository
git clone https://github.com/pallandos/benchmarker.git
cd benchmarker
# Build the application
make build
# Or build manually
go build -o bin/benchmarker cmd/benchmarker/main.goThe application uses the following key dependencies:
github.com/docker/docker: Docker API clientgithub.com/sirupsen/logrus: Structured logginggithub.com/joho/godotenv: Environment variable management
Configure the application using environment variables in configs/.env:
# Target Docker stack/project name
STACK_NAME=your-stack-name
# Log file directory
LOG_PATH=logs
# Monitoring frequency (Go duration format)
MONITOR_INTERVAL=1s
# Total monitoring duration (Go duration format)
MONITOR_DURATION=10m| Variable | Description | Default | Example |
|---|---|---|---|
STACK_NAME |
Docker Compose project or Swarm stack name to monitor | Required | bitcoin-network |
LOG_PATH |
Directory for log files | logs |
./monitoring-logs |
MONITOR_INTERVAL |
Frequency of network stats collection | 5s |
1s, 500ms, 2m |
MONITOR_DURATION |
Total monitoring time before automatic shutdown | 60s |
10m, 1h, 30s |
# Run with default configuration
make run
# Or run the binary directly
./bin/benchmarkertime="2025-07-31T15:14:58+10:00" level=info msg="Bandwidth metrics" container_id=c32c1b7933ce container_name=bitcoin-network_local-1-1.1 period_ms=4000 rx_bytes_per_sec=98.49 rx_packets_per_sec=1.25 tx_bytes_per_sec=98.49 tx_packets_per_sec=1.25
time="2025-07-31T15:14:58+10:00" level=info msg="Bandwidth metrics" container_id=824faed26908 container_name=bitcoin-network_local-1-8.1 period_ms=5005 rx_bytes_per_sec=0 rx_packets_per_sec=0 tx_bytes_per_sec=0 tx_packets_per_sec=0
- Discovery: Automatically discovers containers belonging to the specified stack
- Parallel Monitoring: Launches separate goroutines for each container
- Data Collection: Collects network statistics via Docker API every
MONITOR_INTERVAL - Bandwidth Calculation: Calculates differential metrics between consecutive measurements
- Logging: Outputs structured logs with network performance data
- Graceful Shutdown: Stops after
MONITOR_DURATIONor manual interruption
The application uses Docker labels to identify containers within a stack:
com.docker.compose.project: For Docker Compose projectscom.docker.stack.namespace: For Docker Swarm stacks
- Connects to Docker Engine API
- Calls
ContainerStatsAPI for each monitored container - Aggregates network statistics across all container interfaces
- Captures: RX/TX bytes, RX/TX packets, timestamps
- Maintains previous measurement state for each container
- Calculates differential metrics:
(current - previous) / time_elapsed - Provides per-second rates for bytes and packets
- Tracks measurement periods for accuracy validation
- Concurrent Monitoring: Each container monitored in separate goroutine
- API Optimization: Single API call per container per interval
- Memory Efficiency: Only stores previous measurement for each container
- Error Resilience: Individual container failures don't affect others
- Application startup/shutdown events
- Container discovery results
- Error conditions and warnings
- Real-time bandwidth metrics
- Container-specific monitoring events
- Network performance data
The codebase follows Go best practices with clear separation of concerns:
cmd/: Application entry pointsinternal/: Private application codeconfigs/: Configuration fileslogs/: Runtime log files
- Monitor Service: Orchestrates the monitoring process
- Docker Monitor: Interfaces with Docker API
- Bandwidth Calculator: Computes network performance metrics
- Container Manager: Handles container discovery and management
- Configuration Manager: Manages environment-based settings
To extend monitoring capabilities:
- Add new fields to
NetworkStatsstruct instats/network.go - Extend Docker API data collection in
GetNetworkStats() - Update bandwidth calculation logic in
stats/bandwidth.go - Modify logging output in monitor service
No containers found: Verify STACK_NAME matches your Docker Compose project name
docker ps --filter label=com.docker.compose.project=your-stack-namePermission denied: Ensure user has Docker API access
sudo usermod -aG docker $USERAPI timeout errors: Increase monitoring interval for heavily loaded systems
MONITOR_INTERVAL=5sEnable verbose logging by modifying the logger configuration in internal/utils/logger/logger.go.