Performance optimization strategies including caching, profiling, database query optimization, frontend optimization, and load testing. Use when optimizing application performance, reducing latency, improving throughput, or diagnosing performance bottlenecks.
This skill provides comprehensive performance optimization strategies covering application profiling, caching strategies, database optimization, frontend performance, and load testing methodologies.
# Python with functools.lru_cache
from functools import lru_cache
@lru_cache(maxsize=128)
def get_user_by_id(user_id: int) -> dict:
return db.query(User).get(user_id)
# Redis caching
import redis
from functools import wraps
r = redis.Redis(host='localhost', port=6379, db=0)
def cache_with_ttl(seconds=300):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
key = f"{func.__name__}:{args}:{kwargs}"
cached = r.get(key)
if cached:
return json.loads(cached)
result = func(*args, **kwargs)
r.setex(key, seconds, json.dumps(result))
return result
return wrapper
return decorator
@cache_with_ttl(seconds=600)
def get_expensive_data(param: str):
# Expensive operation
return result
# CloudFront/CloudFlare headers
Cache-Control: public, max-age=31536000, immutable # Static assets
Cache-Control: public, max-age=3600 # API responses
Cache-Control: private, no-cache # User-specific data
# SQLAlchemy query caching
from sqlalchemy.orm import joinedload
# ❌ N+1 Problem
users = db.query(User).all()
for user in users:
print(user.profile.bio) # N additional queries
# ✅ Eager Loading
users = db.query(User).options(joinedload(User.profile)).all()
# ✅ Selective Loading
users = db.query(User).options(
joinedload(User.profile),
joinedload(User.posts)
).all()
-- ❌ Full table scan
SELECT * FROM orders WHERE YEAR(created_at) = 2024;
-- ✅ Index-friendly query
SELECT * FROM orders
WHERE created_at >= '2024-01-01'
AND created_at < '2025-01-01';
-- ✅ Covering index
CREATE INDEX idx_orders_user_date ON orders(user_id, created_at)
INCLUDE (total_amount, status);
-- ✅ Composite index for range queries
CREATE INDEX idx_products_category_price ON products(category, price);
# SQLAlchemy connection pooling
from sqlalchemy import create_engine
engine = create_engine(
'postgresql://user:pass@localhost/db',
pool_size=10,
max_overflow=20,
pool_pre_ping=True,
pool_recycle=3600
)
# Node.js with pg-pool
const pool = new Pool({
host: 'localhost',
port: 5432,
database: 'myapp',
max: 20, // Maximum pool size
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
// React lazy loading
const Dashboard = lazy(() => import('./Dashboard'));
const Analytics = lazy(() => import('./Analytics'));
// Route-based splitting
<Route path="/dashboard" component={Dashboard} />
<Route path="/analytics" component={Analytics} />
<!-- Responsive images -->
<picture>
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpg" alt="Description" loading="lazy">
</picture>
<!-- Next.js Image component -->
import Image from 'next/image';
<Image
src="/photo.jpg"
alt="Photo"
width={800}
height={600}
placeholder="blur"
priority={false}
/>
# cProfile
import cProfile
import pstats
profiler = cProfile.Profile()
profiler.enable()
# Your code here
result = expensive_function()
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative')
stats.print_stats(20)
# Line profiler
from line_profiler import LineProfiler
profiler = LineProfiler()
@profiler # Add decorator
def my_function():
for i in range(1000):
x = [i ** 2 for i in range(1000)]
return x
my_function()
profiler.print_stats()
-- PostgreSQL EXPLAIN ANALYZE
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT * FROM orders o
JOIN users u ON o.user_id = u.id
WHERE o.status = 'pending';
-- MySQL EXPLAIN
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE user_id = 123
ORDER BY created_at DESC
LIMIT 10;
See comprehensive guides in references/:
Use this skill when:
@kubernetes-patterns - Scaling and resource optimization@docker-patterns - Container optimization@postgresql-patterns - Database-specific optimization@mongodb-patterns - NoSQL optimization@observability-monitoring - Performance monitoringnpx skills add Jonathan0823/performance-optimization下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer