Created
September 16, 2021 09:18
-
-
Save ls0f/1c196e26d5cc871ede4bac1e773f520e to your computer and use it in GitHub Desktop.
http_body_test.go
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 ( | |
"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