| name | performance-expert |
| version | 1.0.0 |
| description | Expert-level performance optimization, profiling, benchmarking, and tuning |
| category | devops |
| tags | ["performance","optimization","profiling","benchmarking","scalability"] |
| allowed-tools | ["Read","Write","Edit","Bash(*)"] |
Performance Expert
Expert guidance for performance optimization, profiling, benchmarking, and system tuning.
Core Concepts
Performance Fundamentals
- Response time vs throughput
- Latency vs bandwidth
- CPU, memory, I/O bottlenecks
- Concurrency vs parallelism
- Caching strategies
- Load balancing
Optimization Areas
- Algorithm optimization
- Database optimization
- Network optimization
- Frontend performance
- Backend performance
- Infrastructure tuning
Profiling Tools
- CPU profilers
- Memory profilers
- Network profilers
- Application Performance Monitoring (APM)
- Load testing tools
Python Performance
import cProfile
import pstats
import timeit
import memory_profiler
from functools import lru_cache
from typing import List
import numpy as np
def profile_function(func):
"""Decorator for profiling function execution"""
def wrapper(*args, **kwargs):
profiler = cProfile.Profile()
profiler.enable()
result = func(*args, **kwargs)
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative')
stats.print_stats(10)
return result
return wrapper
@profile_function
def slow_function():
total = 0
for i in range(1000000):
total += i
return total
@lru_cache(maxsize=128)
def fibonacci(n: int) -> int:
"""Cached Fibonacci calculation"""
if n < 2:
return n
return fibonacci(n-) + fibonacci(n-)
() -> []:
[x ** + * x + x data]
() -> np.ndarray:
data ** + * data +
():
total_time = timeit.timeit(
: func(*args),
number=iterations
)
avg_time = total_time / iterations
{
: total_time,
: avg_time,
: iterations
}
():
data = [i i ()]
(data)
() -> :
result =
item items:
result += item
result
() -> :
.join(items)
() -> []:
[i ** i (n)]
():
i (n):
i **
Database Optimization
from sqlalchemy import create_engine, Index, text
from sqlalchemy.orm import sessionmaker
import redis
engine = create_engine(
'postgresql://user:pass@localhost/db',
pool_size=20,
max_overflow=0,
pool_pre_ping=True,
pool_recycle=3600
)
class DatabaseOptimizer:
def __init__(self, session):
self.session = session
def bad_n_plus_one(self):
"""N+1 query problem"""
users = self.session.query(User).all()
for user in users:
posts = user.posts
print(f"{user.name}: {len(posts)} posts")
def good_eager_loading(self):
"""Eager loading to avoid N+1"""
from sqlalchemy.orm import joinedload
users = self.session.query(User)\
.options(joinedload(User.posts))\
.all()
for user in users:
posts = user.posts
print(f": posts")
():
Index(, User.email)
Index(, Post.created_at)
Index(, Post.user_id, Post.status)
():
.session.bulk_insert_mappings(User, items)
.session.commit()
():
offset = (page - ) * page_size
.session.query(User)\
.order_by(User.created_at.desc())\
.limit(page_size)\
.offset(offset)\
.()
:
():
.redis = redis_client
():
cached = .redis.get(key)
cached:
json.loads(cached)
result = query_func()
.redis.setex(key, ttl, json.dumps(result))
result
():
data = .redis.get(key)
data :
data = fetch_func()
.redis.setex(key, ttl, json.dumps(data))
:
data = json.loads(data)
data
Frontend Performance
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
const searchInput = document.getElementById('search');
const debouncedSearch = debounce((query) => {
fetchSearchResults(query);
}, 300);
searchInput.addEventListener('input', (e) => {
debouncedSearch(e.target.value);
});
const lazyLoadImages = () => {
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries) => {
entries.( {
(entry.) {
img = entry.;
img. = img..;
img.();
imageObserver.(img);
}
});
});
images.( imageObserver.(img));
};
{
() {
. = container;
. = items;
. = itemHeight;
. = .(container. / itemHeight);
.();
}
() {
scrollTop = ..;
startIndex = .(scrollTop / .);
endIndex = startIndex + .;
visibleData = ..(startIndex, endIndex);
.. = visibleData
.( )
.();
}
}
() {
= ();
.();
}
API Performance
from fastapi import FastAPI, BackgroundTasks
from fastapi.responses import StreamingResponse
import asyncio
from typing import AsyncGenerator
app = FastAPI()
@app.get("/users/{user_id}")
async def get_user(user_id: str):
"""Async endpoint for database queries"""
user = await db.fetch_user(user_id)
return user
async def generate_large_file() -> AsyncGenerator[bytes, None]:
"""Stream large file in chunks"""
chunk_size = 8192
with open("large_file.csv", "rb") as f:
while chunk := f.read(chunk_size):
yield chunk
await asyncio.sleep(0)
@app.get("/download")
async def download_large_file():
return StreamingResponse(
generate_large_file(),
media_type="text/csv"
)
@app.post()
():
background_tasks.add_task(heavy_processing_task)
{: }
aiohttp
:
():
.session =
():
.session = aiohttp.ClientSession(
connector=aiohttp.TCPConnector(limit=)
)
():
.session.close()
():
.session.get(url) response:
response.json()
Load Testing
from locust import HttpUser, task, between
class PerformanceTest(HttpUser):
wait_time = between(1, 3)
@task(3)
def get_users(self):
"""High frequency endpoint"""
self.client.get("/api/users")
@task(1)
def create_user(self):
"""Lower frequency endpoint"""
self.client.post("/api/users", json={
"email": "test@example.com",
"name": "Test User"
})
def on_start(self):
"""Login once per user"""
response = self.client.post("/api/login", json={
"username": "test",
"password": "password"
})
self.token = response.json()["token"]
Best Practices
General
- Measure before optimizing
- Focus on bottlenecks
- Use appropriate data structures
- Cache expensive computations
- Minimize I/O operations
- Use connection pooling
- Implement pagination
Database
- Create proper indexes
- Avoid N+1 queries
- Use eager loading
- Batch operations
- Optimize queries
- Use read replicas
- Implement caching
Frontend
- Minimize bundle size
- Code splitting
- Lazy loading
- Compress assets
- Use CDN
- Optimize images
- Debounce/throttle events
Backend
- Use async for I/O
- Implement caching
- Connection pooling
- Background processing
- Horizontal scaling
- Load balancing
Anti-Patterns
❌ Premature optimization
❌ No profiling/measurement
❌ Optimizing wrong bottlenecks
❌ Ignoring caching
❌ Synchronous I/O operations
❌ No database indexes
❌ Loading all data at once
Resources