-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--rps-per-worker-limit and --ratelimit-window flags
when set, ratelimit operations during the prepare and the main phase of the bench. The ratelimit applies to each concurrent worker and not globally.
- Loading branch information
Showing
15 changed files
with
213 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package bench | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// | ||
// RpsPerWorkerLimit is not race condition proof | ||
// it must be used in a single goroutine | ||
// | ||
|
||
type RpsPerWorkerLimit struct { | ||
limit float64 | ||
window time.Duration | ||
counter int64 | ||
start time.Time | ||
} | ||
|
||
func InitRpsPerWorkerLimit(limit float64, window time.Duration) RpsPerWorkerLimit { | ||
return RpsPerWorkerLimit{ | ||
limit: limit, | ||
window: window, | ||
counter: 0, | ||
} | ||
} | ||
|
||
// Limit returns true if the function has been interrupted by 'done', false otherwise | ||
// Check if there is room for a new request, if not, sleep for the duration | ||
// that will limit the requests rate whithin the ratelimit | ||
func (r *RpsPerWorkerLimit) Limit(done <-chan struct{}) bool { | ||
if r.limit <= 0 { | ||
return false | ||
} | ||
|
||
// init the relative clock when first used | ||
if r.start.IsZero() { | ||
r.start = time.Now() | ||
} | ||
|
||
// calculate the time to sleep before next request to stay under the limit within the window | ||
// sleepDuration <= 0: we have room for new requests, no need to ratelimit | ||
// sleepDuration > 0: no more room for new requests, we have to wait before allowing new requests | ||
elapsed := time.Since(r.start) | ||
sleepDuration := time.Duration(float64(r.counter)/(r.limit/float64(r.window))) - elapsed | ||
|
||
// increment the number of requests | ||
r.counter++ | ||
|
||
// sleep for sleepDuration and allow to be interrupted by done | ||
// if sleepDuration is <= 0, it will immediately expire | ||
timer := time.NewTimer(sleepDuration) | ||
select { | ||
case <-done: | ||
return true | ||
case <-timer.C: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.