← Back to libraryQuestion 253 of 273
šŸ“‹Software TestingAdvanced

Performance Testing Basics

šŸ“Œ Definition:

Testing how applications behave and perform under various load, stress, and real-world conditions.

šŸ“– Detailed Explanation:

Performance testing is critical but often done last or skipped. A beautifully designed system that can't handle 100 concurrent users is worthless. Performance issues directly impact business: Slow sites lose customers, crashes cause data loss, timeouts frustrate users. Amazon's research: 100ms delay = 1% sales loss. Facebook: 1 second delay = 2% decline in traffic.

šŸ“Ž Key Performance Metrics:
MetricDescriptionFormulaIndustry BenchmarkExample
Response Time (Latency)How long user waits for responseTime from request sent to response receivedWeb: <200ms is excellent, <1s is good, >3s is unacceptableUser clicks 'Login' → Server processes → Response received = 150ms
ThroughputHow many requests/transactions completed per secondTotal requests processed / Total timeWeb API: 1000-10000 req/sec typicalAPI can process 5000 requests per second
Concurrent UsersHow many users can use system simultaneouslyPeak load the system can handleE-commerce: 10,000 concurrent during Black Friday saleSystem handles 1000 concurrent users without degradation
CPU UsageProcessor utilization under loadCPU cycles used / Total CPU capacityHealthy: 60-80%, warning: >80%, critical: >90%At 1000 users, CPU usage 75% (room to grow)
Memory UsageRAM consumed by applicationUsed memory / Total availableHealthy: <70%, warning: 70-85%, critical: >85%App uses 4GB of 8GB available
Error RatePercentage of failed requests under load(Failed requests / Total requests) Ɨ 100Acceptable: <0.1% errors, target: 0% errorsAt 5000 concurrent users, 0.05% requests fail (1 in 2000)
šŸ“Ž Types Of Performance Testing:
NameDescriptionPurposeExampleExpected OutcomeTools
Load TestingTest under expected/normal production loadVerify system meets performance requirements under typical conditionsE-commerce site during normal day: 1000 concurrent users expectedResponse time <500ms, CPU <70%, no errorsJMeter, LoadRunner, Gatling
Stress TestingTest beyond maximum expected load to find breaking pointIdentify failure threshold and graceful degradationE-commerce Black Friday sale: 100,000 concurrent users (10x normal)System degrades gracefully (shows 'wait in queue'), doesn't crashJMeter, LoadRunner, Locust
Spike TestingSudden dramatic increase in loadVerify system recovers from traffic spikesTraffic normally 100 users, suddenly jumps to 50,000 (viral post)Handles spike, doesn't crash, recovers to normalJMeter with spike configuration
Endurance/Soak TestingRun system under moderate load for extended periodFind memory leaks, resource exhaustion over timeRun 10,000 transactions per hour for 24 hoursMemory usage stays constant, no gradual degradationJMeter, custom scripts
Ramp-up TestingGradually increase load over timeIdentify at what load point system starts strugglingStart with 0 users, add 100 users every minute until failureIdentify exact threshold (e.g., system OK until 8000 users)
šŸ“Ž Real World Scenario:
Situation:

E-commerce site went down during Black Friday sale

Analysis:

Normal capacity: 1000 concurrent users → 100ms response time. Expected Black Friday: 5000 concurrent. Actual: 50,000 concurrent (viral post). System received 50x normal load. Expected response time: 100ms Ɨ 50 = 5 seconds. Actual: System hung and crashed.

Solution:

1) Load test at 50,000 concurrent. 2) Identify bottleneck (database query taking too long?). 3) Optimize (add caching, database indexing). 4) Re-test. 5) Implement auto-scaling (add more servers dynamically). 6) Set up circuit breaker (return 'try later' instead of crashing).

šŸ“Ž Performance Testing Process:
StepDetailsExample
1. Identify Performance RequirementsWhat's acceptable response time? How many concurrent users? What's peak load?Requirement: API must respond in <200ms for 95% of requests at 10,000 concurrent users
2. Design Load ModelHow do users behave? Constant load? Ramp-up? Spikes?0-2 min: 0→1000 users (ramp-up), 2-5 min: 1000 users (steady), 5-6 min: 1000→5000 (spike)
3. Create Test ScriptsScript realistic user workflowsLogin → Search products → Add to cart → Checkout → Payment → Logout
4. Set Up MonitoringCollect metrics: response time, CPU, memory, error rateJMeter collects metrics, push to monitoring dashboard
5. Execute Load TestRun test and collect dataJMeter generates load for 30 minutes, records all metrics
6. Analyze ResultsCompare actual vs requirements, identify bottlenecksResponse time 500ms vs requirement 200ms. Database queries slow. CPU >90%.
7. Identify BottleneckWhere's the problem? Database? Network? Code?Slow query in product search. Missing database index.
8. Optimize & Re-testFix performance issues and re-run testAdd index, response time now 150ms. Meets requirement.
šŸ“Ž Common Performance Issues And Solutions:
IssueDescriptionImpactSolution
N+1 Query ProblemCode queries database in a loop instead of batch1000 requests → 1001 database queries instead of 2Use JOIN queries, batch operations, caching
Missing Database IndexesSearching without indexes is slowQuery takes 5 seconds to find 1 record in million-row tableAdd index on frequently searched columns
Memory LeakApplication keeps using more memory over timeServer starts with 2GB usage, grows to 8GB after hours, crashesProfile code, find objects not being garbage collected
Inefficient AlgorithmAlgorithm has poor time complexitySorting 1 million items using O(n²) instead of O(n log n)Use better algorithm (QuickSort instead of BubbleSort)
No CachingComputing same result repeatedly200ms to compute user profile, times 10,000 users = 2000 secondsCache computation for 5 minutes, reuse for all requests
Synchronous OperationsWaiting for slow operations to completeEach request waits 3 seconds for email sendingMake asynchronous (queue email, send in background)
šŸ“Ž Popular Performance Testing Tools:
ToolBest ForDrawback
Apache JMeterOpen-source, free, supports all protocolsSteep learning curve
LoadRunnerEnterprise, advanced featuresExpensive licensing
GatlingModern tool, code-based, CI/CD friendlyScala syntax may be unfamiliar
LocustPython-based, simple scriptingNewer tool, less mature
K6Cloud-based, JavaScript, developer-friendlyRequires subscription for advanced features
šŸŽÆ Scenario-Based Interview Question:

Your checkout API responds in 100ms with 100 users, but 2 seconds with 1000 users. Expected: <500ms even at 1000 users. Diagnose and fix.

šŸ’” Quick Tip:

šŸ“ˆ Performance issues found under load, not with single user. Always load test before release. Slow = broken. Dead = worse.

šŸ’” Interview Focus:

Performance testing is advanced topic. Strong understanding separates senior testers from juniors.