Skip to content

Commit 23d7bad

Browse files
Add files via upload
1 parent 161a45a commit 23d7bad

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

core_engine/main.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ type UploadTestResult struct {
5959
UploadMbps float64 `json:"upload_mbps"`
6060
BytesUploaded int64 `json:"bytes_uploaded"`
6161
}
62+
63+
type zeroReader struct{}
64+
65+
func (z zeroReader) Read(p []byte) (n int, err error) {
66+
for i := range p {
67+
p[i] = 0
68+
}
69+
return len(p), nil
70+
}
71+
72+
73+
6274
func main() {
6375
inputData, err := io.ReadAll(os.Stdin)
6476
if err != nil { os.Exit(1) }
@@ -122,54 +134,44 @@ func main() {
122134
fmt.Println(string(outputData))
123135
}
124136

125-
126137
func runUploadTest(j UploadTestJob, results chan<- UploadTestResult) {
127-
payloadReader := io.LimitReader(io.Zero, int64(j.UploadBytes))
138+
payloadReader := io.LimitReader(zeroReader{}, int64(j.UploadBytes))
128139

129140
dialer, err := proxy.SOCKS5("tcp", fmt.Sprintf("%s:%d", j.ListenIP, j.TestPort), nil, proxy.Direct)
130141
if err != nil {
131142
results <- UploadTestResult{Tag: j.Tag, Status: "error: dialer creation failed"}
132143
return
133144
}
134-
135145
transport := &http.Transport{
136146
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
137147
return dialer.Dial(network, addr)
138148
},
139149
}
140150
httpClient := &http.Client{Transport: transport, Timeout: 45 * time.Second}
141-
142151
req, err := http.NewRequest("POST", j.UploadURL, payloadReader)
143152
if err != nil {
144153
results <- UploadTestResult{Tag: j.Tag, Status: fmt.Sprintf("error: request creation failed: %v", err)}
145154
return
146155
}
147156
req.Header.Set("Content-Type", "application/octet-stream")
148157
req.ContentLength = int64(j.UploadBytes)
149-
150158
start := time.Now()
151159
resp, err := httpClient.Do(req)
152160
if err != nil {
153161
results <- UploadTestResult{Tag: j.Tag, Status: fmt.Sprintf("error: http post failed: %v", err)}
154162
return
155163
}
156164
defer resp.Body.Close()
157-
158165
duration := time.Since(start).Seconds()
159-
160166
if resp.StatusCode != http.StatusOK {
161167
results <- UploadTestResult{Tag: j.Tag, Status: fmt.Sprintf("error: bad status %d", resp.StatusCode)}
162168
return
163169
}
164-
165170
if duration == 0 {
166171
results <- UploadTestResult{Tag: j.Tag, Status: "error: division by zero"}
167172
return
168173
}
169-
170-
// Speed in Megabits per second
171174
uploadMbps := (float64(j.UploadBytes) * 8) / (duration * 1024 * 1024)
172-
173175
results <- UploadTestResult{
174176
Tag: j.Tag,
175177
Status: "success",

0 commit comments

Comments
 (0)