aws / aws/aws-sdk-net

Event Bus PutEventsAsync is slow on first execution

Open
#3,046 10 comments 1 reaction 0 assignees View on GitHub
bug p2 perf
Dominant language
C#
Stars
140
Forks
891
Avg merge
21h 51m
Merged PRs (30d)
10

Description

### Describe the bug

On the first execution, the PutEventsAsync is running slow.

My lambda with a Dotnet api project was running slow, so I created a single function to send an event and it also runs slow and I tracked the slowest part to the AWS Command PutEventsAsync.

Here is my code:

### Expected Behavior

I expected this command to run in less than 50ms.

### Current Behavior

It takes over 5 seconds to issue the command the first time.

### Reproduction Steps

Execute the lambda function

```
using Amazon.CloudWatchEvents;
using Amazon.CloudWatchEvents.Model;
using Amazon.Lambda.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Diagnostics;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace PutEventTimingTest;

public class Function
{
private class TestClass
{
public int Id { get; set; }
public string Name { get; set; }
}

public async Task FunctionHandler(string input, ILambdaContext context)
{
var services = new ServiceCollection();
services.AddAWSService();

ServiceProvider serviceProvider = services.BuildServiceProvider();

var eventService = serviceProvider.GetRequiredService();

var test = new TestClass { Id = 1, Name = "Slow Function Test" };

var putEvent = new PutEventsRequest
{
Entries = new List {
new PutEventsRequestEntry {
Source = "testSource",
Detail = JsonSerializer.Serialize(test, new JsonSerializerOptions {
MaxDepth = 10,
ReferenceHandler = ReferenceHandler.Preserve,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
}),
DetailType = "slowTestEvent",
Time = DateTime.UtcNow,
EventBusName = "testBus"
} }
};

var stopwatch = Stopwatch.StartNew();
await eventService.PutEventsAsync(putEvent);
stopwatch.Stop();
Console.WriteLine($"put took {stopwatch.ElapsedMilliseconds}ms");
}
}
```

### Possible Solution

unknown.

### Additional Information/Context

_No response_

### AWS .NET SDK and/or Package version used





### Targeted .NET Platform

.net 6

### Operating System and version

Amazon Linux (Lambda)

Contributor guide

Open the contributing guide

Research direction

The reproduction is in Function.FunctionHandler and measures Amazon.CloudWatchEvents PutEventsAsync on .NET 6 in Lambda, using the listed package versions. Start by running the example and comparing the first call with later calls, then investigate the confirmed source of the latency and document a reproducible fix or explanation.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, csharp
Domain
backend, cloud, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.