コンテンツにスキップ

Performance Optimization Commands: Master Professional Command-Line Excellence

July 26, 2025 | Reading Time: 13 minutes 37 seconds

Master performance optimization commands with this comprehensive guide designed for IT professionals and system administrators. From fundamental concepts to advanced optimization techniques, this detailed technical guide provides the command-line tools and methodologies needed to achieve peak system performance in modern computing environments.

Introduction: The Critical Role of Performance Optimization

Performance optimization represents one of the most crucial aspects of modern system administration and DevOps practices. In today's high-performance computing environments, the ability to identify bottlenecks, optimize resource utilization, and maintain peak system performance directly impacts business operations, user experience, and operational costs. This comprehensive guide provides IT professionals with essential command-line tools and methodologies for achieving optimal system performance across diverse infrastructure environments.

Performance optimization is not merely about making systems faster—it's about creating sustainable, efficient, and reliable computing environments that can scale with organizational growth while maintaining cost-effectiveness. Modern performance optimization requires understanding complex interactions between hardware resources, operating system components, application layers, and network infrastructure. The command-line tools and techniques presented in this guide provide the foundation for systematic performance analysis and optimization that every professional should master.

System Performance Monitoring Commands

CPU Performance Analysis

htop - Interactive process viewer with real-time CPU monitoring

# Install and run htop for comprehensive CPU analysis
sudo apt install htop
htop

# Key features:
# - Real-time CPU usage per core
# - Process tree visualization
# - Memory and swap usage
# - Load average monitoring
# - Interactive process management

vmstat - Virtual memory statistics and system performance

# Display system performance statistics
vmstat 1 10    # Update every 1 second, 10 iterations

# Key metrics:
# - CPU usage breakdown (user, system, idle, wait)
# - Memory utilization and swap activity
# - I/O operations and context switches
# - Process creation and scheduling statistics

# Detailed memory analysis
vmstat -s      # Display memory statistics summary
vmstat -d      # Display disk statistics

sar - System Activity Reporter for historical performance data

# Install sysstat package for sar command
sudo apt install sysstat

# CPU utilization analysis
sar -u 1 10    # CPU usage every 1 second, 10 times
sar -u -f /var/log/sysstat/saXX    # Historical CPU data

# Memory utilization tracking
sar -r 1 10    # Memory usage statistics
sar -S 1 10    # Swap space utilization

# I/O performance monitoring
sar -b 1 10    # I/O transfer rate statistics
sar -d 1 10    # Block device activity

Memory Performance Optimization

free - Memory usage analysis and optimization

# Display memory usage in human-readable format
free -h

# Continuous monitoring with updates
free -h -s 2   # Update every 2 seconds

# Detailed memory breakdown
free -m -t     # Show totals in megabytes

# Clear system caches (use with caution)
sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

pmap - Process memory mapping analysis

# Analyze memory usage of specific processes
pmap -d PID    # Detailed memory mapping
pmap -x PID    # Extended format with additional details

# Sort processes by memory usage
ps aux --sort=-%mem | head -10

# Memory usage by process tree
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem

Disk I/O Performance Commands

iostat - I/O statistics for devices and partitions

# Basic I/O statistics
iostat -x 1 10    # Extended statistics, 1-second intervals

# Key metrics to monitor:
# - %iowait: CPU time waiting for I/O
# - avgqu-sz: Average queue size
# - await: Average wait time for I/O requests
# - %util: Device utilization percentage

# Specific device monitoring
iostat -x sda 1 5    # Monitor specific device

iotop - Real-time I/O usage by processes

# Install and run iotop
sudo apt install iotop
sudo iotop

# Key features:
# - Real-time I/O usage per process
# - Read/write bandwidth monitoring
# - Total I/O statistics
# - Process-level I/O prioritization

fio - Flexible I/O tester for performance benchmarking

# Install fio for comprehensive I/O testing
sudo apt install fio

# Random read performance test
fio --name=random-read --ioengine=libaio --rw=randread --bs=4k --numjobs=4 --size=1G --runtime=60 --group_reporting

# Sequential write performance test
fio --name=sequential-write --ioengine=libaio --rw=write --bs=1M --numjobs=1 --size=2G --runtime=60 --group_reporting

# Mixed workload simulation
fio --name=mixed-workload --ioengine=libaio --rw=randrw --rwmixread=70 --bs=4k --numjobs=4 --size=1G --runtime=120 --group_reporting

