aws / aws-lambda-go

Libraries, samples and tools to help Go developers develop AWS Lambda functions.
Apache License 2.0
3.62k stars 550 forks source link

The generic handler is slower than the dynamic handler #522

Closed dcu closed 10 months ago

dcu commented 1 year ago

From the following benchmarks:

func BenchmarkColdStartDynamic(b *testing.B) {
    b.StopTimer()
    server, _ := runtimeAPIServer("null", 1) // serve a single invoke, and then cause an internal error
    expected := "expected"
    actual := "unexpected"

    os.Setenv("AWS_LAMBDA_RUNTIME_API", strings.Split(server.URL, "://")[1])
    defer os.Unsetenv("AWS_LAMBDA_RUNTIME_API")
    logFatalf = func(format string, v ...interface{}) {}
    defer func() { logFatalf = log.Fatalf }()

    b.StartTimer()
    for i := 0; i < b.N; i++ {
        StartWithOptions(func(ctx context.Context) error {
            actual, _ = ctx.Value(ctxTestKey{}).(string)
            return nil
        }, WithContext(context.WithValue(context.Background(), ctxTestKey{}, expected)))
    }
    b.StopTimer()

    assert.Equal(b, expected, actual)
}

func BenchmarkColdStartGeneric(b *testing.B) {
    b.StopTimer()
    server, _ := runtimeAPIServer("null", 1) // serve a single invoke, and then cause an internal error
    expected := "expected"
    actual := "unexpected"

    os.Setenv("AWS_LAMBDA_RUNTIME_API", strings.Split(server.URL, "://")[1])
    defer os.Unsetenv("AWS_LAMBDA_RUNTIME_API")
    logFatalf = func(format string, v ...interface{}) {}
    defer func() { logFatalf = log.Fatalf }()

    b.StartTimer()
    for i := 0; i < b.N; i++ {
        StartHandlerFunc(func(ctx context.Context, _ any) (any, error) {
            actual, _ = ctx.Value(ctxTestKey{}).(string)
            return nil, nil
        }, WithContext(context.WithValue(context.Background(), ctxTestKey{}, expected)))
    }
    b.StopTimer()

    assert.Equal(b, expected, actual)
}

I'm getting:

BenchmarkColdStartDynamic-8        10000        134489 ns/op       17937 B/op        142 allocs/op
BenchmarkColdStartGeneric-8         7624        354916 ns/op       14515 B/op        127 allocs/op