Skip to content

Commit 295add4

Browse files
author
Kevin Li
committed
Adding changes to accomadate GET requestS
1 parent 9aa57e6 commit 295add4

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

apirunner/apirunner.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"net/http"
3131
"net/url"
3232
"os"
33+
"regexp"
3334
"strconv"
3435
"strings"
3536
"time"
@@ -124,11 +125,12 @@ func executeAPIandCalculate(profileName string, apiURL string, command string, p
124125
var avgTime float64
125126
var totalTime float64
126127
var count float64
128+
isGetRequest, _ := regexp.MatchString("^(get|list|query|find)(\\w+)+$", command)
127129
if iterations != 1 {
128130
log.Infof("Calling API %s for %d number of iterations with parameters %s", command, iterations, params)
129131
for i := 1; i <= iterations; i++ {
130132
log.Infof("Started with iteration %d for the command %s", i, command)
131-
elapsedTime, apicount, result := executeAPI(apiURL, params)
133+
elapsedTime, apicount, result := executeAPI(apiURL, params, isGetRequest)
132134
count = apicount
133135
if elapsedTime < minTime {
134136
minTime = elapsedTime
@@ -145,7 +147,7 @@ func executeAPIandCalculate(profileName string, apiURL string, command string, p
145147
log.Infof("count [%.f] : Time in seconds [Min - %.2f] [Max - %.2f] [Avg - %.2f]\n", count, minTime, maxTime, avgTime)
146148
saveData(apiURL, count, minTime, maxTime, avgTime, page, pagesize, keyword, profileName, command, dbProfile, reportAppend)
147149
} else {
148-
elapsedTime, apicount, _ := executeAPI(apiURL, params)
150+
elapsedTime, apicount, _ := executeAPI(apiURL, params, isGetRequest)
149151
log.Infof("Elapsed time [%.2f seconds] for the count [%.0f]", elapsedTime, apicount)
150152
saveData(apiURL, count, elapsedTime, elapsedTime, elapsedTime, page, pagesize, keyword, profileName, command, dbProfile, reportAppend)
151153
}
@@ -251,16 +253,24 @@ func saveData(apiURL string, count float64, minTime float64, maxTime float64, av
251253
log.Info(message)
252254
}
253255

254-
func executeAPI(apiURL string, params url.Values) (float64, float64, bool) {
256+
func executeAPI(apiURL string, params url.Values, post bool) (float64, float64, bool) {
255257
// Send the API request and calculate the time
256-
data := strings.NewReader(params.Encode())
258+
var resp *http.Response
259+
var err error
260+
dataBody := strings.NewReader(params.Encode())
257261
log.Infof("Running the API %s", apiURL)
258262
start := time.Now()
259-
resp, err := http.Post(
260-
apiURL,
261-
"application/x-www-form-urlencoded",
262-
data,
263-
)
263+
if post {
264+
resp, err = http.Post(
265+
apiURL,
266+
"application/x-www-form-urlencoded",
267+
dataBody,
268+
)
269+
} else {
270+
apiURL = fmt.Sprintf("%s?%s", apiURL, params.Encode())
271+
resp, err = http.Get(apiURL)
272+
}
273+
264274
APIscount++
265275
if err != nil {
266276
log.Infof("Error sending API request: %s with error %s\n", apiURL, err)

0 commit comments

Comments
 (0)