Skip to content

Instantly share code, notes, and snippets.

@crewjam
Last active December 15, 2020 17:51
Show Gist options
  • Save crewjam/9548cfbe4de569b1fdbaadd6d9df3337 to your computer and use it in GitHub Desktop.
Save crewjam/9548cfbe4de569b1fdbaadd6d9df3337 to your computer and use it in GitHub Desktop.

Go crypto operations are super slow on AWS lambda while processing an event, but they are super fast during cold start.

The performance is pretty reliably loke the following:

$ aws lambda invoke --function-name $FUNCTION response.json
$ jq -r < response.json
cold start: count: 10
cold start: total dur: 143.704664ms
cold start: avg dur 14.370466ms
count: 10
total dur: 1.973332046s
avg dur 197.333204ms
package main
import (
"bytes"
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dgrijalva/jwt-go"
"time"
)
var pk = []byte("-----BEGIN EC PRIVATE KEY-----\nMIHcAgEBBEIBkhuWYSdLeZkeKFskbnfAzmecetbNEe95CvS3txBAF+TinQtjluUN\nttbwVDyhNRY7uEJH5CKtaNABRojzU2KwhwagBwYFK4EEACOhgYkDgYYABAHEvCZg\nbsuBQxPy8c3Rki1EG3dfDw5aWurBKeq2QoFcke2Dx8usTz0KtQBKKGrC8SukZdYh\nsR8wBe/hGbqPxnaizQDIcmC5bDxWTkTP2ugPxd+ia8fTAkIoFhiJHg7Iv3Cpr5Wd\nne13zsG54ZlpJY1cRqgNZFuClRt26ZgIgg8AfV3c0A==\n-----END EC PRIVATE KEY-----")
func whyIsThisSlow(count int) time.Duration {
startTime := time.Now()
for i := 0 ; i < count; i++ {
if _, err := jwt.ParseECPrivateKeyFromPEM(pk); err != nil {
panic(err)
}
}
return time.Now().Sub(startTime)
}
func main() {
const coldStartCount = 10
coldStartDuration := whyIsThisSlow(coldStartCount)
lambda.Start(func (context.Context) (string, error) {
w := bytes.NewBuffer(nil)
const count = 10
duration := whyIsThisSlow(count)
fmt.Fprintf(w, "cold start: count: %d\n", coldStartCount)
fmt.Fprintf(w, "cold start: total dur: %s\n", coldStartDuration)
fmt.Fprintf(w, "cold start: avg dur %s\n", coldStartDuration / coldStartCount)
fmt.Fprintf(w, "count: %d\n", count)
fmt.Fprintf(w, "total dur: %s\n", duration)
fmt.Fprintf(w, "avg dur %s\n", duration / count)
return w.String(), nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment