Skip to content

Commit

Permalink
scenario request sleep
Browse files Browse the repository at this point in the history
yo tools and renaming
  • Loading branch information
oke11o committed Sep 18, 2023
1 parent 3a4308b commit 388f6e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
20 changes: 15 additions & 5 deletions components/providers/http_scenario/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func convertScenarioToAmmo(sc ScenarioConfig, reqs map[string]RequestConfig) (*A
iter := mp.NewNextIterator(time.Now().UnixNano())
result := &Ammo{name: sc.Name, minWaitingTime: time.Millisecond * time.Duration(sc.MinWaitingTime)}
for _, sh := range sc.Requests {
name, cnt, err := parseShootName(sh)
name, cnt, sleep, err := parseShootName(sh)
if err != nil {
return nil, fmt.Errorf("failed to parse shoot %s: %w", sh, err)
}
Expand All @@ -84,6 +84,9 @@ func convertScenarioToAmmo(sc ScenarioConfig, reqs map[string]RequestConfig) (*A
return nil, fmt.Errorf("request %s not found", name)
}
r := convertConfigToRequest(req, iter)
if sleep > 0 {
r.sleep += time.Millisecond * time.Duration(sleep)
}
for i := 0; i < cnt; i++ {
result.Requests = append(result.Requests, r)
}
Expand Down Expand Up @@ -117,19 +120,26 @@ func convertConfigToRequest(req RequestConfig, iter mp.Iterator) Request {
return result
}

func parseShootName(shoot string) (string, int, error) {
func parseShootName(shoot string) (string, int, int, error) {
name, args, err := str.ParseStringFunc(shoot)
if err != nil {
return "", 0, err
return "", 0, 0, err
}
cnt := 1
if len(args) > 0 && args[0] != "" {
cnt, err = strconv.Atoi(args[0])
if err != nil {
return "", 0, fmt.Errorf("failed to parse count: %w", err)
return "", 0, 0, fmt.Errorf("failed to parse count: %w", err)
}
}
sleep := 0
if len(args) > 1 && args[1] != "" {
sleep, err = strconv.Atoi(args[1])
if err != nil {
return "", 0, 0, fmt.Errorf("failed to parse count: %w", err)
}
}
return name, cnt, nil
return name, cnt, sleep, nil
}

func spreadNames(input []ScenarioConfig) (map[string]int, int) {
Expand Down
9 changes: 5 additions & 4 deletions components/providers/http_scenario/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ func TestParseShootName(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
name, cnt, err := parseShootName(tc.input)
name, cnt, sleep, err := parseShootName(tc.input)
if tc.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.wantName, name, "Name does not match for input: %s", tc.input)
assert.Equal(t, tc.wantSleep, sleep, "Name does not match for input: %s", tc.input)
assert.Equal(t, tc.wantCnt, cnt, "Count does not match for input: %s", tc.input)
})
}
Expand Down Expand Up @@ -198,9 +199,9 @@ func Test_convertScenarioToAmmo(t *testing.T) {
Requests: []Request{
convertConfigToRequestWithSleep(req1, 0),
convertConfigToRequestWithSleep(req2, 0),
convertConfigToRequestWithSleep(req2, 0),
convertConfigToRequestWithSleep(req2, 0),
convertConfigToRequestWithSleep(req2, time.Millisecond*500),
convertConfigToRequestWithSleep(req2, time.Millisecond*100),
convertConfigToRequestWithSleep(req2, time.Millisecond*100),
convertConfigToRequestWithSleep(req2, time.Millisecond*600),
},
},
wantErr: false,
Expand Down

0 comments on commit 388f6e4

Please sign in to comment.