33# #
44# # This source file is part of the SwiftAWSLambdaRuntime open source project
55# #
6- # # Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+ # # Copyright (c) 2017-2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors
77# # Licensed under Apache License v2.0
88# #
99# # See LICENSE.txt for license information
1313# #
1414# #===----------------------------------------------------------------------===##
1515
16- set -eu
16+ log () { printf -- " ** %s\n" " $* " >&2 ; }
17+ error () { printf -- " ** ERROR: %s\n" " $* " >&2 ; }
18+ fatal () { error " $@ " ; exit 1; }
1719
1820export HOST=127.0.0.1
19- export PORT=3000
21+ export PORT=7000
2022export AWS_LAMBDA_RUNTIME_API=" $HOST :$PORT "
21- export LOG_LEVEL=warning # important, otherwise log becomes a bottleneck
23+ export LOG_LEVEL=error # important, otherwise log becomes a bottleneck
24+
25+ DATE_CMD=" date"
26+ # using gdate on darwin for nanoseconds
27+ # gdate is installed by coreutils on macOS
28+ if [[ $( uname -s) == " Darwin" ]]; then
29+ if ! command -v gdate & > /dev/null; then
30+ # shellcheck disable=SC2006 # we explicitly want to use backticks here
31+ fatal " gdate could not be found. Please \` brew install coreutils\` to proceed."
32+ fi
33+ DATE_CMD=" gdate"
34+ fi
35+ echo " ⏱️ using $DATE_CMD to count time"
2236
23- # using gdate on mdarwin for nanoseconds
24- if [[ $( uname -s) == " Linux" ]]; then
25- shopt -s expand_aliases
26- alias gdate=" date"
37+ if ! command -v " $DATE_CMD " & > /dev/null; then
38+ fatal " $DATE_CMD could not be found. Please install $DATE_CMD to proceed."
2739fi
2840
41+ echo " 🏗️ Building library and test functions"
2942swift build -c release -Xswiftc -g
3043LAMBDA_USE_LOCAL_DEPS=../.. swift build --package-path Examples/HelloWorld -c release -Xswiftc -g
3144LAMBDA_USE_LOCAL_DEPS=../.. swift build --package-path Examples/HelloJSON -c release -Xswiftc -g
3245
3346cleanup () {
34- kill -9 $server_pid # ignore-unacceptable-language
47+ pkill -9 MockServer && echo " killed previous mock server " # ignore-unacceptable-language
3548}
3649
37- trap " cleanup" ERR
50+ # start a mock server
51+ start_mockserver () {
52+ if [ $# -ne 2 ]; then
53+ fatal " Usage: $0 <mode> <invocations>"
54+ fi
55+ MODE=$1
56+ INVOCATIONS=$2
57+ pkill -9 MockServer && echo " killed previous mock server" && sleep 1 # ignore-unacceptable-language
58+ echo " 👨🔧 starting server in $MODE mode for $INVOCATIONS invocations"
59+ (MAX_INVOCATIONS=" $INVOCATIONS " MODE=" $MODE " ./.build/release/MockServer) &
60+ server_pid=$!
61+ sleep 1
62+ kill -0 $server_pid # check server is alive # ignore-unacceptable-language
63+ }
3864
39- cold_iterations=1000
40- warm_iterations=10000
65+ cold_iterations=100
66+ warm_iterations=1000
4167results=()
4268
4369# ------------------
4470# string
4571# ------------------
4672
47- export MODE=string
73+ MODE=string
4874
49- # start (fork) mock server
50- pkill -9 MockServer && echo " killed previous servers" && sleep 1 # ignore-unacceptable-language
51- echo " starting server in $MODE mode"
52- (./.build/release/MockServer) &
53- server_pid=$!
54- sleep 1
55- kill -0 $server_pid # check server is alive # ignore-unacceptable-language
75+ # Start mock server
76+ start_mockserver " $MODE " " $cold_iterations "
5677
5778# cold start
58- echo " running $MODE mode cold test"
79+ echo " 🚀❄️ running $MODE mode $cold_iterations cold test"
5980cold=()
60- export MAX_REQUESTS=1
6181for (( i= 0 ; i< cold_iterations; i++ )) ; do
62- start=$( gdate +%s%N)
82+ start=$( " $DATE_CMD " +%s%N)
6383 ./Examples/HelloWorld/.build/release/MyLambda
64- end=$( gdate +%s%N)
84+ end=$( " $DATE_CMD " +%s%N)
6585 cold+=( $(( end- start)) )
6686done
6787sum_cold=$( IFS=+; echo " $(( ${cold[*]} )) " )
6888avg_cold=$(( sum_cold/ cold_iterations))
6989results+=( " $MODE , cold: $avg_cold (ns)" )
7090
91+ # reset mock server
92+ start_mockserver " $MODE " " $warm_iterations "
93+
7194# normal calls
72- echo " running $MODE mode warm test"
73- export MAX_REQUESTS=$warm_iterations
74- start=$( gdate +%s%N)
95+ echo " 🚀🌤️ running $MODE mode warm test"
96+ start=$( " $DATE_CMD " +%s%N)
7597./Examples/HelloWorld/.build/release/MyLambda
76- end=$( gdate +%s%N)
98+ end=$( " $DATE_CMD " +%s%N)
7799sum_warm=$(( end- start- avg_cold)) # substract by avg cold since the first call is cold
78100avg_warm=$(( sum_warm/ (warm_iterations- 1 )) ) # substract since the first call is cold
79101results+=( " $MODE , warm: $avg_warm (ns)" )
@@ -84,34 +106,30 @@ results+=( "$MODE, warm: $avg_warm (ns)" )
84106
85107export MODE=json
86108
87- # start (fork) mock server
88- pkill -9 MockServer && echo " killed previous servers" && sleep 1 # ignore-unacceptable-language
89- echo " starting server in $MODE mode"
90- (./.build/release/MockServer) &
91- server_pid=$!
92- sleep 1
93- kill -0 $server_pid # check server is alive # ignore-unacceptable-language
109+ # Start mock server
110+ start_mockserver " $MODE " " $cold_iterations "
94111
95112# cold start
96- echo " running $MODE mode cold test"
113+ echo " 🚀❄️ running $MODE mode cold test"
97114cold=()
98- export MAX_REQUESTS=1
99115for (( i= 0 ; i< cold_iterations; i++ )) ; do
100- start=$( gdate +%s%N)
101- ./Examples/HelloJSON/.build/release/MyLambda
102- end=$( gdate +%s%N)
116+ start=$( " $DATE_CMD " +%s%N)
117+ ./Examples/HelloJSON/.build/release/HelloJSON
118+ end=$( " $DATE_CMD " +%s%N)
103119 cold+=( $(( end- start)) )
104120done
105121sum_cold=$( IFS=+; echo " $(( ${cold[*]} )) " )
106122avg_cold=$(( sum_cold/ cold_iterations))
107123results+=( " $MODE , cold: $avg_cold (ns)" )
108124
125+ # reset mock server
126+ start_mockserver " $MODE " " $warm_iterations "
127+
109128# normal calls
110- echo " running $MODE mode warm test"
111- export MAX_REQUESTS=$warm_iterations
112- start=$( gdate +%s%N)
113- ./Examples/HelloJSON/.build/release/MyLambda
114- end=$( gdate +%s%N)
129+ echo " 🚀🌤️ running $MODE mode warm test"
130+ start=$( " $DATE_CMD " +%s%N)
131+ ./Examples/HelloJSON/.build/release/HelloJSON
132+ end=$( " $DATE_CMD " +%s%N)
115133sum_warm=$(( end- start- avg_cold)) # substract by avg cold since the first call is cold
116134avg_warm=$(( sum_warm/ (warm_iterations- 1 )) ) # substract since the first call is cold
117135results+=( " $MODE , warm: $avg_warm (ns)" )
0 commit comments