tcnksm / go-httpstat

Tracing golang HTTP request latency
MIT License
423 stars 72 forks source link

Goroutine support? #17

Open ximply opened 6 years ago

ximply commented 6 years ago

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()

}`