Network Performance Optimization

Network Monitoring and Analysis

iftop - Real-time network bandwidth usage

# Install and run iftop
sudo apt install iftop
sudo iftop -i eth0    # Monitor specific interface

# Key features:
# - Real-time bandwidth usage per connection
# - Source and destination analysis
# - Protocol breakdown
# - Historical bandwidth trends

nethogs - Network usage by process

# Install and monitor network usage by process
sudo apt install nethogs
sudo nethogs eth0

# Features:
# - Process-level network usage
# - Real-time bandwidth consumption
# - Application identification
# - Network troubleshooting support

ss - Socket statistics and network connections

# Display all network connections
ss -tuln    # TCP and UDP listening ports

# Detailed connection analysis
ss -i       # Show internal TCP information
ss -s       # Display socket statistics summary

# Monitor specific protocols
ss -t state established    # Established TCP connections
ss -u       # UDP socket information

Network Performance Tuning

tc - Traffic control for bandwidth management

# Create traffic shaping rules
sudo tc qdisc add dev eth0 root handle 1: htb default 30

# Add bandwidth classes
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit
sudo tc class add dev eth0 parent 1:1 classid 1:10 htb rate 80mbit ceil 100mbit
sudo tc class add dev eth0 parent 1:1 classid 1:20 htb rate 20mbit ceil 100mbit

# Apply filters for traffic classification
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 80 0xffff flowid 1:10

ethtool - Ethernet device configuration and optimization

# Display interface capabilities and settings
ethtool eth0

# Optimize network interface settings
sudo ethtool -G eth0 rx 4096 tx 4096    # Increase ring buffer sizes
sudo ethtool -K eth0 tso on gso on       # Enable TCP/Generic segmentation offload
sudo ethtool -A eth0 rx on tx on         # Enable flow control

# Monitor interface statistics
ethtool -S eth0    # Display detailed statistics

Application Performance Profiling

Process Performance Analysis

perf - Performance analysis tools for Linux

# Install perf tools
sudo apt install linux-tools-common linux-tools-generic

# CPU profiling and analysis
sudo perf top                    # Real-time performance monitoring
sudo perf record -g ./application    # Record performance data with call graphs
sudo perf report                 # Analyze recorded performance data

# System-wide performance analysis
sudo perf stat -a sleep 10       # System-wide statistics for 10 seconds
sudo perf record -a -g sleep 10  # Record system-wide performance data

strace - System call tracing for application analysis

# Trace system calls for performance analysis
strace -c ./application          # Count system calls
strace -T ./application          # Show time spent in each system call
strace -f -p PID                 # Trace running process and children

# Filter specific system calls
strace -e trace=file ./application    # Trace file-related calls
strace -e trace=network ./application # Trace network-related calls

ltrace - Library call tracing

# Trace library calls for performance analysis
ltrace -c ./application          # Count library calls
ltrace -T ./application          # Show time spent in library calls
ltrace -S ./application          # Include system calls in trace

Database Performance Optimization

MySQL Performance Commands

# MySQL performance analysis
mysql -e "SHOW PROCESSLIST;"     # Display running queries
mysql -e "SHOW ENGINE INNODB STATUS\G"    # InnoDB engine status

# Query performance analysis
mysql -e "SELECT * FROM information_schema.processlist WHERE command != 'Sleep';"

# Enable slow query log analysis
mysql -e "SET GLOBAL slow_query_log = 'ON';"
mysql -e "SET GLOBAL long_query_time = 2;"

# Analyze slow query log
mysqldumpslow /var/log/mysql/mysql-slow.log

PostgreSQL Performance Commands

# PostgreSQL performance monitoring
psql -c "SELECT * FROM pg_stat_activity WHERE state = 'active';"

# Query performance analysis
psql -c "SELECT query, calls, total_time, mean_time FROM pg_stat_statements ORDER BY total_time DESC LIMIT 10;"

# Database statistics
psql -c "SELECT * FROM pg_stat_database;"
psql -c "SELECT * FROM pg_stat_user_tables;"

Advanced Performance Optimization Techniques

Kernel Parameter Tuning

sysctl - Kernel parameter optimization

# Display current kernel parameters
sysctl -a | grep -E "(net|vm|fs)"

# Network performance tuning
echo 'net.core.rmem_max = 134217728' | sudo tee -a /etc/sysctl.conf
echo 'net.core.wmem_max = 134217728' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_rmem = 4096 87380 134217728' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_wmem = 4096 65536 134217728' | sudo tee -a /etc/sysctl.conf

