Skip to content

Instantly share code, notes, and snippets.

@janovesk
Created November 10, 2016 12:58
Show Gist options
  • Save janovesk/e2f7de54606e4dfa597e328638a21a95 to your computer and use it in GitHub Desktop.
Save janovesk/e2f7de54606e4dfa597e328638a21a95 to your computer and use it in GitHub Desktop.
NSB perf testing
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;
static class ConsoleApplication1
{
public static int BatchSize = 50000;
public static int MessageCount = 0;
public static SemaphoreSlim Done = new SemaphoreSlim(0, 1);
static void Main()
{
AsyncMain().GetAwaiter().GetResult();
}
static async Task AsyncMain()
{
Console.Title = "ConsoleApplication1";
var endpointConfiguration = new EndpointConfiguration("ConsoleApplication1");
endpointConfiguration.UsePersistence<InMemoryPersistence>();
endpointConfiguration.EnableInstallers();
endpointConfiguration.SendFailedMessagesTo("error");
endpointConfiguration.UseTransport<MsmqTransport>().Transactions(TransportTransactionMode.None);
endpointConfiguration.LimitMessageProcessingConcurrencyTo(10);
var recoverability = endpointConfiguration.Recoverability();
recoverability.Immediate(
immediate =>
{
immediate.NumberOfRetries(0);
});
recoverability.Delayed(
delayed =>
{
delayed.NumberOfRetries(0);
});
var endpointInstance = await Endpoint.Start(endpointConfiguration)
.ConfigureAwait(false);
try
{
//await Generate(endpointInstance);
await TimeHandler();
Console.ReadLine();
}
finally
{
await endpointInstance.Stop()
.ConfigureAwait(false);
}
}
static async Task TimeHandler()
{
Console.WriteLine($"Handling {BatchSize} messages.");
var w = new Stopwatch();
w.Start();
await Done.WaitAsync();
w.Stop();
Console.WriteLine($"Handled {MessageCount} messages in {w.Elapsed}. {((double)BatchSize/w.ElapsedMilliseconds)*1000} msg/s");
}
static async Task Generate(IEndpointInstance endpointInstance)
{
var w = new Stopwatch();
w.Start();
Console.WriteLine($"Sending {BatchSize} messages");
for (var i = 0; i < BatchSize; i++)
{
var options = new SendOptions();
options.SetDestination("dump");
await endpointInstance.Send(new MyMessage(), options);
}
w.Stop();
Console.WriteLine($"Sent {BatchSize} messages in {w.Elapsed}. {((double)BatchSize /w.ElapsedMilliseconds)*1000} msg/s");
}
}
public class MyHandler : IHandleMessages<MyMessage>
{
static ILog log = LogManager.GetLogger<MyHandler>();
#region MessageHandler
public async Task Handle(MyMessage message, IMessageHandlerContext context)
{
var options = new SendOptions();
options.SetDestination("dump_done");
await context.Send(message, options);
Interlocked.Increment(ref ConsoleApplication1.MessageCount);
if (ConsoleApplication1.MessageCount == ConsoleApplication1.BatchSize)
ConsoleApplication1.Done.Release(1);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment