-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
96 lines (87 loc) · 2.06 KB
/
client.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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var latenc prometheus.Histogram
type Client struct {
bucket *tokenBucket
hcli *http.Client
url string
mutex sync.Mutex
size int
}
func clientLoop(cli *Client) {
for {
if cli.bucket.get() {
go func () {
data := make([]byte, cli.size)
start := time.Now()
_, err := cli.hcli.Post(cli.url, "text/plain", bytes.NewReader(data))
//_, err := cli.hcli.Get(cli.url)
if err != nil {
log.Println(err)
}
lat := time.Now().Sub(start)
latenc.Observe(float64(lat.Milliseconds()))
//log.Printf("%d\n", lat.Microseconds())
}()
} else {
time.Sleep(1*time.Millisecond)
}
}
}
func client() {
latenc = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "bechmark_latency",
Help: "http latency",
Buckets: prometheus.ExponentialBuckets(1, 2, 20),
})
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":9998", nil)
hcli := createClient()
cli := Client{
bucket: newBucket(0),
hcli : hcli,
}
go clientLoop(&cli)
for {
time.Sleep(1*time.Second)
resp, err := hcli.Get(urlCtrl+"/client")
if err != nil {
log.Println(err)
continue
}
if resp.StatusCode != 200 {
log.Println("ctrl status "+resp.Status)
continue;
}
defer resp.Body.Close()
var msg ProtoClientResp
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
continue
}
log.Println(string(body))
err = json.Unmarshal(body, &msg)
if err != nil {
log.Println(err)
continue
}
cli.mutex.Lock()
cli.url = msg.Url
cli.bucket.speed = float64(msg.Speed)
cli.size = msg.Size
cli.mutex.Unlock()
log.Printf("config changed to %f %s\n", cli.bucket.speed, cli.url)
}
}