private static double ComputeUtilization(AmbientBottleneckUtilizationAlgorithm limitType, long totalStopwatchTicks, double limitUsed, double?limit, TimeSpan?limitPeriod) { double result; if (limit == null || Math.Abs(limit.Value) < 1) { limit = 1.0; } double limitPeriodStopwatchTicks = (limitPeriod == null) ? 1.0 : TimeSpanExtensions.TimeSpanTicksToStopwatchTicks(limitPeriod.Value.Ticks); if (totalStopwatchTicks < 1) { totalStopwatchTicks = 1; } switch (limitType) { case AmbientBottleneckUtilizationAlgorithm.Linear: result = (1.0 * limitUsed / totalStopwatchTicks) / (1.0 * limit.Value / limitPeriodStopwatchTicks); break; case AmbientBottleneckUtilizationAlgorithm.ExponentialLimitApproach: result = 1.0 - 1.0 / (2.0 * limitUsed / totalStopwatchTicks + 1.0); break; default: case AmbientBottleneckUtilizationAlgorithm.Zero: result = 0.0; break; } return(result); }
/// <summary> /// Constructs an AmbientBottleneck with the specified properties. /// </summary> /// <param name="id">The string identifier for the bottleneck.</param> /// <param name="utilizationAlgorithm">An <see cref="AmbientBottleneckUtilizationAlgorithm"/> indicating the algorithm to use to calculate the utilization.</param> /// <param name="automatic">Whether or not the bottleneck's usage is measured automaticall or using <see cref="AmbientBottleneckAccessor.SetUsage"/> or <see cref="AmbientBottleneckAccessor.AddUsage"/>.</param> /// <param name="description">A description of the bottleneck.</param> /// <param name="limit">An optional limit (for automatic bottlenecks, in units of stopwatch ticks).</param> /// <param name="limitPeriod">A <see cref="TimeSpan"/> indicating the period during which the limit is applied.</param> public AmbientBottleneck(string id, AmbientBottleneckUtilizationAlgorithm utilizationAlgorithm, bool automatic, string description, double?limit = null, TimeSpan?limitPeriod = null) { if (limit <= 0.0) { throw new ArgumentOutOfRangeException(nameof(limit), "The bottleneck limit must be null or positive!"); } if (limitPeriod?.Ticks <= 0.0) { throw new ArgumentOutOfRangeException(nameof(limitPeriod), "The bottleneck limitPeriod must be null or positive!"); } Id = id; UtilizationAlgorithm = utilizationAlgorithm; Automatic = automatic; Description = description; Limit = limit; LimitPeriod = limitPeriod; }