# Memory management optimization
echo 'vm.swappiness = 10' | sudo tee -a /etc/sysctl.conf
echo 'vm.dirty_ratio = 15' | sudo tee -a /etc/sysctl.conf
echo 'vm.dirty_background_ratio = 5' | sudo tee -a /etc/sysctl.conf

# Apply changes
sudo sysctl -p

Container Performance Optimization

Docker Performance Commands

# Monitor container resource usage
docker stats                     # Real-time container statistics
docker stats --no-stream        # One-time statistics snapshot

# Container resource limits
docker run --memory=1g --cpus=2 image_name    # Set resource limits
docker update --memory=2g container_name      # Update running container limits

# Performance analysis
docker exec container_name top   # Process monitoring inside container
docker exec container_name iostat 1 5    # I/O statistics in container

Kubernetes Performance Commands

# Node resource monitoring
kubectl top nodes               # Node resource usage
kubectl top pods --all-namespaces    # Pod resource usage

# Detailed resource analysis
kubectl describe node node_name # Node resource allocation
kubectl get pods -o wide        # Pod distribution across nodes

# Performance metrics
kubectl get --raw /metrics      # Cluster metrics endpoint

Performance Monitoring Automation

Automated Performance Monitoring Scripts

System Health Check Script

#!/bin/bash
# comprehensive_performance_check.sh

echo "=== System Performance Health Check ==="
echo "Timestamp: $(date)"
echo

# CPU Usage
echo "CPU Usage:"
top -bn1 | grep "Cpu(s)" | awk '{print $2 $4}'

# Memory Usage
echo "Memory Usage:"
free -h | grep -E "Mem|Swap"

# Disk Usage
echo "Disk Usage:"
df -h | grep -vE '^Filesystem|tmpfs|cdrom'

# Load Average
echo "Load Average:"
uptime

# Network Connections
echo "Network Connections:"
ss -s

# Top Processes by CPU
echo "Top CPU Processes:"
ps aux --sort=-%cpu | head -5

# Top Processes by Memory
echo "Top Memory Processes:"
ps aux --sort=-%mem | head -5

echo "=== End Performance Check ==="

Performance Alerting Script

#!/bin/bash
# performance_alerting.sh

# Thresholds
CPU_THRESHOLD=80
MEMORY_THRESHOLD=85
DISK_THRESHOLD=90

# Check CPU usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
if (( $(echo "$CPU_USAGE > $CPU_THRESHOLD" | bc -l) )); then
    echo "ALERT: High CPU usage: $CPU_USAGE%"
fi

# Check memory usage
MEMORY_USAGE=$(free | grep Mem | awk '{printf("%.0f", $3/$2 * 100.0)}')
if [ $MEMORY_USAGE -gt $MEMORY_THRESHOLD ]; then
    echo "ALERT: High memory usage: $MEMORY_USAGE%"
fi

# Check disk usage
while read output;
do
  usage=$(echo $output | awk '{ print $5}' | sed 's/%//g')
  partition=$(echo $output | awk '{ print $1 }')
  if [ $usage -ge $DISK_THRESHOLD ]; then
    echo "ALERT: High disk usage on $partition: $usage%"
  fi
done <<< "$(df -h | grep -vE '^Filesystem|tmpfs|cdrom')"

Conclusion: Mastering Performance Optimization

Performance optimization is an ongoing process that requires systematic monitoring, analysis, and tuning of system resources. The commands and techniques presented in this guide provide the foundation for maintaining optimal system performance in modern IT environments. Success in performance optimization comes from understanding the relationships between different system components and applying the right tools and techniques for specific performance challenges.

Regular performance monitoring, proactive optimization, and continuous learning about new tools and techniques are essential for maintaining peak system performance. By mastering these command-line tools and methodologies, IT professionals can ensure their systems deliver optimal performance while maintaining reliability and cost-effectiveness.

The key to successful performance optimization lies in establishing baseline measurements, implementing continuous monitoring, and taking a systematic approach to identifying and resolving performance bottlenecks. These skills are essential for any IT professional working in modern high-performance computing environments.

References and Further Reading

This comprehensive guide provides the foundation for professional excellence in performance optimization commands. For additional resources and advanced topics, consult system administration documentation, performance optimization guides, and specialized training programs that can further enhance your command-line expertise.