Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goroutine support? #17

Open
ximply opened this issue May 3, 2018 · 0 comments
Open

Goroutine support? #17

ximply opened this issue May 3, 2018 · 0 comments

Comments

@ximply
Copy link

ximply commented May 3, 2018

Just like as follows:
`package main

import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/tcnksm/go-httpstat"
"sync"
//"runtime"

)

func worker(host string, wg *sync.WaitGroup) {
req, err := http.NewRequest("GET", host, nil)
if err != nil {
wg.Done()
return
}

var result httpstat.Result
ctx := httpstat.WithHTTPStat(req.Context(), &result)
req = req.WithContext(ctx)

client := http.DefaultClient
client.Timeout = 5 * time.Second
res, err := client.Do(req)
if err != nil {
	wg.Done()
	return
}

if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
	wg.Done()
	return
}
res.Body.Close()
result.End(time.Now())

fmt.Println(result)
wg.Done()

}

func main() {
args := os.Args
if len(args) < 2 {
log.Fatalf("Usage: go run main.go URL")
}

//runtime.GOMAXPROCS(runtime.NumCPU())

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
	wg.Add(1)
	go worker(args[1], &wg)
}
wg.Wait()

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant