Example #1
0
        public ResponseTimeInformation ExecuteAllPendingLazyOperations()
        {
            if (pendingLazyOperations.Count == 0)
            {
                return(new ResponseTimeInformation());
            }

            try
            {
                var sw = Stopwatch.StartNew();

                IncrementRequestCount();

                var responseTimeDuration = new ResponseTimeInformation();

                while (ExecuteLazyOperationsSingleStep(responseTimeDuration))
                {
                    ThreadSleep.Sleep(100);
                }

                responseTimeDuration.ComputeServerTotal();


                foreach (var pendingLazyOperation in pendingLazyOperations)
                {
                    Action <object> value;
                    if (onEvaluateLazy.TryGetValue(pendingLazyOperation, out value))
                    {
                        value(pendingLazyOperation.Result);
                    }
                }
                responseTimeDuration.TotalClientDuration = sw.Elapsed;
                return(responseTimeDuration);
            }
            finally
            {
                pendingLazyOperations.Clear();
            }
        }
Example #2
0
        private bool ExecuteLazyOperationsSingleStep(ResponseTimeInformation responseTimeInformation)
        {
            var disposables = pendingLazyOperations.Select(x => x.EnterContext()).Where(x => x != null).ToList();

            try
            {
                if (DatabaseCommands is ServerClient) // server mode
                {
                    var requests  = pendingLazyOperations.Select(x => x.CreateRequest()).ToArray();
                    var responses = DatabaseCommands.MultiGet(requests);

                    for (int i = 0; i < pendingLazyOperations.Count; i++)
                    {
                        long totalTime;
                        long.TryParse(responses[i].Headers["Temp-Request-Time"], out totalTime);

                        responseTimeInformation.DurationBreakdown.Add(new ResponseTimeItem
                        {
                            Url      = requests[i].UrlAndQuery,
                            Duration = TimeSpan.FromMilliseconds(totalTime)
                        });
                        if (responses[i].RequestHasErrors())
                        {
                            throw new InvalidOperationException("Got an error from server, status code: " + responses[i].Status +
                                                                Environment.NewLine + responses[i].Result);
                        }
                        pendingLazyOperations[i].HandleResponse(responses[i]);
                        if (pendingLazyOperations[i].RequiresRetry)
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
                else // embedded mode
                {
                    var responses = new List <object>();
                    foreach (var lazyOp in pendingLazyOperations)
                    {
                        var sw     = Stopwatch.StartNew();
                        var result = lazyOp.ExecuteEmbedded(DatabaseCommands);
                        responses.Add(result);
                        responseTimeInformation.DurationBreakdown.Add(new ResponseTimeItem
                        {
                            Url      = lazyOp.ToString(),
                            Duration = sw.Elapsed
                        });
                    }

                    for (int i = 0; i < pendingLazyOperations.Count; i++)
                    {
                        pendingLazyOperations[i].HandleEmbeddedResponse(responses[i]);
                        if (pendingLazyOperations[i].RequiresRetry)
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
            }
            finally
            {
                foreach (var disposable in disposables)
                {
                    disposable.Dispose();
                }
            }
        }