Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SNS Load Testing Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.SimpleNotificationService;
using Newtonsoft.Json;
namespace sns_load_test
{
class Program
{
//Change this to control what SNS topic you are publishing to
const string TopicArn = "arn:aws:sns:ap-southeast-2:xxxxxxxxxxxxxxxxxxxx";
//Change this to control how many messages to send in the load test
const int NumberOfMessagesToSend = 100;
static void Main(string[] args)
{
Run().Wait();
}
static async Task Run()
{
List<string> serializedMessages = new List<string>();
for (int i = 0; i < NumberOfMessagesToSend; i++)
{
//Customise the content of your SNS messages as need be here
var message = new {
Property = "Hello World!"
};
serializedMessages.Add(JsonConvert.SerializeObject(message));
}
var snsClient = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.APSoutheast2);
IEnumerable<Task> tasks = serializedMessages.Select(async serializedMessage =>
{
try
{
await snsClient.PublishAsync(TopicArn, serializedMessage);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
});
await Task.WhenAll(tasks);
Console.WriteLine("Done!");
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>sns_load_test</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.3.2.10" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment