Created
January 1, 2021 09:54
-
-
Save santoshphegde/966d18c9ac4f7e13aa1b4fd87830a6bb to your computer and use it in GitHub Desktop.
Go routine to load http endpoint with different ua string
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
"strconv" | |
"sync" | |
"sync/atomic" | |
) | |
func startLoadTest(count int, url string) { | |
var wg sync.WaitGroup | |
var finishedCount uint64 | |
for i := 0; i < count; i++ { | |
client := &http.Client{ | |
CheckRedirect: func(req *http.Request, via []*http.Request) error { | |
return http.ErrUseLastResponse | |
}, | |
} | |
wg.Add(1) | |
go func() { | |
req, _ := http.NewRequest("GET", url, nil) | |
req.Header.Set("User-Agent", "ua_" + strconv.FormatUint(finishedCount, 10)) | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println(fmt.Sprintf("Got error: %v, status: %v", err, resp)) | |
return | |
} | |
defer func() { | |
if resp != nil && resp.Body != nil { | |
resp.Body.Close() | |
} | |
wg.Done() | |
}() | |
atomic.AddUint64(&finishedCount, 1) | |
fmt.Println(fmt.Sprintf("Finished GET request #%v, status: %d", finishedCount, resp.StatusCode)) | |
}() | |
} | |
wg.Wait() | |
fmt.Println("Done!") | |
} | |
func main() { | |
count, err := strconv.Atoi(os.Args[1]) | |
if err != nil { | |
panic("error in cmd line arg: 1, should be int") | |
} | |
url := os.Args[2] | |
startLoadTest(count, url) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment