Skip to content

Instantly share code, notes, and snippets.

@hrmsk66
Last active September 9, 2019 05:59
Show Gist options
  • Save hrmsk66/95e94756c53e606bf5f4da4ce11af209 to your computer and use it in GitHub Desktop.
Save hrmsk66/95e94756c53e606bf5f4da4ce11af209 to your computer and use it in GitHub Desktop.
Generating concurrent HTTP/HTTPS requests
package main
import (
"flag"
"fmt"
"net/http"
"strconv"
"time"
)
func worker(id int, jobs <-chan string, results chan<- string) {
for url := range jobs {
fmt.Println("worker", id, "requesting to", url)
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
time.Sleep(time.Second)
continue
}
resp.Body.Close()
fmt.Println("worker", id, "got response for", url, "status:", resp.StatusCode)
results <- resp.Status
}
}
func main() {
// The tool needs three parameters.
// Number of workers (Default: 5)
// Number of HTTP requests (Default: 1)
// Target URL (Default: https://repro.global.ssl.fastly.net/"
// For 20 workers to process 10000 requests toward the default target URL
// go run conreq.go -w 20 -r 10000
var requestCnt, workerCnt int
var baseUrl string
m := make(map[string]int)
flag.IntVar(&workerCnt, "w", 5, "Number of workers")
flag.IntVar(&requestCnt, "r", 1, "Number of HTTP requests")
flag.StringVar(&baseUrl, "u", "https://repro.global.ssl.fastly.net/", "Target URL")
flag.Parse()
jobs := make(chan string, requestCnt)
results := make(chan string, requestCnt)
for w := 1; w <= workerCnt; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= requestCnt; j++ {
url := baseUrl + strconv.Itoa(j)
jobs <- url
}
close(jobs)
for a := 1; a <= requestCnt; a++ {
status := <-results
m[status]++
}
fmt.Println("------------------------------")
for status, count := range m {
fmt.Printf("%v / %v\n", status, count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment