|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | +trap cleanup EXIT |
| 5 | + |
| 6 | +if [ -z "S3_BUCKET" ]; then |
| 7 | + echo "Missing S3_BUCKET environment variabe" |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | + |
| 11 | +export BENCHMARK_STACK_NAME=${BENCHMARK_STACK_NAME:-"powertools-benchmark"} |
| 12 | + |
| 13 | +function cleanup { |
| 14 | + echo "Cleaning up stack..." |
| 15 | + aws cloudformation delete-stack --stack-name $BENCHMARK_STACK_NAME |
| 16 | +} |
| 17 | + |
| 18 | +function run_function { |
| 19 | + # Update function to force a cold start |
| 20 | + aws lambda update-function-configuration --function-name $1 --memory-size 256 >/dev/null |
| 21 | + aws lambda update-function-configuration --function-name $1 --memory-size 128 >/dev/null |
| 22 | + # Cold-start invoke |
| 23 | + aws lambda invoke --function-name $1 --payload '{}' /dev/null >/dev/null && echo -n . || echo -n e |
| 24 | +} |
| 25 | + |
| 26 | +# Retrieve statistics |
| 27 | +function get_stats { |
| 28 | + # Gather results from CloudWatch Logs Insights |
| 29 | + query_id=$(aws logs start-query --log-group-name $1 --query-string 'filter @type = "REPORT" | stats pct(@initDuration, 50) as init_duration, pct(@duration, 50) as duration' --start-time $(expr $(date +%s) - 86400) --end-time $(expr $(date +%s) + 0) --query 'queryId' --output text) |
| 30 | + while true; do |
| 31 | + result=$(aws logs get-query-results --query-id $query_id --query 'status' --output text) |
| 32 | + if [ $result == "Complete" ]; then |
| 33 | + break |
| 34 | + fi |
| 35 | + sleep 1 |
| 36 | + done |
| 37 | + |
| 38 | + # Check if greater than threshold and print result |
| 39 | + init_duration=$(aws logs get-query-results --query-id $query_id --query 'results[0][?field==`init_duration`].value' --output text) |
| 40 | + duration=$(aws logs get-query-results --query-id $query_id --query 'results[0][?field==`duration`].value' --output text) |
| 41 | + echo "$init_duration,$duration" |
| 42 | +} |
| 43 | + |
| 44 | +# Build and deploy the benchmark stack |
| 45 | +echo "Building and deploying..." |
| 46 | +sam build |
| 47 | +sam deploy --stack-name $BENCHMARK_STACK_NAME --s3-bucket $S3_BUCKET --capabilities CAPABILITY_IAM |
| 48 | + |
| 49 | +# Retrieve output values |
| 50 | +echo "Retrieve values..." |
| 51 | +export INSTRUMENTED_FUNCTION=$(aws cloudformation describe-stacks --stack-name $BENCHMARK_STACK_NAME --query 'Stacks[0].Outputs[?OutputKey==`InstrumentedFunction`].OutputValue' --output text) |
| 52 | +export REFERENCE_FUNCTION=$(aws cloudformation describe-stacks --stack-name $BENCHMARK_STACK_NAME --query 'Stacks[0].Outputs[?OutputKey==`ReferenceFunction`].OutputValue' --output text) |
| 53 | +export INSTRUMENTED_LOG_GROUP=$(aws cloudformation describe-stacks --stack-name $BENCHMARK_STACK_NAME --query 'Stacks[0].Outputs[?OutputKey==`InstrumentedLogGroup`].OutputValue' --output text) |
| 54 | +export REFERENCE_LOG_GROUP=$(aws cloudformation describe-stacks --stack-name $BENCHMARK_STACK_NAME --query 'Stacks[0].Outputs[?OutputKey==`ReferenceLogGroup`].OutputValue' --output text) |
| 55 | + |
| 56 | +echo INSTRUMENTED_FUNCTION=$INSTRUMENTED_FUNCTION |
| 57 | +echo REFERENCE_FUNCTION=$REFERENCE_FUNCTION |
| 58 | +echo INSTRUMENTED_LOG_GROUP=$INSTRUMENTED_LOG_GROUP |
| 59 | +echo REFERENCE_LOG_GROUP=$REFERENCE_LOG_GROUP |
| 60 | + |
| 61 | +# Running cold starts |
| 62 | +echo "Running functions..." |
| 63 | +for i in {0..20}; do |
| 64 | + run_function $INSTRUMENTED_FUNCTION |
| 65 | +done & |
| 66 | +process_id=$! |
| 67 | +for i in {0..20}; do |
| 68 | + run_function $REFERENCE_FUNCTION |
| 69 | +done & |
| 70 | +wait $process_id |
| 71 | +wait $! |
| 72 | +echo |
| 73 | + |
| 74 | +# Gather statistics |
| 75 | +# Waiting 2.5 minutes to make sure the data propagates from CloudWatch Logs |
| 76 | +# into CloudWatch Logs Insights. |
| 77 | +echo "Waiting for data to propagate in CloudWatch Logs Insights..." |
| 78 | +sleep 150 |
| 79 | +return_code=0 |
| 80 | +echo "INSTRUMENTED=$(get_stats $INSTRUMENTED_LOG_GROUP)" |
| 81 | +echo "REFERENCE=$(get_stats $REFERENCE_LOG_GROUP)" |
| 82 | + |
| 83 | +exit $return_code |
0 commit comments