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