private async Task ReportObservedTPSAsync() { _lastReportTime = DateTime.Now; // Write TPS periodically while (_enabled) { try { // Wait for start of next interval await Task.Delay(GetMillisecondsToStartOfNextInterval()); // Note info for current interval, resetting interval aggretation data members DateTime currentTimeSnapshot = DateTime.Now; var recentLatencyTimes = Interlocked.Exchange(ref _intervalLatencyTimes, new ConcurrentDictionary <int, int>()); var recentTotalRequestCharge = Interlocked.Exchange(ref _totalRequestCharge, 0.0); // Calculate one second interval metrics IntervalMetrics m = InitPerSecondIntervalMetric(currentTimeSnapshot, currentTimeSnapshot - _lastReportTime, recentLatencyTimes, recentTotalRequestCharge); PopulateExtendedMetrics?.Invoke(m); PerSecondMetricsAvailable?.Invoke(m); // Update one minute interval metrics & post if needed UpdatePerMinuteIntervalMetrics(m); // Remember this interval _lastReportTime = currentTimeSnapshot; await Task.Delay(10); // Don't run too fast & report more than once per interval } catch (Exception x) { Tracer.TraceWarning("Ignoring exception on background metrics reporting thread. Exception = {0}", x.ToString()); } } }
private async Task ReportObservedTpsAsync(CancellationToken cancellationToken) { _lastReportTime = DateTime.Now; // Write TPS periodically while (true) { // Bail if cancelled cancellationToken.ThrowIfCancellationRequested(); try { // Wait for start of next interval await Task.Delay(GetMillisecondsToStartOfNextInterval(), cancellationToken); // Bail if cancelled cancellationToken.ThrowIfCancellationRequested(); // Note info for current interval, resetting interval aggretation data members DateTime currentTimeSnapshot = DateTime.Now; var recentLatencyTimes = Interlocked.Exchange(ref _intervalLatencyTimes, new ConcurrentDictionary <int, int>()); var recentCpuPercentageReported = Interlocked.Exchange(ref _totalCpuPercentageReported, 0.0); // Calculate one second interval metrics IntervalMetrics m = InitPerSecondIntervalMetric(currentTimeSnapshot, currentTimeSnapshot - _lastReportTime, recentLatencyTimes, recentCpuPercentageReported); PopulateExtendedMetrics?.Invoke(m); PerSecondMetricsAvailable?.Invoke(m); // Update one minute interval metrics & post if needed UpdatePerMinuteIntervalMetrics(m); // Remember this interval _lastReportTime = currentTimeSnapshot; // Don't run too fast & report more than once per interval await Task.Delay(10, cancellationToken); } catch (OperationCanceledException) { // Don't throw exception when cancelled, just exit silently break; } catch (Exception x) { Tracer.TraceWarning($"Ignoring exception on background metrics reporting thread. Exception = {x.ToString()}"); } } }