Пример #1
0
        /// <summary>
        /// Body of the polling thread.
        /// </summary>
        private void RunHandlerLoop(CompletionQueueSafeHandle cq, IProfiler optionalProfiler)
        {
            if (optionalProfiler != null)
            {
                Profilers.SetForCurrentThread(optionalProfiler);
            }

            CompletionQueueEvent ev;

            do
            {
                ev = cq.Next();
                if (ev.type == CompletionQueueEvent.CompletionType.OpComplete)
                {
                    bool   success = (ev.success != 0);
                    IntPtr tag     = ev.tag;
                    try
                    {
                        var callback = cq.CompletionRegistry.Extract(tag);
                        callback(success);
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception occured while invoking completion delegate");
                    }
                }
            }while (ev.type != CompletionQueueEvent.CompletionType.Shutdown);
        }
Пример #2
0
        private void RunUnary(Channel channel, IInterarrivalTimer timer, BasicProfiler optionalProfiler)
        {
            if (optionalProfiler != null)
            {
                Profilers.SetForCurrentThread(optionalProfiler);
            }

            bool profilerReset = false;

            var client    = new BenchmarkService.BenchmarkServiceClient(channel);
            var request   = CreateSimpleRequest();
            var stopwatch = new Stopwatch();

            while (!stoppedCts.Token.IsCancellationRequested)
            {
                // after the first stats reset, also reset the profiler.
                if (optionalProfiler != null && !profilerReset && statsResetCount.Count > 0)
                {
                    optionalProfiler.Reset();
                    profilerReset = true;
                }

                stopwatch.Restart();
                client.UnaryCall(request);
                stopwatch.Stop();

                // spec requires data point in nanoseconds.
                threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos);

                timer.WaitForNext();
            }
        }
Пример #3
0
        public void UnaryCallPerformance()
        {
            var profiler = new BasicProfiler();

            Profilers.SetForCurrentThread(profiler);

            helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) =>
            {
                return(request);
            });

            var callDetails = helper.CreateUnaryCall();

            for (int i = 0; i < 3000; i++)
            {
                Calls.BlockingUnaryCall(callDetails, "ABC");
            }

            profiler.Reset();

            for (int i = 0; i < 3000; i++)
            {
                Calls.BlockingUnaryCall(callDetails, "ABC");
            }
            profiler.Dump("latency_trace_csharp.txt");
        }
Пример #4
0
        /// <summary>
        /// Body of the polling thread.
        /// </summary>
        private void RunHandlerLoop(CompletionQueueSafeHandle cq, IProfiler optionalProfiler)
        {
            if (optionalProfiler != null)
            {
                Profilers.SetForCurrentThread(optionalProfiler);
            }

            CompletionQueueEvent ev;

            do
            {
                ev = cq.Next();
                if (ev.type == CompletionQueueEvent.CompletionType.OpComplete)
                {
                    bool   success = (ev.success != 0);
                    IntPtr tag     = ev.tag;
                    try
                    {
                        var callback = cq.CompletionRegistry.Extract(tag);
                        // Use cached delegates to avoid unnecessary allocations
                        if (!inlineHandlers)
                        {
                            queuedContinuationCounter.Increment();
                            ThreadPool.QueueUserWorkItem(success ? runCompletionQueueEventCallbackSuccess : runCompletionQueueEventCallbackFailure, callback);
                        }
                        else
                        {
                            RunCompletionQueueEventCallback(callback, success);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception occured while extracting event from completion registry.");
                    }
                }
            }while (ev.type != CompletionQueueEvent.CompletionType.Shutdown);

            // Continuations are running on default threadpool that consists of background threads.
            // GrpcThreadPool thread (a foreground thread) will not exit unless all queued work had
            // been finished to prevent terminating the continuations queued prematurely.
            int sleepIterations = 0;

            while (queuedContinuationCounter.Count != 0)
            {
                // Only happens on shutdown and having pending continuations shouldn't very common,
                // so sleeping here for a little bit is fine.
                if (sleepIterations >= MaxFinishContinuationsSleepTotalMillis / FinishContinuationsSleepMillis)
                {
                    Logger.Warning("Shutting down gRPC thread [{0}] with unfinished callbacks (Timed out waiting for callbacks to finish).",
                                   Thread.CurrentThread.Name);
                    break;
                }
                Thread.Sleep(FinishContinuationsSleepMillis);
                sleepIterations++;
            }
        }
Пример #5
0
        /// <summary>
        /// Body of the polling thread.
        /// </summary>
        private void RunHandlerLoop(CompletionQueueSafeHandle cq, IProfiler optionalProfiler)
        {
            if (optionalProfiler != null)
            {
                Profilers.SetForCurrentThread(optionalProfiler);
            }

            CompletionQueueEvent ev;

            do
            {
                ev = cq.Next();
                if (ev.type == CompletionQueueEvent.CompletionType.OpComplete)
                {
                    bool   success = (ev.success != 0);
                    IntPtr tag     = ev.tag;
                    try
                    {
                        var callback = cq.CompletionRegistry.Extract(tag);
                        // Use cached delegates to avoid unnecessary allocations
                        if (!inlineHandlers)
                        {
                            ThreadPool.QueueUserWorkItem(success ? RunCompletionQueueEventCallbackSuccess : RunCompletionQueueEventCallbackFailure, callback);
                        }
                        else
                        {
                            RunCompletionQueueEventCallback(callback, success);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Exception occured while extracting event from completion registry.");
                    }
                }
            }while (ev.type != CompletionQueueEvent.CompletionType.Shutdown);
        }