-
Notifications
You must be signed in to change notification settings - Fork 249
/
thrasher.go
120 lines (106 loc) · 2.53 KB
/
thrasher.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
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
// copied from lib.go
func remote_delete(remote string) error {
req, err := http.NewRequest("DELETE", remote, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 204 {
return fmt.Errorf("remote_delete: wrong status code %d", resp.StatusCode)
}
return nil
}
func remote_put(remote string, length int64, body io.Reader) error {
req, err := http.NewRequest("PUT", remote, body)
if err != nil {
return err
}
req.ContentLength = length
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 201 && resp.StatusCode != 204 {
return fmt.Errorf("remote_put: wrong status code %d", resp.StatusCode)
}
return nil
}
func remote_get(remote string) (string, error) {
resp, err := http.Get(remote)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.New(fmt.Sprintf("remote_get: wrong status code %d", resp.StatusCode))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
reqs := make(chan string, 20000)
resp := make(chan bool, 20000)
fmt.Println("starting thrasher")
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100
// 16 concurrent processes
for i := 0; i < 16; i++ {
go func() {
for {
key := <-reqs
value := fmt.Sprintf("value-%d", rand.Int())
if err := remote_put("http://localhost:3000/"+key, int64(len(value)), strings.NewReader(value)); err != nil {
fmt.Println("PUT FAILED", err)
resp <- false
continue
}
ss, err := remote_get("http://localhost:3000/" + key)
if err != nil || ss != value {
fmt.Println("GET FAILED", err, ss, value)
resp <- false
continue
}
if err := remote_delete("http://localhost:3000/" + key); err != nil {
fmt.Println("DELETE FAILED", err)
resp <- false
continue
}
resp <- true
}
}()
}
count := 10000
start := time.Now()
for i := 0; i < count; i++ {
key := fmt.Sprintf("benchmark-%d", rand.Int())
reqs <- key
}
for i := 0; i < count; i++ {
if <-resp == false {
fmt.Println("ERROR on", i)
os.Exit(-1)
}
}
fmt.Println(count, "write/read/delete in", time.Since(start))
fmt.Printf("thats %.2f/sec\n", float32(count)/(float32(time.Since(start))/1e9))
}