-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.25 KB
/
main.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
package main
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"flag"
"io"
"log"
"net/http"
"net/http/httputil"
"sync/atomic"
"time"
)
func main() {
sleep := flag.Duration("sleep", 0, "Time to sleep per request")
work := flag.Int("work", 0, "Artificial work load iteration count")
flag.Parse()
count := uint64(0)
go func(last time.Time) {
ticks := time.Tick(time.Second)
for range ticks {
rate := float64(atomic.SwapUint64(&count, 0)) / time.Since(last).Seconds()
last = time.Now()
log.Printf("Rate: %.3f/s", rate)
}
}(time.Now())
http.ListenAndServe(flag.Arg(0), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer atomic.AddUint64(&count, 1)
time.Sleep(*sleep)
if _, err := hash(*work); err != nil {
log.Printf("Error: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
bs, _ := httputil.DumpRequest(r, true)
w.Write(bs)
}))
}
func hash(n int) (string, error) {
if n == 0 {
return "", nil
}
var buf bytes.Buffer
_, err := io.CopyN(&buf, rand.Reader, 1024*1024) // 1MB
if err != nil {
return "", err
}
data := buf.Bytes()
for i := 0; i < n; i++ {
hash := sha256.Sum256(data)
data = hash[:]
}
return base64.URLEncoding.EncodeToString(data), nil
}