示例#1
0
        public void Concurrent_read_only_access_to_aggregate_history_can_occur_in_paralell()
        {
            var user = new User();

            user.Register("*****@*****.**", "password", Guid.NewGuid());
            using (var session = OpenSession(CreateStore()))
            {
                session.Save(user);
            }

            var iterations = 20;
            var delayEachTransactionByMilliseconds = 100;

            Action readUserHistory = () =>
            {
                using (var session = OpenSession(CreateStore()))
                {
                    using (var transaction = new TransactionScope())
                    {
                        ((IEventStoreReader)session).GetHistory(user.Id);
                        Thread.Sleep(TimeSpanExtensions.Milliseconds(delayEachTransactionByMilliseconds));
                        transaction.Complete();
                    }
                }
            };

            readUserHistory();//one warmup to get consistent times later.
            var timeForSingleTransactionalRead = (int)StopwatchExtensions.TimeExecution(readUserHistory).TotalMilliseconds;

            var timingsSummary = TimeAsserter.ExecuteThreaded(
                readUserHistory,
                iterations: iterations,
                timeIndividualExecutions: true,
                maxTotal: ((iterations * timeForSingleTransactionalRead) / 2).Milliseconds(),
                description: $"If access is serialized the time will be approximately {iterations * timeForSingleTransactionalRead} milliseconds. If parelellized it should be far below this value.");

            timingsSummary.Average.Should().BeLessThan(delayEachTransactionByMilliseconds.Milliseconds());

            timingsSummary.IndividualExecutionTimes.Sum().Should().BeGreaterThan(timingsSummary.Total);
        }
示例#2
0
        public static StopwatchExtensions.TimedExecutionSummary Execute
            (Action action,
            int iterations      = 1,
            TimeSpan?maxAverage = null,
            TimeSpan?maxTotal   = null,
            string description  = "",
            string timeFormat   = DefaultTimeFormat,
            int maxTries        = 3)
        {
            maxAverage = maxAverage != default(TimeSpan) ? maxAverage : TimeSpan.MaxValue;
            maxTotal   = maxTotal != default(TimeSpan) ? maxTotal : TimeSpan.MaxValue;

            Func <TimeSpan?, string> format = date => date?.ToString(timeFormat) ?? "";

            StopwatchExtensions.TimedExecutionSummary executionSummary = null;
            for (int tries = 1; tries <= maxTries; tries++)
            {
                executionSummary = StopwatchExtensions.TimeExecution(action: action, iterations: iterations);
                try
                {
                    RunAsserts(maxAverage, maxTotal, executionSummary, format);
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"Try: {tries} {e.GetType().FullName}: {e.Message}");
                    if (tries >= maxTries)
                    {
                        PrintSummary(iterations, maxAverage, maxTotal, description, format, executionSummary);
                        throw;
                    }
                    continue;
                }
                PrintSummary(iterations, maxAverage, maxTotal, description, format, executionSummary);
                break;
            }

            return(executionSummary);
        }