Skip to content

Commit

Permalink
[fix] return err instead of Exit while requesting
Browse files Browse the repository at this point in the history
  • Loading branch information
showwin committed Jan 31, 2021
1 parent f6fbedf commit 2ab0679
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
77 changes: 48 additions & 29 deletions speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 0 additions & 13 deletions speedtest/util.go

This file was deleted.

0 comments on commit 2ab0679

Please sign in to comment.