From 2ab0679f413220294a55ad9edd46125f682b6b8b Mon Sep 17 00:00:00 2001 From: showwin Date: Sun, 31 Jan 2021 10:03:22 +0900 Subject: [PATCH] [fix] return err instead of Exit while requesting --- go.mod | 1 + go.sum | 2 ++ speedtest/request.go | 77 +++++++++++++++++++++++++++----------------- speedtest/util.go | 13 -------- 4 files changed, 51 insertions(+), 42 deletions(-) delete mode 100644 speedtest/util.go diff --git a/go.mod b/go.mod index e8e8695..5a02f45 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.12 require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4 // indirect + golang.org/x/sync v0.0.0-20201207232520-09787c993a3a gopkg.in/alecthomas/kingpin.v2 v2.2.6 ) diff --git a/go.sum b/go.sum index 02996e6..bd96f55 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/speedtest/request.go b/speedtest/request.go index c19050f..d25f3e5 100644 --- a/speedtest/request.go +++ b/speedtest/request.go @@ -6,8 +6,9 @@ import ( "net/url" "strconv" "strings" - "sync" "time" + + "golang.org/x/sync/errgroup" ) var dlSizes = [...]int{350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000} @@ -17,15 +18,18 @@ var client = http.Client{} // DownloadTest executes the test to measure download speed func (s *Server) DownloadTest(savingMode bool) error { dlURL := strings.Split(s.URL, "/upload")[0] - wg := new(sync.WaitGroup) + eg := errgroup.Group{} // Warming up sTime := time.Now() for i := 0; i < 2; i++ { - wg.Add(1) - go dlWarmUp(wg, dlURL) + eg.Go(func() error { + return dlWarmUp(dlURL) + }) + } + if err := eg.Wait(); err != nil { + return err } - wg.Wait() fTime := time.Now() // 1.125MB for each request (750 * 750 * 2) wuSpeed := 1.125 * 8 * 2 / fTime.Sub(sTime.Add(s.Latency)).Seconds() @@ -55,10 +59,13 @@ func (s *Server) DownloadTest(savingMode bool) error { if skip == false { sTime = time.Now() for i := 0; i < workload; i++ { - wg.Add(1) - go downloadRequest(wg, dlURL, weight) + eg.Go(func() error { + return downloadRequest(dlURL, weight) + }) + } + if err := eg.Wait(); err != nil { + return err } - wg.Wait() fTime = time.Now() reqMB := dlSizes[weight] * dlSizes[weight] * 2 / 1000 / 1000 @@ -71,16 +78,17 @@ func (s *Server) DownloadTest(savingMode bool) error { // UploadTest executes the test to measure upload speed func (s *Server) UploadTest(savingMode bool) error { - wg := new(sync.WaitGroup) - // Warm up sTime := time.Now() - wg = new(sync.WaitGroup) + eg := errgroup.Group{} for i := 0; i < 2; i++ { - wg.Add(1) - go ulWarmUp(wg, s.URL) + eg.Go(func() error { + return ulWarmUp(s.URL) + }) + } + if err := eg.Wait(); err != nil { + return err } - wg.Wait() fTime := time.Now() // 1.0 MB for each request wuSpeed := 1.0 * 8 * 2 / fTime.Sub(sTime.Add(s.Latency)).Seconds() @@ -110,10 +118,13 @@ func (s *Server) UploadTest(savingMode bool) error { if skip == false { sTime = time.Now() for i := 0; i < workload; i++ { - wg.Add(1) - go uploadRequest(wg, s.URL) + eg.Go(func() error { + return uploadRequest(s.URL) + }) + } + if err := eg.Wait(); err != nil { + return err } - wg.Wait() fTime = time.Now() reqMB := float64(ulSizes[weight]) / 1000 @@ -125,54 +136,62 @@ func (s *Server) UploadTest(savingMode bool) error { return nil } -func dlWarmUp(wg *sync.WaitGroup, dlURL string) { +func dlWarmUp(dlURL string) error { size := dlSizes[2] xdlURL := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg" resp, err := client.Get(xdlURL) - checkError(err) + if err != nil { + return err + } defer resp.Body.Close() ioutil.ReadAll(resp.Body) - wg.Done() + return nil } -func ulWarmUp(wg *sync.WaitGroup, ulURL string) { +func ulWarmUp(ulURL string) error { size := ulSizes[4] v := url.Values{} v.Add("content", strings.Repeat("0123456789", size*100-51)) resp, err := client.PostForm(ulURL, v) - checkError(err) + if err != nil { + return err + } defer resp.Body.Close() ioutil.ReadAll(resp.Body) - wg.Done() + return nil } -func downloadRequest(wg *sync.WaitGroup, dlURL string, w int) { +func downloadRequest(dlURL string, w int) error { size := dlSizes[w] xdlURL := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg" resp, err := client.Get(xdlURL) - checkError(err) + if err != nil { + return err + } defer resp.Body.Close() ioutil.ReadAll(resp.Body) - wg.Done() + return nil } -func uploadRequest(wg *sync.WaitGroup, ulURL string) { +func uploadRequest(ulURL string) error { size := ulSizes[9] v := url.Values{} v.Add("content", strings.Repeat("0123456789", size*100-51)) resp, err := client.PostForm(ulURL, v) - checkError(err) + if err != nil { + return err + } defer resp.Body.Close() ioutil.ReadAll(resp.Body) - wg.Done() + return nil } // PingTest executes test to measure latency diff --git a/speedtest/util.go b/speedtest/util.go deleted file mode 100644 index 0961039..0000000 --- a/speedtest/util.go +++ /dev/null @@ -1,13 +0,0 @@ -package speedtest - -import ( - "log" - "os" -) - -func checkError(err error) { - if err != nil { - log.Fatal(err) - os.Exit(1) - } -}