private async Task <int> Append(IStreamStore streamStore, CancellationToken ct) { var numberOfStreams = Input.ReadInt("Number of streams: ", 1, 100000000); 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 (bytes): ", 1, 10 * 1024 * 1024); int numberOfMessagesPerAmend = Input.ReadInt("Number of messages per append: ", 1, 1000); var tasks = new List <Task>(); int count = 0; string jsonData = new string('a', messageJsonDataSize); 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 = MessageFactory .CreateNewStreamMessages(jsonData, messageNumbers); var info = $"{streamNumber}"; 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); Output.WriteLine(ex.ToString()); } } }, 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); }