Skip to content

Commit

Permalink
Merge pull request #90 from r3inbowari/change/upreader
Browse files Browse the repository at this point in the history
change: use a custom reader to allocate less memory when uploading
  • Loading branch information
showwin authored Dec 13, 2022
2 parents b1ddc48 + 8e6841d commit 7cab7d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
9 changes: 4 additions & 5 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,15 +216,15 @@ 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 - 51) * 10)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ulURL, reader)
req.ContentLength = reader.ContentLength
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Type", "application/octet-stream")
resp, err := doer.Do(req)
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 < readChunkSize {
if r.n <= 0 {
return n, io.EOF
}
n = copy(b, r.rs[:r.n])
} else {
n = copy(b, r.rs)
}
r.n -= n
return
}

0 comments on commit 7cab7d6

Please sign in to comment.