Skip to content

Commit

Permalink
Resolves #371 - Configurable Statistics Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Sep 12, 2023
1 parent 0724279 commit 738c106
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/crud_smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestCrudOnAResource(t *testing.T) {

httpclient.Initialize(1, 60)
httpclient.Initialize(1, 60, 0)

// Use a random account name to prevent collisions with other tests
id := rand.Int63()
Expand Down
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var rateLimit uint16

var requestTimeout float32

var statisticsFrequency uint16

var jqCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{
".data.",
Expand Down Expand Up @@ -103,6 +105,7 @@ func InitializeCmd() {
RootCmd.PersistentFlags().BoolVarP(&httpclient.DontLog2xxs, "silence-2xx", "", false, "Whether we should silence HTTP 2xx response code logging")

RootCmd.PersistentFlags().Float32VarP(&requestTimeout, "timeout", "", 60, "Request timeout in seconds (fractional values allowed)")
RootCmd.PersistentFlags().Uint16VarP(&statisticsFrequency, "statistics-frequency", "", 15, "If greater than 0 will print statistics this often")

RootCmd.PersistentFlags().BoolVarP(&aliases.SkipAliasProcessing, "skip-alias-processing", "", false, "if set, we don't process the response for aliases")
ResetStore.PersistentFlags().BoolVarP(&DeleteApplicationKeys, "delete-application-keys", "", false, "if set, we delete application keys as well")
Expand Down Expand Up @@ -186,8 +189,8 @@ Environment Variables
if env.EPCC_RATE_LIMIT != 0 {
rateLimit = env.EPCC_RATE_LIMIT
}
log.Debugf("Rate limit set to %d request per second ", rateLimit)
httpclient.Initialize(rateLimit, requestTimeout)
log.Debugf("Rate limit set to %d request per second, printing statistics every %d seconds ", rateLimit, statisticsFrequency)
httpclient.Initialize(rateLimit, requestTimeout, int(statisticsFrequency))

for _, runFunc := range persistentPreRunFuncs {
err := runFunc(cmd, args)
Expand Down
48 changes: 25 additions & 23 deletions external/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,38 @@ func init() {
}
}
}
}

go func() {
lastTotalRequests := uint64(0)
var Limit *rate.Limiter = nil

for {
time.Sleep(15 * time.Second)
func Initialize(rateLimit uint16, requestTimeout float32, statisticsFrequency int) {
Limit = rate.NewLimiter(rate.Limit(rateLimit), 1)
HttpClient.Timeout = time.Duration(int64(requestTimeout*1000) * int64(time.Millisecond))

statsLock.Lock()
if statisticsFrequency > 0 {
go func() {
lastTotalRequests := uint64(0)

deltaRequests := stats.totalRequests - lastTotalRequests
lastTotalRequests = stats.totalRequests
statsLock.Unlock()
for {
time.Sleep(time.Duration(statisticsFrequency) * time.Second)

if shutdown.ShutdownFlag.Load() {
break
}
statsLock.Lock()

if deltaRequests > 0 {
log.Infof("Total requests %d, requests in past 15 seconds %d, latest %d requests per second.", lastTotalRequests, deltaRequests, deltaRequests/15.0)
}
deltaRequests := stats.totalRequests - lastTotalRequests
lastTotalRequests = stats.totalRequests
statsLock.Unlock()

}
}()
}
if shutdown.ShutdownFlag.Load() {
break
}

var Limit *rate.Limiter = nil
if deltaRequests > 0 {
log.Infof("Total requests %d, requests in past %d seconds %d, latest %d requests per second.", lastTotalRequests, statisticsFrequency, deltaRequests, deltaRequests/uint64(statisticsFrequency))
}

func Initialize(rateLimit uint16, requestTimeout float32) {
Limit = rate.NewLimiter(rate.Limit(rateLimit), 1)
HttpClient.Timeout = time.Duration(int64(requestTimeout*1000) * int64(time.Millisecond))
}
}()
}
}

var Retry429 = false
Expand All @@ -113,9 +115,9 @@ func LogStats() {

for _, k := range keys {
if k == 0 {
counts += fmt.Sprintf("%d:%d, ", k, stats.respCodes[k])
} else {
counts += fmt.Sprintf("CONN_ERROR:%d, ", stats.respCodes[k])
} else {
counts += fmt.Sprintf("%d:%d, ", k, stats.respCodes[k])
}
}

Expand Down

0 comments on commit 738c106

Please sign in to comment.