Skip to content

Commit d5d4fb3

Browse files
committed
Add option --mebibytes
Fixes #1
1 parent 9df434e commit d5d4fb3

File tree

5 files changed

+52
-24
lines changed

5 files changed

+52
-24
lines changed

defs/bytes_counter.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type BytesCounter struct {
1515
total int
1616
payload []byte
1717
reader io.ReadSeeker
18+
mebi bool
1819
}
1920

2021
// Write implements io.Writer
@@ -33,26 +34,40 @@ func (c *BytesCounter) Read(p []byte) (int, error) {
3334
return n, err
3435
}
3536

37+
// SetBase sets the base for dividing bytes into megabyte or mebibyte
38+
func (c *BytesCounter) SetMebi(mebi bool) {
39+
c.mebi = mebi
40+
}
41+
3642
// Average returns the average bytes/second
3743
func (c *BytesCounter) Average() float64 {
3844
return float64(c.total) / time.Now().Sub(c.start).Seconds()
3945
}
4046

41-
func (c *BytesCounter) AvgMbits() string {
42-
return fmt.Sprintf("%.02f Mbps", c.Average()/131072)
47+
func (c *BytesCounter) AvgMbps() float64 {
48+
var base float64 = 100000
49+
if c.mebi {
50+
base = 131072
51+
}
52+
return c.Average() / base
4353
}
4454

4555
func (c *BytesCounter) AvgHumanize() string {
4656
val := c.Average()
4757

48-
if val < 1024 {
58+
var base float64 = 1000
59+
if c.mebi {
60+
base = 1024
61+
}
62+
63+
if val < base {
4964
return fmt.Sprintf("%.2f bytes/s", val)
50-
} else if val/1024 < 1024 {
51-
return fmt.Sprintf("%.2f KB/s", val/1024)
52-
} else if val/1024/1024 < 1024 {
53-
return fmt.Sprintf("%.2f MB/s", val/1024/1024)
65+
} else if val/base < base {
66+
return fmt.Sprintf("%.2f KB/s", val/base)
67+
} else if val/base/base < base {
68+
return fmt.Sprintf("%.2f MB/s", val/base/base)
5469
} else {
55-
return fmt.Sprintf("%.2f GB/s", val/1024/1024/1024)
70+
return fmt.Sprintf("%.2f GB/s", val/base/base/base)
5671
}
5772
}
5873

defs/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
OptionNoDownload = "no-download"
1010
OptionNoUpload = "no-upload"
1111
OptionBytes = "bytes"
12+
OptionMebiBytes = "mebibytes"
1213
OptionDistance = "distance"
1314
OptionShare = "share"
1415
OptionSimple = "simple"

defs/server.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,14 @@ func (s *Server) PingAndJitter(count int) (float64, float64, error) {
177177
}
178178

179179
// Download performs the actual download test
180-
func (s *Server) Download(silent bool, useBytes bool) (float64, int, error) {
180+
func (s *Server) Download(silent bool, useBytes, useMebi bool) (float64, int, error) {
181181
t := time.Now()
182182
defer func() {
183183
s.TLog.Logf("Download took %s", time.Now().Sub(t).String())
184184
}()
185185

186186
counter := &BytesCounter{}
187+
counter.SetMebi(useMebi)
187188

188189
ctx, cancel := context.WithCancel(context.Background())
189190
defer cancel()
@@ -233,7 +234,7 @@ func (s *Server) Download(silent bool, useBytes bool) (float64, int, error) {
233234
if useBytes {
234235
s.Suffix = fmt.Sprintf(" %s", counter.AvgHumanize())
235236
} else {
236-
s.Suffix = fmt.Sprintf(" %s", counter.AvgMbits())
237+
s.Suffix = fmt.Sprintf(" %.2f Mbps", counter.AvgMbps())
237238
}
238239
}
239240

@@ -242,7 +243,7 @@ func (s *Server) Download(silent bool, useBytes bool) (float64, int, error) {
242243
if useBytes {
243244
pb.FinalMSG = fmt.Sprintf("Download rate:\t%s\n", counter.AvgHumanize())
244245
} else {
245-
pb.FinalMSG = fmt.Sprintf("Download rate:\t%s\n", counter.AvgMbits())
246+
pb.FinalMSG = fmt.Sprintf("Download rate:\t%.2f Mbps\n", counter.AvgMbps())
246247
}
247248
pb.Stop()
248249
}()
@@ -261,17 +262,18 @@ Loop:
261262
}
262263
}
263264

264-
return counter.Average() / 131072, counter.Total(), nil
265+
return counter.AvgMbps(), counter.Total(), nil
265266
}
266267

267268
// Upload performs the actual upload test
268-
func (s *Server) Upload(noPrealloc, silent, useBytes bool) (float64, int, error) {
269+
func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool) (float64, int, error) {
269270
t := time.Now()
270271
defer func() {
271272
s.TLog.Logf("Upload took %s", time.Now().Sub(t).String())
272273
}()
273274

274275
counter := &BytesCounter{}
276+
counter.SetMebi(useMebi)
275277

276278
if noPrealloc {
277279
log.Info("Pre-allocation is disabled, performance might be lower!")
@@ -322,7 +324,7 @@ func (s *Server) Upload(noPrealloc, silent, useBytes bool) (float64, int, error)
322324
if useBytes {
323325
s.Suffix = fmt.Sprintf(" %s", counter.AvgHumanize())
324326
} else {
325-
s.Suffix = fmt.Sprintf(" %s", counter.AvgMbits())
327+
s.Suffix = fmt.Sprintf(" %.2f Mbps", counter.AvgMbps())
326328
}
327329
}
328330

@@ -331,7 +333,7 @@ func (s *Server) Upload(noPrealloc, silent, useBytes bool) (float64, int, error)
331333
if useBytes {
332334
pb.FinalMSG = fmt.Sprintf("Upload rate:\t%s\n", counter.AvgHumanize())
333335
} else {
334-
pb.FinalMSG = fmt.Sprintf("Upload rate:\t%s\n", counter.AvgMbits())
336+
pb.FinalMSG = fmt.Sprintf("Upload rate:\t%.2f Mbps\n", counter.AvgMbps())
335337
}
336338
pb.Stop()
337339
}()
@@ -350,7 +352,7 @@ Loop:
350352
}
351353
}
352354

