tcnksm/go-httpstat

Goroutine support?

ximply opened this issue · 0 comments

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

}`