示例#1
0
        public override async Task <FactorialReply> Factorial(FactorialRequest request, ServerCallContext context)
        {
            // Obtain the number of available threads in ThreadPool
            ThreadPool.GetAvailableThreads(out var availableThreads, out _);
            // The number of available threads is the example of Gauge metric
            // Send gauge metric to StatsD (using JustEat.StatsD nuget)
            _stats.Gauge(availableThreads, "GaugeAvailableThreads");

            // Increment a counter metric for incoming requests
            _stats.Increment("CountRequests");

            // The method _stats.Time() will calculate the time while the _semaphoreSlim.WaitAsync() were waiting
            // and send the metric to StatsD
            await _stats.Time("TimeWait", async f => await _semaphoreSlim.WaitAsync());

            try
            {
                // Again measure time length of calculation and send it to StatsD
                var result = await _stats.Time("TimeCalculation", async t => await CalculateFactorialAsync(request.Factor));

                // Increment a counter of processed requests
                _stats.Increment("CountProcessed");

                return(await Task.FromResult(new FactorialReply
                {
                    Result = result
                }));
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
示例#2
0
        public async Task Run()
        {
            await Task.Yield();

            var sw = new Stopwatch();

            sw.Start();
            do
            {
                await Task.Delay(100);

                ThreadPool.QueueUserWorkItem(async _ =>
                {
                    ThreadPool.GetAvailableThreads(out var availableThreads, out var _);
                    _stats.Gauge(availableThreads, "GaugeAvailableThreads");

                    _stats.Increment("CountRequests");
                    await _stats.Time("TimeWait", async f => await _semaphoreSlim.WaitAsync());

                    try
                    {
                        var parameter     = new Random(DateTime.Now.Millisecond).Next(20);
                        using var channel = GrpcChannel.ForAddress(_settings.WorkerServiceEndpoint);
                        var client        = new Worker.WorkerClient(channel);
                        var reply         = await client.FactorialAsync(new FactorialRequest {
                            Factor = parameter
                        });
                        _stats.Increment("CountProcessed");
                    }
                    finally
                    {
                        _semaphoreSlim.Release();
                    }
                });
            } while (_settings.LoadSecondsInterval == 0 || (sw.ElapsedMilliseconds <= _settings.LoadSecondsInterval * 1000));
            sw.Stop();
        }