コード例 #1
0
ファイル: TestGaps.cs プロジェクト: dealproc/SqlStreamStore
        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 = MessageFactory
                                      .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)");
        }
コード例 #2
0
        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 (bytes): ", 1, 1024 * 1024);

            int numberOfMessagesPerAmend = Input.ReadInt("Number of messages per stream append: ", 1, 1000);

            string jsonData = new string('a', messageJsonDataSize);

            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 = MessageFactory
                                      .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);
        }
コード例 #3
0
        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, dispose) = GetStore();

            try
            {
                int numberOfMessagesToWrite = Input.ReadInt(
                    "Number message to append: ",
                    1,
                    1000000);

                int messageJsonDataSize = Input.ReadInt("Size of Json (bytes): ", 1, 1024 * 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,
                                                     MessageFactory.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}");
            }
            finally
            {
                dispose();
            }
        }
コード例 #4
0
        public override async Task Run(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, dispose) = await GetStore(ct);

            try
            {
                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 = MessageFactory.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}");
            }
            finally
            {
                dispose();
            }
        }
コード例 #5
0
        private async Task <int> Append(IStreamStore streamStore, CancellationToken ct)
        {
            var numberOfStreams         = 100;
            int parallelTasks           = 16;
            int numberOfMessagesToWrite = 100;
            //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 = 1024;
            //int messageJsonDataSize = Input.ReadInt("Size of Json (bytes): ", 1, 10 * 1024 * 1024);

            int numberOfMessagesPerAmend = 2;
            //int numberOfMessagesPerAmend = Input.ReadInt("Number of messages per append: ", 1, 1000);

            var tasks = new List <Task>();
            int count = 0;

            Queue <int> streamsToInsert = new Queue <int>(Enumerable.Range(0, numberOfStreams));

            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() =>
                {
                    while (!ct.IsCancellationRequested && streamsToInsert.TryDequeue(out var streamNumber))
                    {
                        var messageNumbers = new int[numberOfMessagesPerAmend];
                        try
                        {
                            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.NoStream,
                                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);
        }