Skip to content

Commit

Permalink
change: use a custom reader to allocate less memory when uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
r3inbowari committed Dec 9, 2022
1 parent 9019865 commit cd3438d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
7 changes: 3 additions & 4 deletions speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -217,10 +216,10 @@ func downloadRequest(ctx context.Context, doer *http.Client, dlURL string, w int

func uploadRequest(ctx context.Context, doer *http.Client, ulURL string, w int) error {
size := ulSizes[w]
v := url.Values{}
v.Add("content", strings.Repeat("0123456789", size*100-51))

req, err := http.NewRequestWithContext(ctx, http.MethodPost, ulURL, strings.NewReader(v.Encode()))
reader := NewRepeatReader(size*100*10 - 51)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ulURL, reader)
req.ContentLength = reader.ContentLength
if err != nil {
return err
}
Expand Down
35 changes: 35 additions & 0 deletions speedtest/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package speedtest

import (
"bytes"
"io"
)

const readChunkSize = 1024 * 32 // 32 KBytes

type RepeatReader struct {
ContentLength int64
rs []byte
n int
}

func NewRepeatReader(size int) *RepeatReader {
if size <= 0 {
panic("the size of repeated bytes should be > 0")
}
seqChunk := bytes.Repeat([]byte{0xAA}, readChunkSize) // uniformly distributed sequence of bits
return &RepeatReader{rs: seqChunk, ContentLength: int64(size), n: size}
}

func (r *RepeatReader) Read(b []byte) (n int, err error) {
if r.n <= 0 {
return 0, io.EOF
}
if r.n < readChunkSize {
n = copy(b, r.rs[:r.n])
} else {
n = copy(b, r.rs)
}
r.n -= n
return
}

0 comments on commit cd3438d

Please sign in to comment.