/// <summary> /// Calculates subsequent end times based on the surrounding duration values /// </summary> /// <param name="currentEndTime">The current end time.</param> /// <param name="adjustedDuration">Duration of the adjusted.</param> /// <param name="nextDuration">Duration of the next.</param> /// <returns></returns> private DateTime CalculateEndTime(DateTime currentEndTime, TimeSpan adjustedDuration, TimeSpan nextDuration) { TraceFactory.Logger.Debug("ET: {0}, AD: {1}, ND: {2}" .FormatWith(currentEndTime, adjustedDuration, nextDuration)); // Pick a simple way to "randomly" decide if the variance will be positive or negative. bool isPositive = currentEndTime.Ticks % 2 == 0; // Calculate the variance for the current duration, which has been adjusted to account for the end time // of the last segment. This asjusted duration may be larger or smaller than its default value. var variance = Variance(adjustedDuration); TraceFactory.Logger.Debug("Variance {0}".FormatWith(variance)); // This variance can't extend beyond 1/2 of the next duration, if it is positive. if (isPositive) { TimeSpan halfNextDuration = TimeSpanUtil.Divide(nextDuration, 2); if (variance > halfNextDuration) { // Keep the variance to some value less than 1/2 of this shorter duration variance = TimeSpanUtil.GetRandom(halfNextDuration); TraceFactory.Logger.Debug("Variance reduced {0}".FormatWith(variance)); } } // Return the new end time based on the current end time, plus the current duration, // then +/- the variance. var newDuration = currentEndTime.Add(adjustedDuration); return(isPositive ? newDuration.Add(variance) : newDuration.Subtract(variance)); }
private TimeSpan Variance(TimeSpan duration) { // Determine a variance maximum value by taking 12.5% of the duration's current size. The 12.5% value is // one half of an overall 25% value (12.5% on each side of the transition boundary). Then use that // maximum value to calculate a new random value between zero and the maximum value. TimeSpan varianceMax = TimeSpanUtil.Divide(duration, 8); return(TimeSpanUtil.GetRandom(varianceMax)); }