protected override async Task RunAsync(CancellationToken ct) { Output.WriteLine(""); Output.WriteLine(ConsoleColor.Green, "Appends events to a single stream that has a maxCount."); Output.WriteLine(""); var streamStore = GetStore(); int numberOfMessagesToWrite = Input.ReadInt( "Number message to append: ", 1, 1000000); int messageJsonDataSize = Input.ReadInt("Size of Json (kb): ", 1, 1024); int maxCount = Input.ReadInt("Metadata maxCount: ", 0, 1000); int numberOfMessagesPerAmend = Input.ReadInt("Number of messages per amend: ", 1, 1000); int count = 1; const string streamId = "stream"; await streamStore.SetStreamMetadata(streamId, ExpectedVersion.NoStream, maxCount : maxCount, cancellationToken : ct); var messageNumbers = new int[numberOfMessagesPerAmend]; string jsonData = new string('a', messageJsonDataSize * 1024); var stopwatch = Stopwatch.StartNew(); while (count < numberOfMessagesToWrite) { for (int j = 0; j < numberOfMessagesPerAmend; j++) { messageNumbers[j] = count++; } await streamStore.AppendToStream(streamId, ExpectedVersion.Any, StreamStoreAcceptanceTests.CreateNewStreamMessages(jsonData, messageNumbers), cancellationToken : ct); Console.Write($"\r> {messageNumbers[numberOfMessagesPerAmend - 1]}"); } stopwatch.Stop(); var rate = Math.Round((decimal)count / stopwatch.ElapsedMilliseconds * 1000, 0); Output.WriteLine(""); Output.WriteLine($"> {count} messages written in {stopwatch.Elapsed} ({rate} m/s)"); var streampage = await streamStore.ReadStreamForwards(streamId, StreamVersion.Start, maxCount + 1, ct); Output.WriteLine($"> Stream Message length: {streampage.Messages.Length}"); }
private static async Task RunWrites( CancellationToken ct, int numberOfMessagesPerAmend, int numberOfStreams, int offset, string jsonData, IStreamStore streamStore) { var stopwatch = Stopwatch.StartNew(); var messageNumbers = new int[numberOfMessagesPerAmend]; int count = 1; for (int i = 0; i < numberOfStreams; i++) { ct.ThrowIfCancellationRequested(); try { for (int j = 0; j < numberOfMessagesPerAmend; j++) { messageNumbers[j] = count++; } var newmessages = StreamStoreAcceptanceTests .CreateNewStreamMessages(jsonData, messageNumbers); await streamStore.AppendToStream( $"stream-{i + offset}", ExpectedVersion.Any, newmessages, ct); //Console.Write($"> {messageNumbers[numberOfMessagesPerAmend - 1]}"); } catch (SqlException ex) when(ex.Number == -2) { // just timeout } catch (Exception ex) when(!(ex is TaskCanceledException)) { Output.WriteLine(ex.ToString()); } } stopwatch.Stop(); var rate = Math.Round((decimal)count / stopwatch.ElapsedMilliseconds * 1000, 0); Output.WriteLine(""); Output.WriteLine($"> {count - 1} messages written in {stopwatch.Elapsed} ({rate} m/s)"); }
public async Task <int> Append(IStreamStore streamStore, CancellationToken ct) { var numberOfStreams = Input.ReadInt("Number of streams: ", 1, 100000000); int messageJsonDataSize = Input.ReadInt("Size of Json (kb): ", 1, 1024); int numberOfMessagesPerAmend = Input.ReadInt("Number of messages per stream append: ", 1, 1000); string jsonData = new string('a', messageJsonDataSize * 1024); var stopwatch = Stopwatch.StartNew(); var messageNumbers = new int[numberOfMessagesPerAmend]; int count = 1; for (int i = 0; i < numberOfStreams; i++) { ct.ThrowIfCancellationRequested(); try { for (int j = 0; j < numberOfMessagesPerAmend; j++) { messageNumbers[j] = count++; } var newmessages = StreamStoreAcceptanceTests .CreateNewStreamMessages(jsonData, messageNumbers); await streamStore.AppendToStream( $"stream-{i}", ExpectedVersion.Any, newmessages, ct); Console.Write($"\r> {messageNumbers[numberOfMessagesPerAmend - 1]}"); } catch (Exception ex) when(!(ex is TaskCanceledException)) { Log.Logger.Error(ex, ex.Message); } } stopwatch.Stop(); var rate = Math.Round((decimal)count / stopwatch.ElapsedMilliseconds * 1000, 0); Output.WriteLine(""); Output.WriteLine($"> {count-1} messages written in {stopwatch.Elapsed} ({rate} m/s)"); return(count); }
private static async Task RunLoadTest(CancellationTokenSource cts, IStreamStore streamStore) { var tasks = new List <Task>(); int count = 0; for (int i = 0; i < Environment.ProcessorCount; i++) { var random = new Random(); var task = Task.Run(async() => { while (!cts.IsCancellationRequested) { try { int streamNumber = random.Next(0, 100); var eventNumber1 = Interlocked.Increment(ref count); var eventNumber2 = Interlocked.Increment(ref count); var newmessages = StreamStoreAcceptanceTests .CreateNewStreamMessages(eventNumber1, eventNumber2); var info = $"{streamNumber} - {newmessages[0].MessageId}," + $"{newmessages[1].MessageId}"; Log.Logger.Information($"Begin {info}"); await streamStore.AppendToStream( $"stream-{streamNumber}", ExpectedVersion.Any, newmessages, cts.Token); Log.Logger.Information($"End {info}"); Console.Write($"\r{eventNumber2}"); } catch (Exception ex) when(!(ex is TaskCanceledException)) { cts.Cancel(); Log.Logger.Error(ex, ex.Message); Console.WriteLine(ex); Console.ReadKey(); } } }, cts.Token); tasks.Add(task); } await Task.WhenAll(tasks); }
public async Task <int> Append(IStreamStore streamStore, CancellationToken ct) { int numberOfStreams = -1; Output.WriteLine(ConsoleColor.Yellow, "Number of streams:"); new Menu() .Add("1", () => numberOfStreams = 1) .Add("10", () => numberOfStreams = 10) .Add("100", () => numberOfStreams = 100) .Add("1000", () => numberOfStreams = 1000) .Add("10000", () => numberOfStreams = 10000) .Add("Custom", () => { }) .Display(); int parallelTasks = Input.ReadInt( $"Number of parallel write tasks (Processor count = {Environment.ProcessorCount}): ", 1, 100); int numberOfMessagesToWrite = Input.ReadInt( $"Number message to append: ", 1, 10000000); int messageJsonDataSize = Input.ReadInt("Size of Json (kb): ", 1, 1024); int numberOfMessagesPerAmend = Input.ReadInt("Number of messages per amend: ", 1, 1000); var tasks = new List <Task>(); int count = 0; string jsonData = new string('a', messageJsonDataSize * 1024); var stopwatch = Stopwatch.StartNew(); for (int i = 0; i < parallelTasks; i++) { var random = new Random(); var task = Task.Run(async() => { var messageNumbers = new int[numberOfMessagesPerAmend]; while (!ct.IsCancellationRequested && count < numberOfMessagesToWrite) { try { int streamNumber = random.Next(0, numberOfStreams); for (int j = 0; j < numberOfMessagesPerAmend; j++) { messageNumbers[j] = Interlocked.Increment(ref count); } var newmessages = StreamStoreAcceptanceTests .CreateNewStreamMessages(jsonData, messageNumbers); var info = $"{streamNumber} - {newmessages[0].MessageId}," + $"{newmessages[1].MessageId}"; Log.Logger.Information($"Begin {info}"); await streamStore.AppendToStream( $"stream-{streamNumber}", ExpectedVersion.Any, newmessages, ct); Log.Logger.Information($"End {info}"); Console.Write($"\r> {messageNumbers[numberOfMessagesPerAmend - 1]}"); } catch (Exception ex) when(!(ex is TaskCanceledException)) { Log.Logger.Error(ex, ex.Message); } } }, ct); tasks.Add(task); } await Task.WhenAll(tasks); stopwatch.Stop(); var rate = Math.Round((decimal)count / stopwatch.ElapsedMilliseconds * 1000, 0); Output.WriteLine(""); Output.WriteLine($"> {count} messages written in {stopwatch.Elapsed} ({rate} m/s)"); return(count); }
protected override async Task RunAsync(CancellationToken ct) { Output.WriteLine(""); Output.WriteLine(ConsoleColor.Green, "Subscribes to multiple individual streams and appends to each of them in turn, multiple times."); Output.WriteLine(""); var streamStore = GetStore(); int numberOfStreams = Input.ReadInt( "Number of independent streams, each will have it's own subscriber: ", 1, 1000); int numberOfAppends = Input.ReadInt("Number of times each stream will have an append: ", 1, 1000); string jsonData = "{}"; var subscriptions = new List <IDisposable>(); int messagesReceived = 0; for (int i = 0; i < numberOfStreams; i++) { var subscription = streamStore.SubscribeToStream( $"stream-{i}", StreamVersion.None, (_, __, ___) => { Interlocked.Increment(ref messagesReceived); return(Task.CompletedTask); }); subscriptions.Add(subscription); } int amendAcount = 0; int eventNumber = 1; var totalMessages = numberOfAppends * numberOfStreams; var stopwatch = Stopwatch.StartNew(); while (amendAcount < numberOfAppends) { for (int i = 0; i < numberOfStreams; i++) { var newStreamMessages = StreamStoreAcceptanceTests.CreateNewStreamMessages(jsonData, eventNumber); await streamStore.AppendToStream($"stream-{i}", ExpectedVersion.Any, newStreamMessages, ct); eventNumber++; } amendAcount++; Console.Write($"\r> Appending: {amendAcount} / {numberOfAppends} . Received: {messagesReceived} / {totalMessages}"); } while (messagesReceived < totalMessages) { Console.Write($"\r> Appending: {amendAcount} / {numberOfAppends} . Received: {messagesReceived} / {totalMessages}"); await Task.Delay(100, ct); } Console.WriteLine($"\r> Appending: {amendAcount} / {numberOfAppends} . Received: {messagesReceived} / {totalMessages}"); foreach (var subscription in subscriptions) { subscription.Dispose(); } Output.WriteLine($"> Complete in {stopwatch.Elapsed:c}"); }