This tool helps test and document AWS Lambda CPU throttling behavior at different memory levels. It deploys Lambda functions with various memory configurations and measures how CPU throttling affects execution.
AWS Lambda allocates CPU power in proportion to the memory configured for the function. According to AWS:
- At 1,769 MB, a function has the equivalent of 1 vCPU
- Lower memory settings receive a proportional share of CPU time
- For example, at 128 MB, a function receives roughly 7.2% (128/1,769) of a vCPU
This tool helps to:
- Measure actual CPU throttling pauses at different memory settings
- Visualize throttling patterns and ratios
- Document the real-world impact of Lambda memory configuration on CPU-intensive tasks
View the latest visualization report
- Node.js 20+ and npm
- AWS account with appropriate permissions
- AWS CLI configured with credentials (SSO supported)
git clone https://github.com/pwrdrvr/lambda-throttling.git
cd lambda-throttling
npm install
- Build and deploy the Lambda functions with multiple memory configurations using CDK:
npm run deploy
This creates the following Lambda functions:
throttling-test-128mb
(128 MB)throttling-test-256mb
(256 MB)throttling-test-512mb
(512 MB)throttling-test-1024mb
(1024 MB)throttling-test-1769mb
(1769 MB)throttling-calibration-3000mb
(3000 MB) for baseline calibrationadaptive-throttling-128mb
(128 MB) for adaptive workload testingadaptive-throttling-256mb
(256 MB) for adaptive workload testingadaptive-throttling-512mb
(512 MB) for adaptive workload testingadaptive-throttling-1024mb
(1024 MB) for adaptive workload testingadaptive-throttling-1769mb
(1769 MB) for adaptive workload testing
- Run the standard throttling tests to observe throttling behavior:
# Test all memory sizes (128, 256, 512, 1024, 1769 MB)
npm run test-throttling
# Test a specific memory size
npm run test-throttling:128
npm run test-throttling:256
npm run test-throttling:512
npm run test-throttling:1024
npm run test-throttling:1769
# Run a longer test (15 seconds)
npm run test-throttling:long
# Run with higher CPU intensity (500KB data)
npm run test-throttling:intense
# Run with extreme CPU intensity (1MB data)
npm run test-throttling:extreme
# Custom parameters
npx ts-node src/test-throttling.ts --memory=512 --duration=10000 --dataSize=200
- Run the adaptive throttling tests that stay under throttle intervals:
# Test all memory sizes with adaptive workloads
npm run test-adaptive
- Visualize the results:
# Visualize standard throttling tests
npm run visualize
# Visualize adaptive throttling tests
npm run visualize:adaptive
This generates HTML reports with charts in the charts/
directory.
- Run everything in sequence:
# Run standard throttling tests (deploy, test, visualize)
npm run run-all
# Run adaptive throttling tests (deploy, test, visualize)
npm run run-adaptive
-
Calibration Phase:
- A 3,000 MB Lambda (over 1 full vCPU) is used to establish baseline performance
- Warmup iterations stabilize performance
- Precise measurements of unthrottled iteration time and CPU usage
- Establishes baseline metrics for detecting throttling
-
Testing Phase:
- Lambda functions at different memory levels run CPU-intensive operations:
- Generates random data (configurable size, default 100KB)
- Performs cryptographic operations (SHA-256 hashing)
- Compresses data using zlib
- Tracks CPU usage through the Node.js process.cpuUsage() API
- Each iteration's performance is measured against the baseline
- Both wall-clock time and CPU time are measured using high-resolution timers
- Significant delays compared to baseline indicate throttling
- Lambda functions at different memory levels run CPU-intensive operations:
-
Analysis & Visualization:
- Calculates throttling ratio (percentage of time spent throttled)
- Records timing of each iteration and throttling event
- Measures CPU efficiency compared to the unthrottled baseline
- Generates interactive visualizations showing throttling patterns
- Tracks iteration-by-iteration performance to detect early throttling
-
Smart CPU Allocation:
- Based on the principle that 1769 MB = 100% of a vCPU (1 full CPU)
- Determines the Lambda function's CPU allocation as a fraction of a full vCPU
- A 128 MB Lambda function receives approximately 7.2% of a CPU (128/1769)
-
Throttle Interval Awareness:
- AWS Lambda throttles CPU in 20ms intervals
- For example, a 128 MB Lambda gets approximately 1.44ms of CPU time per 20ms window (7.2% * 20ms)
- Lambda is throttled (paused) for the remaining 18.56ms out of every 20ms interval
-
Adaptive Workload Sizing:
- Calculates how much workload can be processed in a single throttle interval
- Scales the data size based on the Lambda's CPU allocation percentage
- Reduces the data size by 10% as a safety margin to avoid exceeding the throttle interval
- Example: A 100KB job on a 1769 MB Lambda becomes a 7KB job on a 128 MB Lambda
-
Precise Timing Control:
- Executes one unit of work per throttle interval (every 20ms)
- Uses busy-waiting to ensure precise timing between iterations
- Measures actual CPU time and wall clock time for each iteration
- Records execution times to detect any unexpected throttling
-
Analysis & Visualization:
- Compares CPU allocation percentages across memory sizes
- Shows the adaptive data size for each memory configuration
- Measures actual execution time against the 20ms throttle interval
- Visualizes iteration-by-iteration performance
- Detects any potential throttling events where execution exceeds expectations
The Lambda function:
- Runs a tight loop consuming CPU
- Measures execution time using Node.js's high-resolution
process.hrtime()
- Compares expected vs. actual execution time to detect throttling pauses
- Records precise timings of each throttling event
- Calculates a "throttling ratio" (percentage of time the function was throttled)
- Throttling Ratio: Higher values indicate more aggressive throttling
- Throttling Events: Count of distinct pauses in execution
- Event Timeline: Shows when throttling occurred during execution
- Delay Duration: Shows the length of each throttling pause
This chart shows the percentage of time a function spends being throttled at different memory levels:
This chart shows the execution time of each iteration, helping identify throttling patterns:
This scatter plot shows when throttling events occurred and their severity:
This chart shows how CPU allocation scales with memory size:
This chart shows how the workload size is adjusted based on memory allocation:
This chart shows CPU time vs. wall clock time for different memory sizes with adaptive workloads:
For interactive visualizations with tooltips and more details, view the full HTML report.
Memory configurations to test:
Memory (MB) | Expected CPU % | Notes |
---|---|---|
128 | ~7.2% | Function receives ~7.2% of a vCPU |
256 | ~14.5% | Function receives ~14.5% of a vCPU |
512 | ~29% | Function receives ~29% of a vCPU |
1024 | ~58% | Function receives ~58% of a vCPU |
1769 | 100% | Function receives full vCPU access |
Common issues and their solutions:
- Missing IAM Permissions: Ensure your AWS credentials have appropriate Lambda and IAM permissions
- CDK Deployment Errors: Check that the AWS CDK is bootstrapped in your account and region
- Lambda Timeout: Increase the Lambda timeout in the CDK stack if needed
- Cold Start Noise: Initial test results may include cold start overhead; run tests multiple times for consistent results
This tool uses AWS SDK v3, which supports AWS SSO authentication. Make sure you've run aws sso login
before running the tests if you're using AWS SSO.