-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
140 lines (118 loc) · 3.12 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"crypto/rand"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"io"
mrand "math/rand"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
func startUploadTest(targets []Target) float64 {
ctx, cancel := context.WithTimeout(context.Background(), uploadDuration)
defer cancel()
resultChan := make(chan *TestResult, parallelConnections)
var wg sync.WaitGroup
wg.Add(parallelConnections)
for i := 0; i < parallelConnections; i++ {
go func(ctx context.Context, targets []Target) {
defer wg.Done()
result, err := startUploadConnection(ctx, targets)
if err != nil {
log.Error().Err(err).Msg("Error")
return
}
resultChan <- result
}(ctx, targets)
}
var totalSize uint64 = 0
var totalTime time.Duration
wg.Wait()
close(resultChan)
for result := range resultChan {
totalSize += result.TotalSize
totalTime += result.TotalTime
}
speed := (float64(totalSize * uint64(time.Second))) / float64(totalTime) * float64(parallelConnections)
log.Debug().Dur("totalTime", totalTime).Uint64("totalSize", totalSize).Float64("speed", speed/1e3/1e3*8).Msg("UploadSpeed")
return speed
}
func startUploadConnection(ctx context.Context, targets []Target) (*TestResult, error) {
log.Debug().Msg("startUploadConnection")
// random target
testResult := &TestResult{
TotalSize: 0,
TotalTime: 0,
Target: targets[mrand.Intn(len(targets))],
}
for {
select {
case <-ctx.Done():
return testResult, nil
default:
url := strings.Replace(testResult.Target.URL, "/speedtest", "/speedtest/range/0-"+strconv.Itoa(uploadBufferSize), 1)
log.Debug().Str("url", url).Msg("Upload")
result, err := uploadTest(ctx, url)
if err != nil {
return nil, err
}
testResult.TotalSize += result.TotalSize
testResult.TotalTime += result.TotalTime
log.Debug().Str("url", url).Dur("spendtime", result.TotalTime).Uint64("uploadedSize", result.TotalSize).Msg("Uploaded")
}
}
}
func uploadTest(ctx context.Context, url string) (*TestResult, error) {
written := uint64(0)
reader, writer := io.Pipe()
go func() {
defer writer.Close()
size := 1024
buf := make([]byte, size)
rand.Read(buf)
for {
//log.Debug().Msg("Reading")
select {
case <-ctx.Done():
return
default:
wn, err := writer.Write(buf)
if err != nil {
panic(err)
}
written += uint64(wn)
if wn != size {
panic("short write")
}
if written >= uploadBufferSize {
writer.Close()
return
}
}
}
}()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, io.NopCloser(reader))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/octet-stream")
req.ContentLength = uploadBufferSize
startTime := time.Now()
// skip error because of context timeout
resp, _ := http.DefaultClient.Do(req)
if resp != nil {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Error().Int("statusCode", resp.StatusCode).Msg("Request failed")
return nil, errors.Errorf("Request failed with response code: %d", resp.StatusCode)
}
}
return &TestResult{
TotalSize: written,
TotalTime: time.Since(startTime),
}, nil
}