Skip to content

Commit 33abc7e

Browse files
authored
Merge pull request #180 from deploymenttheory/dev
Adjust concurrency logic and metrics in the concurrency package, and …
2 parents 0653d4d + bc87a48 commit 33abc7e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

concurrency/handler.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ type ConcurrencyMetrics struct {
3535
Lock sync.Mutex // Lock for throughput metrics/
3636
}
3737
ResponseTimeVariability struct { // Metrics related to response time variability
38-
Total time.Duration // Total response time for all requests
39-
Average time.Duration // Average response time across all requests
40-
Variance float64 // Variance of response times
41-
Count int64 // Count of responses used for calculating response time variability
42-
Lock sync.Mutex // Lock for response time variability metrics
43-
StdDevThreshold float64 // Maximum acceptable standard deviation for adjusting concurrency
38+
Total time.Duration // Total response time for all requests
39+
Average time.Duration // Average response time across all requests
40+
Variance float64 // Variance of response times
41+
Count int64 // Count of responses used for calculating response time variability
42+
Lock sync.Mutex // Lock for response time variability metrics
43+
StdDevThreshold float64 // Maximum acceptable standard deviation for adjusting concurrency
44+
DebounceScaleDownCount int // Counter to manage scale down actions after consecutive triggers
4445
}
4546
ResponseCodeMetrics struct {
4647
ErrorRate float64 // Error rate calculated as (TotalRateLimitErrors + 5xxErrors) / TotalRequests

concurrency/metrics.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ var responseTimes []time.Duration
199199
func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.Duration) int {
200200
// Append the latest response time
201201
responseTimes = append(responseTimes, responseTime)
202-
if len(responseTimes) > 5 { // Use the last 5 measurements
202+
if len(responseTimes) > 10 { // Use the last 10 measurements for a smoother average
203203
responseTimes = responseTimes[1:]
204204
}
205205

206-
// Calculate average response time from the slice
206+
// Calculate moving average response time from the slice
207207
var sum time.Duration
208208
for _, rt := range responseTimes {
209209
sum += rt
@@ -218,11 +218,20 @@ func (ch *ConcurrencyHandler) MonitorResponseTimeVariability(responseTime time.D
218218
variance := varianceSum / float64(len(responseTimes))
219219
stdDev := math.Sqrt(variance)
220220

221-
// Determine action based on standard deviation against a higher threshold
222-
if stdDev > (ch.Metrics.ResponseTimeVariability.StdDevThreshold * 1.5) { // Increased threshold
223-
return -1 // Suggest decrease concurrency
224-
} else if stdDev <= ch.Metrics.ResponseTimeVariability.StdDevThreshold && len(ch.sem) < MaxConcurrency {
225-
return 1 // Suggest increase concurrency if there is capacity
221+
// Action determination with debounce effect
222+
// Requires keeping track of how often the stdDev threshold has been exceeded
223+
const debounceCount = 3 // e.g., threshold must be exceeded in 3 consecutive checks to act
224+
if stdDev > (ch.Metrics.ResponseTimeVariability.StdDevThreshold * 1.5) {
225+
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount++
226+
if ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount >= debounceCount {
227+
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 // reset counter after action
228+
return -1 // Suggest decrease concurrency
229+
}
230+
} else {
231+
ch.Metrics.ResponseTimeVariability.DebounceScaleDownCount = 0 // reset counter if condition not met
232+
if stdDev <= ch.Metrics.ResponseTimeVariability.StdDevThreshold && len(ch.sem) < MaxConcurrency {
233+
return 1 // Suggest increase concurrency if there is capacity
234+
}
226235
}
227236
return 0
228237
}

0 commit comments

Comments
 (0)