Skip to content

Commit dc6eb0f

Browse files
committed
Configurable download chunks, upload size and test duration
1 parent eb7f5cb commit dc6eb0f

File tree

6 files changed

+41
-23
lines changed

6 files changed

+41
-23
lines changed

defs/bytes_counter.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212

1313
// BytesCounter implements io.Reader and io.Writer interface, for counting bytes being read/written in HTTP requests
1414
type BytesCounter struct {
15-
start time.Time
16-
pos int
17-
total int
18-
payload []byte
19-
reader io.ReadSeeker
20-
mebi bool
15+
start time.Time
16+
pos int
17+
total int
18+
payload []byte
19+
reader io.ReadSeeker
20+
mebi bool
21+
uploadSize int
2122

2223
lock *sync.Mutex
2324
}
@@ -44,7 +45,7 @@ func (c *BytesCounter) Read(p []byte) (int, error) {
4445
c.lock.Lock()
4546
c.total += n
4647
c.pos += n
47-
if c.pos == uploadSize {
48+
if c.pos == c.uploadSize {
4849
c.resetReader()
4950
}
5051
c.lock.Unlock()
@@ -57,6 +58,11 @@ func (c *BytesCounter) SetMebi(mebi bool) {
5758
c.mebi = mebi
5859
}
5960

61+
// SetUploadSize sets the size of payload being uploaded
62+
func (c *BytesCounter) SetUploadSize(uploadSize int) {
63+
c.uploadSize = uploadSize * 1024
64+
}
65+
6066
// AvgBytes returns the average bytes/second
6167
func (c *BytesCounter) AvgBytes() float64 {
6268
return float64(c.total) / time.Now().Sub(c.start).Seconds()
@@ -94,7 +100,7 @@ func (c *BytesCounter) AvgHumanize() string {
94100
// GenerateBlob generates a random byte array of `uploadSize` in the `payload` field, and sets the `reader` field to
95101
// read from it
96102
func (c *BytesCounter) GenerateBlob() {
97-
c.payload = getRandomData(uploadSize)
103+
c.payload = getRandomData(c.uploadSize)
98104
c.reader = bytes.NewReader(c.payload)
99105
}
100106

defs/defs.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package defs
22

3-
const (
4-
// chunks to download in download test
5-
downloadChunks = 100
6-
// payload size per upload request
7-
uploadSize = 1024 * 1024
8-
)
9-
103
var (
114
// values to be filled in by build script
125
BuildDate string

defs/options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const (
2424
OptionServerJSON = "server-json"
2525
OptionSource = "source"
2626
OptionTimeout = "timeout"
27+
OptionChunks = "chunks"
28+
OptionUploadSize = "upload-size"
29+
OptionDuration = "duration"
2730
OptionSecure = "secure"
2831
OptionNoPreAllocate = "no-pre-allocate"
2932
OptionVersion = "version"

defs/server.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (s *Server) PingAndJitter(count int) (float64, float64, error) {
185185
}
186186

187187
// Download performs the actual download test
188-
func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int) (float64, int, error) {
188+
func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chunks int, duration time.Duration) (float64, int, error) {
189189
t := time.Now()
190190
defer func() {
191191
s.TLog.Logf("Download took %s", time.Now().Sub(t).String())
@@ -210,7 +210,7 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int) (fl
210210
return 0, 0, err
211211
}
212212
q := req.URL.Query()
213-
q.Set("ckSize", strconv.Itoa(downloadChunks))
213+
q.Set("ckSize", strconv.Itoa(chunks))
214214
req.URL.RawQuery = q.Encode()
215215
req.Header.Set("User-Agent", UserAgent)
216216
req.Header.Set("Accept-Encoding", "identity")
@@ -261,7 +261,7 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int) (fl
261261
go doDownload()
262262
time.Sleep(200 * time.Millisecond)
263263
}
264-
timeout := time.After(15 * time.Second)
264+
timeout := time.After(duration)
265265
Loop:
266266
for {
267267
select {
@@ -277,14 +277,15 @@ Loop:
277277
}
278278

279279
// Upload performs the actual upload test
280-
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int) (float64, int, error) {
280+
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int, uploadSize int, duration time.Duration) (float64, int, error) {
281281
t := time.Now()
282282
defer func() {
283283
s.TLog.Logf("Upload took %s", time.Now().Sub(t).String())
284284
}()
285285

286286
counter := NewCounter()
287287
counter.SetMebi(useMebi)
288+
counter.SetUploadSize(uploadSize)
288289

289290
if noPrealloc {
290291
log.Info("Pre-allocation is disabled, performance might be lower!")
@@ -353,7 +354,7 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
353354
go doUpload()
354355
time.Sleep(200 * time.Millisecond)
355356
}
356-
timeout := time.After(15 * time.Second)
357+
timeout := time.After(duration)
357358
Loop:
358359
for {
359360
select {

main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,24 @@ func main() {
135135
},
136136
&cli.IntFlag{
137137
Name: defs.OptionTimeout,
138-
Usage: "HTTP `TIMEOUT` in seconds.",
138+
Usage: "HTTP `TIMEOUT` in seconds",
139139
Value: 15,
140140
},
141+
&cli.IntFlag{
142+
Name: defs.OptionDuration,
143+
Usage: "Upload and download test duration in seconds",
144+
Value: 15,
145+
},
146+
&cli.IntFlag{
147+
Name: defs.OptionChunks,
148+
Usage: "Chunks to download from server, chunk size depends on server configuration",
149+
Value: 100,
150+
},
151+
&cli.IntFlag{
152+
Name: defs.OptionUploadSize,
153+
Usage: "Size of payload being uploaded in KiB",
154+
Value: 1024,
155+
},
141156
&cli.BoolFlag{
142157
Name: defs.OptionSecure,
143158
Usage: "Use HTTPS instead of HTTP when communicating with\n" +

speedtest/helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
8282
if c.Bool(defs.OptionNoDownload) {
8383
log.Info("Download test is disabled")
8484
} else {
85-
download, br, err := currentServer.Download(silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes), c.Int(defs.OptionConcurrent))
85+
download, br, err := currentServer.Download(silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes), c.Int(defs.OptionConcurrent), c.Int(defs.OptionChunks), time.Duration(c.Int(defs.OptionDuration))*time.Second)
8686
if err != nil {
8787
log.Errorf("Failed to get download speed: %s", err)
8888
return err
@@ -97,7 +97,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
9797
if c.Bool(defs.OptionNoUpload) {
9898
log.Info("Upload test is disabled")
9999
} else {
100-
upload, bw, err := currentServer.Upload(c.Bool(defs.OptionNoPreAllocate), silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes), c.Int(defs.OptionConcurrent))
100+
upload, bw, err := currentServer.Upload(c.Bool(defs.OptionNoPreAllocate), silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes), c.Int(defs.OptionConcurrent), c.Int(defs.OptionUploadSize), time.Duration(c.Int(defs.OptionDuration))*time.Second)
101101
if err != nil {
102102
log.Errorf("Failed to get upload speed: %s", err)
103103
return err

0 commit comments

Comments
 (0)