-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed.go
More file actions
149 lines (125 loc) · 3.56 KB
/
speed.go
File metadata and controls
149 lines (125 loc) · 3.56 KB
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
141
142
143
144
145
146
147
148
149
package main
import (
"crypto/rand"
"encoding/json"
"expvar"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/google/logger"
)
const Megabyte = 1024 * 1024
const Blocksize = 32 * 1024
var connections = expvar.NewInt("connections")
var downloads = expvar.NewInt("downloads")
var uploads = expvar.NewInt("uploads")
var downloadMegs = expvar.NewInt("downloadMegs")
var uploadMegs = expvar.NewInt("uploadMegs")
type Speed struct {
Ip string `json:"ip"`
MBs float64 `json:"megabytespersecond"` // Mega Bytes per second
Mbs float64 `json:"megabitspersecond"` // Mega Bits per second
Duration time.Duration `json:"duration"`
Megabytes float64 `json:"fileseizeinmb"`
}
func download(w http.ResponseWriter, req *http.Request) {
ms := req.FormValue("size")
randID := req.FormValue("randID")
if ms == "" {
ms = "1"
}
m, err := strconv.ParseUint(ms, 10, 0)
if err != nil {
m = 1
}
w.Header().Set("Content-length", strconv.FormatUint(m*Megabyte, 10))
w.Header().Set("Content-Type", "application/octet-stream")
logger.Infof("starting addr=%s megabytes=%d\n", extractIP(req.RemoteAddr), m)
fileGenerator(w, req, m, randID)
}
func upload(w http.ResponseWriter, req *http.Request) {
randID := req.FormValue("randID")
logger.Infof("upload starting addr=%s\n", extractIP(req.RemoteAddr))
start := time.Now()
status := "finished"
written, err := io.Copy(ioutil.Discard, req.Body)
if err != nil {
status = "aborted"
logger.Errorln("Error testing upload speed ", err)
}
duration := time.Since(start)
megabytes := float64(written) / Megabyte
MBs := megabytes / duration.Seconds()
Mbs := MBs * 8
message := fmt.Sprintf("upload %s addr=%s duration=%s megabytes=%.1f speed=%.1fMB/s\n", status, extractIP(req.RemoteAddr), duration, megabytes, MBs)
logger.Infoln(message)
s := &Speed{Ip: extractIP(req.RemoteAddr), Duration: duration, MBs: MBs, Mbs: Mbs, Megabytes: megabytes}
js, _ := json.Marshal(s)
redisClient.Set(randID, js, 0)
w.Write(js)
}
func fileGenerator(w http.ResponseWriter, req *http.Request, m uint64, randID string) {
addConnection()
start := time.Now()
status := "finished"
written, err := io.Copy(w, LimitedRandomGen(m*Megabyte))
if err != nil {
status = "aborted"
}
duration := time.Since(start)
megabytes := float64(written) / Megabyte
mBs := megabytes / duration.Seconds()
mbs := mBs * 8.0
removeConnection(int64(megabytes), 0)
s := &Speed{Ip: extractIP(req.RemoteAddr), Duration: duration, Megabytes: megabytes, MBs: mBs, Mbs: mbs}
sjson, _ := json.Marshal(s)
redisClient.Set(randID+strconv.FormatInt(int64(m), 10), sjson, 0)
logger.Infof("%s addr=%s duration=%s megabytes=%.1f speed=%.1fMB/s\n", status, extractIP(req.RemoteAddr), duration, megabytes, mbs)
}
func addConnection() {
connections.Add(1)
}
func removeConnection(downmegs int64, upmegs int64) {
connections.Add(-1)
if downmegs != 0 {
downloads.Add(1)
downloadMegs.Add(downmegs)
}
if upmegs != 0 {
uploads.Add(1)
uploadMegs.Add(upmegs)
}
}
func extractIP(addrPort string) string {
lastColon := strings.LastIndex(addrPort, ":")
return addrPort[0:lastColon]
}
type FileGen struct {
buf []byte
}
func NewFileGen() *FileGen {
randomBytes := make([]byte, Blocksize)
_, err := rand.Read(randomBytes)
if err != nil {
return nil
}
return &FileGen{
buf: randomBytes,
}
}
func (r *FileGen) Read(p []byte) (n int, err error) {
n = len(p)
toread := n
if n > len(r.buf) {
toread = len(r.buf)
}
copy(p[0:toread], r.buf[0:toread])
return toread, nil
}
func LimitedRandomGen(n uint64) io.Reader {
return io.LimitReader(NewFileGen(), int64(n))
}