353-
return counter.Average() / 131072, counter.Total(), nil
355+
return counter.AvgMbps(), counter.Total(), nil
354356
}
355357

356358
// GetIPInfo accesses the backend's getIP.php endpoint and get current client's IP information

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func main() {
6161
"\tthe image generated by --share, nor output from\n" +
6262
"\t--json or --csv",
6363
},
64+
&cli.BoolFlag{
65+
Name: defs.OptionMebiBytes,
66+
Usage: "Use 1024 bytes as 1 kilobyte instead of 1000",
67+
},
6468
&cli.StringFlag{
6569
Name: defs.OptionDistance,
6670
Usage: "Change distance unit shown in ISP info, use 'mi' for miles,\n" +

speedtest/helper.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
7878
if c.Bool(defs.OptionNoDownload) {
7979
log.Info("Download test is disabled")
8080
} else {
81-
download, br, err := currentServer.Download(silent, c.Bool(defs.OptionBytes))
81+
download, br, err := currentServer.Download(silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes))
8282
if err != nil {
8383
log.Errorf("Failed to get download speed: %s", err)
8484
return err
@@ -93,7 +93,7 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
9393
if c.Bool(defs.OptionNoUpload) {
9494
log.Info("Upload test is disabled")
9595
} else {
96-
upload, bw, err := currentServer.Upload(c.Bool(defs.OptionNoPreAllocate), silent, c.Bool(defs.OptionBytes))
96+
upload, bw, err := currentServer.Upload(c.Bool(defs.OptionNoPreAllocate), silent, c.Bool(defs.OptionBytes), c.Bool(defs.OptionMebiBytes))
9797
if err != nil {
9898
log.Errorf("Failed to get upload speed: %s", err)
9999
return err
@@ -105,7 +105,8 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
105105
// print result if --simple is given
106106
if c.Bool(defs.OptionSimple) {
107107
if c.Bool(defs.OptionBytes) {
108-
log.Warnf("Ping:\t%.0f ms\tJitter:\t%.0f ms\nDownload rate:\t%s\nUpload rate:\t%s", p, jitter, humanizeMbps(downloadValue), humanizeMbps(uploadValue))
108+
useMebi := c.Bool(defs.OptionMebiBytes)
109+
log.Warnf("Ping:\t%.0f ms\tJitter:\t%.0f ms\nDownload rate:\t%s\nUpload rate:\t%s", p, jitter, humanizeMbps(downloadValue, useMebi), humanizeMbps(uploadValue, useMebi))
109110
} else {
110111
log.Warnf("Ping:\t%.0f ms\tJitter:\t%.0f ms\nDownload rate:\t%.2f Mbps\nUpload rate:\t%.2f Mbps", p, jitter, downloadValue, uploadValue)
111112
}
@@ -299,16 +300,21 @@ func sendTelemetry(telemetryServer defs.TelemetryServer, ispInfo *defs.GetIPResu
299300
}
300301
}
301302

302-
func humanizeMbps(mbps float64) string {
303+
func humanizeMbps(mbps float64, useMebi bool) string {
303304
val := mbps / 8
305+
var base float64 = 1000
306+
if useMebi {
307+
base = 1024
308+
}
309+
304310
if val < 1 {
305-
if kb := val * 1024; kb < 1 {
306-
return fmt.Sprintf("%.2f bytes/s", kb*1024)
311+
if kb := val * base; kb < 1 {
312+
return fmt.Sprintf("%.2f bytes/s", kb*base)
307313
} else {
308314
return fmt.Sprintf("%.2f KB/s", kb)
309315
}
310-
} else if val > 1024 {
311-
return fmt.Sprintf("%.2f GB/s", val/1024)
316+
} else if val > base {
317+
return fmt.Sprintf("%.2f GB/s", val/base)
312318
} else {
313319
return fmt.Sprintf("%.2f MB/s", val)
314320
}

0 commit comments

Comments
 (0)