Skip to content

Instantly share code, notes, and snippets.

@ls0f
Created September 16, 2021 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ls0f/1c196e26d5cc871ede4bac1e773f520e to your computer and use it in GitHub Desktop.
Save ls0f/1c196e26d5cc871ede4bac1e773f520e to your computer and use it in GitHub Desktop.
http_body_test.go
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
)
func main() {
url := flag.String("url", "", "url")
concurent := flag.Int("c", 1, "concurent")
cnt := flag.Int("n", 1, "count")
body := flag.Bool("body", false, "body")
clo := flag.Bool("close", false, "close")
flag.Parse()
fmt.Printf("url:%v\nread body:%v\nclose body:%v\n", *url, *body, *clo)
wg := sync.WaitGroup{}
taskChan := make(chan struct{}, *concurent)
closeChan := make(chan struct{})
fn := func() {
for {
select {
case <-taskChan:
case <-closeChan:
return
}
res, err := http.Get(*url)
if err != nil {
panic(err)
}
if *body {
io.Copy(ioutil.Discard, res.Body)
}
if *clo {
res.Body.Close()
}
wg.Done()
}
}
wg.Add(*cnt)
for i := 0; i < *concurent; i++ {
go fn()
}
for i := 0; i < *cnt; i++ {
taskChan <- struct{}{}
}
wg.Wait()
close(closeChan)
fmt.Println("Press the Enter Key to terminate")
fmt.Scanln() // wait for Enter Key
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment