Esempio n. 1
0
#pragma warning restore RCS1213 // Remove unused member declaration.
#pragma warning restore IDE0051 // Remove unused private members

        private static async Task ExecuteSimple(IJsonEventStore eventStore)
        {
#pragma warning disable IDE0059 // Unnecessary assignment of a value - allow for debug inspection
            var toDoListId = Guid.NewGuid();

            // Read and modify a todoitem using the simple aggregate.
            ToDoList toDoList = await ToDoList.ReadOrCreate(eventStore, toDoListId).ConfigureAwait(false);

            toDoList = toDoList.Initialize(DateTimeOffset.Now, "Bill Gates");
            toDoList = await toDoList.Commit().ConfigureAwait(false);

            // Now load it using the "json-specific" aggregate.
            ToDoListJson toDoListJson = await ToDoListJson.ReadOrCreate(eventStore, toDoListId).ConfigureAwait(false);

            toDoListJson = toDoListJson.AddToDoItem(Guid.NewGuid(), "This is my title", "This is my description");
            toDoListJson = toDoListJson.AddToDoItem(Guid.NewGuid(), "This is my second title", "This is my second description");
            toDoListJson = await toDoListJson.Commit().ConfigureAwait(false);

            // Reload the toDoList
            toDoList = await ToDoList.ReadOrCreate(eventStore, toDoListId).ConfigureAwait(false);

            toDoList = toDoList.AddToDoItem(Guid.NewGuid(), "This is another title", "This is another description");
            toDoList = toDoList.AddToDoItem(Guid.NewGuid(), "This is yet another title", "This is yet another description");
            toDoList = await toDoList.Commit().ConfigureAwait(false);

            // Reload the toDoListJson
            toDoListJson = await ToDoListJson.ReadOrCreate(eventStore, toDoListId).ConfigureAwait(false);

#pragma warning restore IDE0059 // Unnecessary assignment of a value
        }
Esempio n. 2
0
        private static async Task ExecuteVolume(IJsonEventStore eventStore)
        {
            // This is the ID of our aggregate - imagine this came in from the request, for example.
            Guid[] aggregateIds = Enumerable.Range(0, 625)
                                  .Select(_ => Guid.NewGuid())
                                  .ToArray();

            const int batchSize           = 625;
            const int iterations          = 50;
            const int nodesPerAggregate   = 8;
            const int minTimePerIteration = 0;
            const int eventsPerCommit     = 8;

            var aggregates = new ToDoListJson[aggregateIds.Length];

            Console.WriteLine("Initializing aggregates.");

            var loadSw = Stopwatch.StartNew();

            for (int i = 0; i < aggregateIds.Length; ++i)
            {
                Guid id = aggregateIds[i];
                aggregates[i] = ToDoListJson.Create(eventStore, id);
            }

            loadSw.Stop();

            Console.WriteLine($"Read {aggregateIds.Length} aggregates in {loadSw.ElapsedMilliseconds / 1000.0} seconds ({aggregateIds.Length / (loadSw.ElapsedMilliseconds / 1000.0)} agg/sec)");

            Console.WriteLine();

            var executeSw = Stopwatch.StartNew();

            var taskList = new Task <ToDoListJson> [batchSize];

            for (int i = 0; i < iterations; ++i)
            {
                Console.WriteLine($"Iteration {i}");
                for (int batch = 0; batch < aggregateIds.Length / batchSize; ++batch)
                {
                    DateTimeOffset startTime = DateTimeOffset.Now;

                    Console.WriteLine($"Batch {batch}");

                    var sw = Stopwatch.StartNew();

                    for (int node = 0; node < nodesPerAggregate; ++node)
                    {
                        for (int taskCount = 0; taskCount < batchSize; ++taskCount)
                        {
                            ToDoListJson toDoList = aggregates[(batch * batchSize) + taskCount];
                            for (int eventCount = 0; eventCount < eventsPerCommit; ++eventCount)
                            {
                                toDoList = toDoList.AddToDoItem(Guid.NewGuid(), "This is my title", "This is my description");
                            }

                            taskList[taskCount] = toDoList.Commit();
                        }

                        ToDoListJson[] batchAggregates = await Task.WhenAll(taskList).ConfigureAwait(false);

                        batchAggregates.CopyTo(aggregates, batch * batchSize);
                    }

                    sw.Stop();

                    Console.WriteLine($"{batchSize * nodesPerAggregate} commits in {sw.ElapsedMilliseconds}, ({(batchSize * nodesPerAggregate) / (sw.ElapsedMilliseconds / 1000.0)} commits/s)");
                    Console.WriteLine($"{(batchSize * nodesPerAggregate * eventsPerCommit) / (sw.ElapsedMilliseconds / 1000.0)} events/s");

                    double elapsed = (DateTimeOffset.Now - startTime).TotalMilliseconds;

                    // Rate limit
                    if (elapsed < minTimePerIteration)
                    {
                        int delay = minTimePerIteration - (int)elapsed;
                        Console.WriteLine($"Delaying {delay}ms");

                        await Task.Delay(delay).ConfigureAwait(false);
                    }
                }
            }

            executeSw.Stop();
            Console.WriteLine($"Executed {aggregateIds.Length * iterations * nodesPerAggregate} atomic commits in {executeSw.ElapsedMilliseconds / 1000.0} seconds ({aggregateIds.Length * iterations * nodesPerAggregate / (executeSw.ElapsedMilliseconds / 1000.0)} ) commits/sec");
            Console.WriteLine($"({aggregateIds.Length * iterations * nodesPerAggregate * eventsPerCommit / (executeSw.ElapsedMilliseconds / 1000.0)} ) events/sec");
        }