Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to skip invalid assert conditions #6

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type RunnerConfig struct {
AssertInvalid bool `config:"assert_invalid"`

ContinueOnAssertInvalid bool `config:"continue_on_assert_invalid"`
SkipInvalidAssert bool `config:"skip_invalid_assert"`

// Exit(2) if any error occurred
AssertError bool `config:"assert_error"`
Expand Down
37 changes: 24 additions & 13 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ type LoadGenerator struct {
}

type LoadStats struct {
TotReqSize int64
TotRespSize int64
TotDuration time.Duration
MinRequestTime time.Duration
MaxRequestTime time.Duration
NumRequests int
NumErrs int
NumInvalid int
StatusCode map[int]int
TotReqSize int64
TotRespSize int64
TotDuration time.Duration
MinRequestTime time.Duration
MaxRequestTime time.Duration
NumRequests int
NumErrs int
NumAssertInvalid int
NumAssertSkipped int
StatusCode map[int]int
}

var (
Expand Down Expand Up @@ -163,7 +164,7 @@ func doRequest(config *LoaderConfig, globalCtx util.MapStr, req *fasthttp.Reques

if err != nil {
loadStats.NumErrs++
loadStats.NumInvalid++
loadStats.NumAssertInvalid++
}

if !config.RunnerConfig.NoSizeStats {
Expand Down Expand Up @@ -222,18 +223,22 @@ func doRequest(config *LoaderConfig, globalCtx util.MapStr, req *fasthttp.Reques
}
condition, buildErr := conditions.NewCondition(item.Assert)
if buildErr != nil {
if config.RunnerConfig.SkipInvalidAssert{
loadStats.NumAssertSkipped++
continue
}
log.Errorf("failed to build conditions while assert existed, error: %+v", buildErr)
loadStats.NumInvalid++
loadStats.NumAssertInvalid++
return
}
if !condition.Check(event) {
loadStats.NumInvalid++
loadStats.NumAssertInvalid++
if item.Request != nil {
log.Errorf("%s %s, assertion failed, skipping subsequent requests", item.Request.Method, item.Request.Url)
}

if !config.RunnerConfig.ContinueOnAssertInvalid {
log.Infof("assertion failed, skipping subsequent requests,", item.Assert, ", event:", event)
log.Info("assertion failed, skipping subsequent requests,", util.MustToJSON(item.Assert), ", event:", util.MustToJSON(event))
return false, err
}
}
Expand Down Expand Up @@ -476,6 +481,12 @@ func (cfg *LoadGenerator) Warmup(config *LoaderConfig) int {
globalCtx := util.MapStr{}
for _, v := range config.Requests {
v.prepareRequest(config, globalCtx, req)

if !req.Validate(){
log.Errorf("invalid request: %v",req.String())
panic("invalid request")
}

next, err := doRequest(config, globalCtx, req, resp, &v, loadStats, nil)
for k, _ := range loadStats.StatusCode {
if len(config.RunnerConfig.ValidStatusCodesDuringWarmup) > 0 {
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func startLoader(cfg *LoaderConfig) *LoadStats {
loadGen.Stop()
case stats := <-statsAggregator:
aggStats.NumErrs += stats.NumErrs
aggStats.NumInvalid += stats.NumInvalid
aggStats.NumAssertInvalid += stats.NumAssertInvalid
aggStats.NumAssertSkipped += stats.NumAssertSkipped
aggStats.NumRequests += stats.NumRequests
aggStats.TotReqSize += stats.TotReqSize
aggStats.TotRespSize += stats.TotRespSize
Expand Down Expand Up @@ -217,7 +218,8 @@ func startLoader(cfg *LoaderConfig) *LoadStats {
}

if cfg.RunnerConfig.AssertInvalid {
fmt.Printf("Number of Invalid:\t%v\n", aggStats.NumInvalid)
fmt.Printf("Assert Invalid:\t\t%v\n", aggStats.NumAssertInvalid)
fmt.Printf("Assert Skipped:\t\t%v\n", aggStats.NumAssertSkipped)
}

for k, v := range aggStats.StatusCode {
Expand Down Expand Up @@ -440,7 +442,7 @@ func runLoaderConfig(config *LoaderConfig) int {

aggStats := startLoader(config)
if aggStats != nil {
if config.RunnerConfig.AssertInvalid && aggStats.NumInvalid > 0 {
if config.RunnerConfig.AssertInvalid && aggStats.NumAssertInvalid > 0 {
return 1
}
if config.RunnerConfig.AssertError && aggStats.NumErrs > 0 {
Expand Down
Loading