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

feat: add possibility to specify DL and UL chunk size #174

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Flags:
--no-upload Disable upload test.
--ping-mode Select a method for Ping. (support icmp/tcp/http)
-d --debug Enable debug mode.
--dl-size=1000 Set download chunk size.
--ul-size=800 Set upload chunk size.
--version Show application version.
```

Expand Down
4 changes: 4 additions & 0 deletions speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ var (
noUpload = kingpin.Flag("no-upload", "Disable upload test.").Bool()
pingMode = kingpin.Flag("ping-mode", "Select a method for Ping. (support icmp/tcp/http)").Default("http").String()
debug = kingpin.Flag("debug", "Enable debug mode.").Short('d').Bool()
dlSize = kingpin.Flag("dl-size", "Set download chunk size.").Default("1000").Int()
ulSize = kingpin.Flag("ul-size", "Set upload chunk size.").Default("800").Int()
)

func main() {
Expand All @@ -54,6 +56,8 @@ func main() {
Keyword: *search,
NoDownload: *noDownload,
NoUpload: *noUpload,
DlSize: *dlSize,
UlSize: *ulSize,
}))
speedtestClient.SetNThread(*thread)

Expand Down
13 changes: 7 additions & 6 deletions speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Server) MultiDownloadTestContext(ctx context.Context, servers Servers)
sp := server
dbg.Printf("Register Download Handler: %s\n", sp.URL)
fp = server.Context.RegisterDownloadHandler(func() {
_ = downloadRequest(_context, sp, 3)
_ = downloadRequest(_context, sp, s.Context.config.DlSize)
})
}
fp.Start(cancel, mainIDIndex) // block here
Expand All @@ -75,7 +75,7 @@ func (s *Server) MultiUploadTestContext(ctx context.Context, servers Servers) er
sp := server
dbg.Printf("Register Upload Handler: %s\n", sp.URL)
fp = server.Context.RegisterUploadHandler(func() {
_ = uploadRequest(_context, sp, 3)
_ = uploadRequest(_context, sp, s.Context.config.UlSize)
})
}
fp.Start(cancel, mainIDIndex) // block here
Expand All @@ -98,10 +98,11 @@ func (s *Server) downloadTestContext(ctx context.Context, downloadRequest downlo
dbg.Println("Download test disabled")
return nil
}

start := time.Now()
_context, cancel := context.WithCancel(ctx)
s.Context.RegisterDownloadHandler(func() {
_ = downloadRequest(_context, s, 3)
_ = downloadRequest(_context, s, s.Context.config.DlSize)
}).Start(cancel, 0)
duration := time.Since(start)
s.DLSpeed = s.Context.GetAvgDownloadRate()
Expand All @@ -125,10 +126,11 @@ func (s *Server) uploadTestContext(ctx context.Context, uploadRequest uploadFunc
dbg.Println("Upload test disabled")
return nil
}

start := time.Now()
_context, cancel := context.WithCancel(ctx)
s.Context.RegisterUploadHandler(func() {
_ = uploadRequest(_context, s, 4)
_ = uploadRequest(_context, s, s.Context.config.UlSize)
}).Start(cancel, 0)
duration := time.Since(start)
s.ULSpeed = s.Context.GetAvgUploadRate()
Expand Down Expand Up @@ -159,8 +161,7 @@ func downloadRequest(ctx context.Context, s *Server, w int) error {
return s.Context.NewChunk().DownloadHandler(resp.Body)
}

func uploadRequest(ctx context.Context, s *Server, w int) error {
size := ulSizes[w]
func uploadRequest(ctx context.Context, s *Server, size int) error {
dc := s.Context.NewChunk().UploadHandler(int64(size*100-51) * 10)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.URL, dc)
req.ContentLength = dc.(*DataChunk).ContentLength
Expand Down
16 changes: 16 additions & 0 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"runtime"
"slices"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -56,6 +57,9 @@ type UserConfig struct {

NoDownload bool
NoUpload bool

DlSize int
UlSize int
}

func parseAddr(addr string) (string, string) {
Expand All @@ -71,6 +75,18 @@ func (s *Speedtest) NewUserConfig(uc *UserConfig) {
dbg.Enable()
}

if !(uc.DlSize > 0 && slices.Contains(dlSizes[:], uc.DlSize)) {
dbg.Printf("Warning: invalid/unset download size: %d.\n", uc.DlSize)
uc.DlSize = dlSizes[4]
dbg.Printf("Using default size: %d.\n", uc.DlSize)
}

if !(uc.UlSize > 0 && slices.Contains(ulSizes[:], uc.UlSize)) {
dbg.Printf("Warning: invalid/unset upload size: %d.\n", uc.UlSize)
uc.UlSize = ulSizes[4]
dbg.Printf("Using default size: %d.\n", uc.UlSize)
}

if uc.SavingMode {
s.SetNThread(1) // Set the number of concurrent connections to 1
}
Expand Down